当客户经过前端页面提交请求后,咱们之前是怎么作的?后端接收请求数据,处理请求,把响应结果交给模板引擎JSP,最后将渲染后的JSP转为HTML,响应给客户端显示。JSP的好处就是当咱们查出一些数据转发到JSP页面之后,咱们能够用JSP轻松实现数据的显示,及交互等。JSP支持很是强大的功能,包括能写Java代码。html
可是呢,咱们如今的这种状况是,第一:SpringBoot这个项目首先是以jar的方式,不是war。第二:咱们用的仍是嵌入式的Tomcat(简化版的,即不支持JSP),因此呢,SpringBoot默认是不支持JSP的。若是咱们直接用纯静态页面的方式,那给咱们开发会带来很是大的麻烦,所以咱们不得已选择,除了JSP外的其余模板引擎。前端
模板引擎的做用就是,咱们来写一个页面模板, 好比有些值,是动态的,咱们在经过写一些表达式,来动态的显示这些值,就像咱们早前经常使用的JSP。模板引擎的做用其实都同样,只不过呢,不一样模板引擎之间,他们可能这个语法有点不同。Spring Boot支持FreeMarker、Groovy、Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf。java
对于模版引擎而言,SpringBoot
默认存放模版文件的路径为src/main/resources/templates
,固然也能够经过配置文件进行修改的。git
固然了,使用JSP也是能够的,但官方已经不建议使用JSP
了,本文也会讲解下SpringBoot
下JSP
的支持的,由于有不少老的项目仍是使用JSP模板的
居多。(不推荐使用JSP也是由于JSP须要编译转换,而其余模板引擎则无需编译转换)程序员
JSP(全称Java Server Page),是web开发最先期的模板引擎产品,随着时代的更新,已渐渐老去,固然目前还未彻底退出市场。SpringBoot微服务架构,全部项目都是以jar文件方式打包部署,嵌入式的Tomcat(简化版,不支持JSP),因此SpringBoot默认是不支持JSP的,那么若是想要整合JSP,就须要独立引入整合依赖,和基础配置。github
SpringBoot
默认存放模版文件的路径为src/main/resources/templates,但因为SpringBoot默认是不支持JSP的,因此咱们不能将JSP文件放在templates目录下。
web
1 <!-- SpringBoot整合JSP:SpringBoot默认整合Tomcat,因此不须要指定JSP版本 --> 2 <dependency> 3 <groupId>org.apache.tomcat.embed</groupId> 4 <artifactId>tomcat-embed-jasper</artifactId> 5 </dependency> 6 <!-- JSP依赖JSTL --> 7 <dependency> 8 <groupId>javax.servlet</groupId> 9 <artifactId>jstl</artifactId> 10 </dependency>
至于web.xml配置文件,是可要可不要了。spring
1 spring: 2 # 配置整合JSP 3 mvc: 4 view: 5 # 配置视图解析器前缀 6 prefix: /WEB-INF/ 7 # 配置视图解析器后缀 8 suffix: .jsp
由于是返回页面 因此不能是@RestController
// @RestController 该注解会将任何返回值都做为JSON暴露,不通过视图解析器 @Controller // SpringBoot整合JSP,不能使用@RestController public class PersonController { @RequestMapping({"/indexJSP","/listJSP"}) public String getIndexJSP() { System.out.println("JSP访问测试。。。。。"); return "index"; } }
1.说明:在完成上面的步骤以后,运行项目就能够访问jsp界面,可是别高兴的太早,当你打成一个jar包的时候,你会发现你不能访问jsp了,缘由是SpringBoot打包时,是不会主动打包webapp目录的,你须要在你的pom文件手动配置把webapp的文件打包进去。详情参考附录。
2.常见错误:没有手动重启服务器!致使没法解析,错误代码500,显示信息:template might not exist or might not be accessible by any of the configured Template Resolvers(模板可能不存在,或者任何配置的模板解析程序都没法访问)
3.若是只是整合JSP,不整合使用其余模板,那么请不要引入其余模板依赖,也无需配置其余模板信息,不然会错错错!!!(SpringBoot默认支持的模板引擎,天然也有对应的默认配置,一旦你添加了对应的依赖,SpringBoot将遵循约定大于配置,自动注入,那你的JSP也就没法访问了)。apache
FreeMarker 是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员能够嵌入他们所开发产品的组件。官网参考后端
在Spring Boot中使用FreeMarker 只需在pom中加入FreeMarker 的starter便可。
1 <!-- SpringBoot整合Freemarker --> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-freemarker</artifactId> 5 </dependency>
1 spring: 2 # Freemarker配置详解(都含有默认配置) 3 freemarker: 4 # 模板加载路径(默认配置就是templates下,能够手动配置,多值使用“,”分割) 5 template-loader-path: 6 - classpath:/templates/ 7 # 配置缓存,开发阶段应该配置为false 由于常常会改,部署后建议开启 8 cache: false 9 # 配置编码设置 10 charset: UTF-8 11 # 建议模版是否存在 12 check-template-location: true 13 # Content-Type 值 14 content-type: text/html 15 # 模板后缀:默认为ftlh(这里测试更改成了html),若是你设置了模板后缀,那么新建的模板文件必须为指定后缀 16 # suffix: .html 17 # 是否启用Freemarker 18 enabled: true 19 # 详解配置能够查看FreeMarkerProperties类
配置说明:这里是向广大朋友概述/举例常见配置,并非全部的都须要,SpringBoot含有默认配置,详情参考
org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties
类,上面的配置中,实际仅配置关闭缓存便可。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 Hello Freemarker!!! 9 <#-- FreeMarker注释:获取数值(注意:这里的${}是FreeMarker语法,相似于EL,要清楚这不是EL表达式,且这里的值不是null,不然会报错) --> 10 ${name } 11 </body> 12 </html>
1 @Controller // 由于是返回页面 因此不能是@RestController 2 public class FreemarkerController { 3 4 @RequestMapping("/indexFtlh") 5 private String freemarkerShowIndex(String name, Model model) { 6 // 将接受的参数经过model共享(实际保存在request中) 7 model.addAttribute("name", name); 8 return "indexFtlh"; 9 } 10 }
常见错误:没有 手动重启服务器!致使没法解析,错误代码500,显示信息:template might not exist or might not be accessible by any of the configured Template Resolvers(模板可能不存在,或者任何配置的模板解析程序都没法访问)
Thymeleaf
是一个XML/XHTML/HTML5
模板引擎,可用于Web与非Web环境中的应用开发。Thymeleaf
的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板建立方式,所以也能够用做静态建模。在Spring Boot中使用Thymeleaf只需在pom中加入Thymeleaf的starter便可。
Thymeleaf官方网站,GitHup(Thymeleaf)
1 <!-- SpringBoot整合Thymeleaf --> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-thymeleaf</artifactId> 5 </dependency>
1 <properties> 2 <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> 3 <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version> 4 </properties>
1 Spring: 2 #开启模板缓存(默认值:true) 3 thymeleaf: 4 cache: false 5 # 在呈现效果前检查模板是否存在. 6 check-template: true 7 # 检查模板位置是否正确(默认值:true) 8 check-template-location: true 9 # Content-Type的值(默认值:text/html) 10 servlet: 11 content-type: text/html 12 # 开启MVC Thymeleaf视图解析(默认值:true) 13 enabled: true 14 # 模板编码 15 encoding: UTF-8 16 # 要被排除在解析以外的视图名称列表 17 # excluded-view-names: 18 # 要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5) 19 mode: HTML5 20 # 在构建URL时添加到视图名称前的前缀(模板加载路径:默认值:classpath:/templates/) 21 prefix: classpath:/templates/ 22 # 在构建URL时添加到视图名称后的后缀(默认值:.html) 23 suffix: .html 24 # Thymeleaf模板解析器在解析器链中的顺序。默认状况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才须要设置这个属性。 25 # template-resolver-order: 26 # 可解析的视图名称列表 27 # view-names: 28 # -
1 <!DOCTYPE html> 2 <html xmlns:th="http://www.thymeleaf.org"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <div th:text="${url}"></div> 9 </body> 10 </html>
在html页面中引入thymeleaf命名空间,即,此时在html模板文件中动态的属性使用th:命名空间修饰 。(避免thymeleaf语法报错,标注警告等问题)
如下简单了解:详情请参阅博主模板语法笔记。
th属性
html有的属性,Thymeleaf基本都有,而经常使用的属性大概有七八个。其中th属性执行的优先级从1~8,数字越低优先级越高。
th:text :设置当前元素的文本内容,相同功能的还有th:utext,二者的区别在于前者不会转义html标签,后者会。优先级不高:order=7
th:value:设置当前元素的value值,相似修改指定属性的还有th:src,th:href。优先级不高:order=6
th:each:遍历循环元素,和th:text或th:value一块儿使用。注意该属性修饰的标签位置,详细日后看。优先级很高:order=2
th:if:条件判断,相似的还有th:unless,th:switch,th:case。优先级较高:order=3
th:insert:代码块引入,相似的还有th:replace,th:include,三者的区别较大,若使用不恰当会破坏html结构,经常使用于公共代码块提取的场景。优先级最高:order=1
th:fragment:定义代码块,方便被th:insert引用。优先级最低:order=8
th:object:声明变量,通常和*{}一块儿配合使用,达到偷懒的效果。优先级通常:order=4
th:attr:修改任意属性,实际开发中用的较少,由于有丰富的其余th属性帮忙,相似的还有th:attrappend,th:attrprepend。优先级通常:order=5
1 @Controller 2 public class HtmlController { 3 4 @RequestMapping("/indexHtml") 5 public String indexHtml(Model model) { 6 model.addAttribute("url","XSGE我的网站:http://www.xsge123.com"); 7 System.out.println("ceshi"); 8 return "indexHtml"; 9 } 10 }
常见错误:没有 手动重启服务器!致使没法解析,错误代码500,显示信息:template might not exist or might not be accessible by any of the configured Template Resolvers(模板可能不存在,或者任何配置的模板解析程序都没法访问)
SpingBoot整合JSP,打包问题:两种总结
1.网上不少说法是,在pom.xml中配置打包地址(这种方法可行)实现以下:
A,修改pom文件,在插件中配置JSP资源路径。
B,修改SpringBoot打包插件版本为(1.4.2.RELEASE)
C,运行Mavne命令打包
1 <!-- SpringBoot打包插件 --> 2 <build> 3 <plugins> 4 <plugin> 5 <groupId>org.springframework.boot</groupId> 6 <artifactId>spring-boot-maven-plugin</artifactId> 7 <version>1.4.2.RELEASE</version> 8 </plugin> 9 </plugins> 10 11 <resources> 12 <resource> 13 <directory>src/main/java</directory> 14 <includes> 15 <include>**/**</include> 16 </includes> 17 </resource> 18 <resource> 19 <directory>src/main/resources</directory> 20 <includes> 21 <include>**/**</include> 22 </includes> 23 <filtering>false</filtering> 24 </resource> 25 <!-- 打包时将jsp文件拷贝到META-INF目录下--> 26 <resource> 27 <!-- 指定resources插件处理哪一个目录下的资源文件 --> 28 <directory>src/main/webapp</directory> 29 <includes> 30 <include>**/**</include> 31 </includes> 32 <!--注意这次必需要放在此目录下才能被访问到--> 33 <targetPath>META-INF/resources</targetPath> 34 <filtering>false</filtering> 35 </resource> 36 </resources> 37 </build>
不少人可能都在网上看到过相似的配置,但几乎没有明确版本!然而,这种配置方式在SpringBoot版本升级后,官方便提出不推荐使用,在SpringBoot1.4.x版本以后,推荐整合JSP时打包为war包方式,SpringBoot项目整合内置Tomcat插件,因此咱们打包的war文件,无需考虑安装服务器问题,就把它看成一个jar文件同样运行便可
Java -jar 文件名.jar/war
2.无需在pom中过多配置,修改打包方式为war便可。
A,修改打包方式为war
B,运行Maven命令打包
C,启动jar测试访问
<packaging>war</packaging>