springboot中模板 freemark,thymeleaf,jsp
Posted 流世幻羽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot中模板 freemark,thymeleaf,jsp相关的知识,希望对你有一定的参考价值。
freemark模板和thymeleaf模板
1.在pom.xml中引入thymeleaf和freemark;
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2.在application.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 ######################################################## ###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
3.编写模板
//注意路径是在配置中默认的,也是可以修改的 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <h1 th:inline="text">Hello.v.2</h1> <p th:text="${hello}"></p> </body> </html> --------freemark模板入下 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <h1>Freemark</h1> <p>${hello}</p> </body> </html>
4.controller层
package com.ithuan.demo.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller //@RequestMapping("/templates") public class TemplateController { @RequestMapping("/hello1111") public String hello1(Map<String,Object> map){ System.err.println("----"); map.put("hello","from TemplateController.helloHtml"); return "hello1"; } @RequestMapping("/hello22") public String hello22(Map map){ System.err.println("2222"); map.put("hello", "最終的大boss"); return "freemakr"; } }
jsp页面展自带的解析不再展示
以上是关于springboot中模板 freemark,thymeleaf,jsp的主要内容,如果未能解决你的问题,请参考以下文章
springboot中模板 freemark,thymeleaf,jsp
springboot中添加模板引擎freemarker和thymeleaf
「快学springboot」SpringBoot整合freeMark模板引擎