编辑文章 SpringBoot ## spring boot使用thymeleaf 1. 在pom.xml中引入thymeleaf 2. 如何关闭thymeleaf缓存 3. 编写访问模板文件controller 4. 编写模板文件.html pom文件加入thymeleaf ``` java <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` application.properties 中关闭thymeleaf缓存 ``` xml ###THYMELEAF (ThymeleafAutoConfiguration) #spring.thymeleaf.prefix=classpath:/templates/ #spring.thymeleaf.suffix=.html #spring.thymeleaf.mode=HTML5 #spring.thymeleaf.encoding=UTF-8 # ;charset=<encoding> is added #spring.thymeleaf.content-type=text/html # set to false for hot refresh spring.thymeleaf.cache=false ``` 编写controller ``` stylus @Controller @RequestMapping("/template") public class TemplateController { /** * url /template/hello * @return */ @RequestMapping("/hello") public ModelAndView thymeleaf(){ ModelAndView mav =new ModelAndView("hello"); mav.addObject("name","dafeiyu"); return mav; } } ``` 编写模板文件src/main/resouces/templates/hello.html ``` html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>thymeleaf</title> </head> <body> <h1>hello thymeleaf!!!!</h1> <hr /> <h1>welcome <span th:text="${name}"></span></h1> </body> </html> ``` *** ## Spring Boot 使用freemarker 1. 在pom.xml中引入freemarker 2. 关闭freemarker缓存 3. 编写访问文件的controller 4. 编写模板文件.ftl **pom中加入freemarker** ``` java <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` **关闭freemarker缓存** ``` xml?linenums ###FREEMARKER (FreeMarkerAutoConfiguration) spring.freemarker.allow-request-override=false spring.freemarker.cache=true spring.freemarker.check-template-location=true spring.freemarker.charset=UTF-8 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=false #spring.freemarker.prefix= #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= #spring.freemarker.suffix=.ftl #spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list #spring.freemarker.view-names= # whitelist of view names that can be resolved ``` **编写controller** ``` java /** * url /template/freemarker * @return */ @RequestMapping("/freemarker") public String freemark(Map<String ,Object> map){ map.put("name","dafeiyu"); return "helloFtl"; } ``` **编写模板文件src/main/resouces/templates/helloFtl.ftl** ``` java <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>freemarker</title> </head> <body> <h1>hello freemarker</h1> <hr /> <h1>welcome <span>${name}</span></h1> </body> </html> ``` *** ## springboot 使用jsp jsp和其他的模版引擎要一起使用需要其他处理,如配置双servlet,只记录单独jsp 1. 新建一个maven web project; 2. 在pom.xml文件中添加相应的依赖包; 3. 新建一个HelloController请求控制类; 4. 编写index.jsp页面; 5. 启动类App.java 测试 **新建一个maven web project并添加pom依赖** ``` java <!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 --> <java.version>1.8</java.version> </properties> <dependencies> <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- servlet 依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <!-- JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat 的支持.--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> ``` **新建controller和加入配置** ``` java @Controller public class HelloController { @RequestMapping("/index") public String index(Map<String,Object> map){ map.put("name", "dafeiyu"); return "index"; } } ``` ``` xml # 页面默认前缀目录 spring.mvc.view.prefix=/WEB-INF/jsp/ # 响应页面默认后缀 spring.mvc.view.suffix=.jsp ``` **新建jsp /web-inf/jsp** ``` html <html> <body> <h2>Hello ${name} </h2> </body> </html> ``` <h2>spring boot使用thymeleaf</h2> <ol> <li>在pom.xml中引入thymeleaf</li> <li>如何关闭thymeleaf缓存</li> <li>编写访问模板文件controller</li> <li>编写模板文件.html</li> </ol> <p>pom文件加入thymeleaf</p> <pre><code class="language-java"><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </code></pre> <p>application.properties 中关闭thymeleaf缓存</p> <pre><code class="language-xml">###THYMELEAF (ThymeleafAutoConfiguration) #spring.thymeleaf.prefix=classpath:/templates/ #spring.thymeleaf.suffix=.html #spring.thymeleaf.mode=HTML5 #spring.thymeleaf.encoding=UTF-8 # ;charset=<encoding> is added #spring.thymeleaf.content-type=text/html # set to false for hot refresh spring.thymeleaf.cache=false </code></pre> <p>编写controller</p> <pre><code class="language-stylus">@Controller @RequestMapping("/template") public class TemplateController { /** * url /template/hello * @return */ @RequestMapping("/hello") public ModelAndView thymeleaf(){ ModelAndView mav =new ModelAndView("hello"); mav.addObject("name","dafeiyu"); return mav; } } </code></pre> <p>编写模板文件src/main/resouces/templates/hello.html</p> <pre><code class="language-html"><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>thymeleaf</title> </head> <body> <h1>hello thymeleaf!!!!</h1> <hr /> <h1>welcome <span th:text="${name}"></span></h1> </body> </html> </code></pre> <hr /> <h2>Spring Boot 使用freemarker</h2> <ol> <li>在pom.xml中引入freemarker</li> <li>关闭freemarker缓存</li> <li>编写访问文件的controller</li> <li>编写模板文件.ftl</li> </ol> <p><strong>pom中加入freemarker</strong></p> <pre><code class="language-java"><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </code></pre> <p><strong>关闭freemarker缓存</strong></p> <pre><code class="language-xml?linenums">###FREEMARKER (FreeMarkerAutoConfiguration) spring.freemarker.allow-request-override=false spring.freemarker.cache=true spring.freemarker.check-template-location=true spring.freemarker.charset=UTF-8 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=false #spring.freemarker.prefix= #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= #spring.freemarker.suffix=.ftl #spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list #spring.freemarker.view-names= # whitelist of view names that can be resolved </code></pre> <p><strong>编写controller</strong></p> <pre><code class="language-java"> /** * url /template/freemarker * @return */ @RequestMapping("/freemarker") public String freemark(Map<String ,Object> map){ map.put("name","dafeiyu"); return "helloFtl"; } </code></pre> <p><strong>编写模板文件src/main/resouces/templates/helloFtl.ftl</strong></p> <pre><code class="language-java"><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>freemarker</title> </head> <body> <h1>hello freemarker</h1> <hr /> <h1>welcome <span>${name}</span></h1> </body> </html> </code></pre> <hr /> <h2>springboot 使用jsp</h2> <p>jsp和其他的模版引擎要一起使用需要其他处理,如配置双servlet,只记录单独jsp</p> <ol> <li>新建一个maven web project;</li> <li>在pom.xml文件中添加相应的依赖包;</li> <li>新建一个HelloController请求控制类;</li> <li>编写index.jsp页面;</li> <li>启动类App.java 测试</li> </ol> <p><strong>新建一个maven web project并添加pom依赖</strong></p> <pre><code class="language-java"><!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 --> <java.version>1.8</java.version> </properties> <dependencies> <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- servlet 依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <!-- JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat 的支持.--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> </code></pre> <p><strong>新建controller和加入配置</strong></p> <pre><code class="language-java">@Controller public class HelloController { @RequestMapping("/index") public String index(Map<String,Object> map){ map.put("name", "dafeiyu"); return "index"; } } </code></pre> <pre><code class="language-xml"> # 页面默认前缀目录 spring.mvc.view.prefix=/WEB-INF/jsp/ # 响应页面默认后缀 spring.mvc.view.suffix=.jsp </code></pre> <p><strong>新建jsp /web-inf/jsp</strong></p> <pre><code class="language-html"><html> <body> <h2>Hello ${name} </h2> </body> </html> </code></pre> 标题: 分类: 请选择分类(必选) 博客 酷玩 分享 相册 标签: 按 Tab、回车或逗号把输入的文字变成标签;点 × 或在输入框为空时按退格删除;最多 8 个 封面图: 上传图片 建议尺寸 800x400,图片大小不超过 1MB 展开更多设置(关键词、摘要、转载、评论) 关键词: 摘要: spring boot使用thymeleaf 在pom.xml中引入thymeleaf 如何关闭thymeleaf缓存 编写访问模板文件controller 编写模板文件.html pom文件加入thymeleaf <dependenc... 选项: 是否转载 启用评论 勾选「是否转载」可标注文章来源,原文链接可留空。 正文: 保存修改