Springboot 使用模板引擎 - 立flag网

Springboot 使用模板引擎

spring boot使用thymeleaf

  1. 在pom.xml中引入thymeleaf
  2. 如何关闭thymeleaf缓存
  3. 编写访问模板文件controller
  4. 编写模板文件.html

pom文件加入thymeleaf

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.properties 中关闭thymeleaf缓存

###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

@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

<!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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

关闭freemarker缓存

###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

    /**
     *  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

<!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依赖

<!-- 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和加入配置

@Controller
public class HelloController {
	
	@RequestMapping("/index")
	public String index(Map<String,Object> map){
		map.put("name", "dafeiyu");
		return "index";
	}
}

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp

新建jsp /web-inf/jsp

<html>
<body>
<h2>Hello ${name} </h2>
</body>
</html>

已有 0 条评论

    我有话说: