我能够从 Spring Boot 中的嵌入式 jar 构建运行 HTML 和 JSP 页面。但是如果您想通过在命令提示符中复制 Jar 来独立运行它,那么您需要复制 JSP 页面文件夹结构,因为它不会在 jar 内容中,您需要稍微更改 pom 文件以便 jar 可以添加外部内容。
第 1 步:添加 Thymeleaf 和 JSP 依赖项
将以下依赖项添加到您的 pom.xml 文件中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
第 2 步:项目结构和文件创建
在源文件夹 src/main/resources 下创建文件夹模板,在该文件夹下创建子文件夹 thymeleaf。并创建一个html文件sample.html(say)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
</head>
<body>
THYMELEAF PAGE: <p th:text="${name}"></p>
</body>
</html>
在 src/main/webapp/WEB-INF 创建子文件夹视图。在views下创建一个jsp文件,sample.jsp(say)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello</title>
</head>
<body>
JSP PAGE: Hello ${name}
</body>
</html>
第 3 步:在您的 application.properties 中设置 thymeleaf 视图名称和用于内部视图解析的 JSP 配置。
#tomcat-connection settings
spring.datasource.tomcat.initialSize=20
spring.datasource.tomcat.max-active=25
#Jasper and thymeleaf configaration
spring.view.prefix= /WEB-INF/
spring.view.suffix= .jsp
spring.view.view-names= views
spring.thymeleaf.view-names= thymeleaf
#Embedded Tomcat server
server.port = 8080
#Enable Debug
debug=true
management.security.enabled=false
第 4 步:创建用于服务 Thymeleaf 和 JSP 页面的控制器:
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class TestController {
@RequestMapping(value="/jasper", method=RequestMethod.GET)
public String newjasper(Map<String, Object> m, String name){
//System.out.print("-- INSIDE JSP CONTROLER ------");
m.put("name", name);
return "views/sample";
}
@RequestMapping(value="/thymeleaf", method=RequestMethod.GET)
public String newthymeleaf(Map<String, Object> m, String name){
//System.out.print("-- INSIDE HTML CONTROLER ------");
m.put("name", name);
return "thymeleaf/sample";
}
}
第 5 步:在某些情况下,您可能需要创建一个配置类 SpringConfig.class(例如)用于 JSP 页面的视图解析。但可选,我不在我的配置文件中使用它。
import org.springframework.web.servlet.view.JstlView;
@Configuration
public class SpringConfig {
@Value("${spring.view.prefix}")
private String prefix;
@Value("${spring.view.suffix}")
private String suffix;
@Value("${spring.view.view-names}")
private String viewNames;
@Bean
InternalResourceViewResolver jspViewResolver() {
final InternalResourceViewResolver viewResolver = new
InternalResourceViewResolver();
viewResolver.setPrefix(prefix);
viewResolver.setSuffix(suffix);
viewResolver.setViewClass(JstlView.class);
viewResolver.setViewNames(viewNames);
return viewResolver;
}
}
第 6 步:测试 jsp 和 html 的应用程序。
当您在浏览器中点击此网址时:http://localhost:8080/thymeleaf?name=rohit。这将打开我们的 sample.html 文件,其参数名称位于页面中心,并使用此 url:http://localhost:8080/jasper?name=rohit 将打开 sample.jsp 页面,其参数名称位于页面中心。