springboot教程1——freeMarket模板

模板引擎的选择

 

  • FreeMarker
  • Thymeleaf
  • Velocity (1.4版本之后弃用,Spring Framework 4.3版本之后弃用)
  • Groovy
  • Mustache
  • 注:jsp应该尽量避免使用,原因如下:

  • jsp只能打包为:war格式,不支持jar格式,只能在标准的容器里面跑(tomcat,jetty都可以)
  • 内嵌的Jetty目前不支持JSP
  • Undertow不支持jsp
  • jsp自定义错误页面不能覆盖spring boot 默认的错误页面

2 freeMarket模板

  1. 依赖的jar(pom.xml引入)

    
        org.springframework.boot
        spring-boot-starter-freemarker
    

    2. 默认加载类路径templates目录下的文件,templates文件夹下存放ftl文件,可以在application.properties中通过属          性spring.freemarker.template-loader-path自定义模版文件所在的目录

spring.freemarker.template-loader-path=classpath:/activiti/
//默认前缀为空
spring.freemarker.prefix=
//默认后缀为.ftl
spring.freemarker.suffix=.ftl
#使用缓存
cache: false
#编码设置
charset: UTF-8
check-template-location: true
content-type: text/html
expose-request-attributes: true
expose-session-attributes: true
request-context-attribute: request
   

  3 代码示范

       WebController.java


package com.roncoo.education.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * spring-boot-demo-6-1
 * 
 * @author wujing
 */
@Controller
@RequestMapping(value = "/web")
public class WebController {

	@RequestMapping(value = "index")
	public String index(ModelMap map) {
		map.put("title", "freemarker hello word");
		return "index"; // 开头不要加上/,linux下面会出错
	}

}

   SpringBootDemo61Application.java

  

package com.roncoo.education;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@ServletComponentScan
@SpringBootApplication
public class SpringBootDemo61Application {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemo61Application.class, args);
	}
}

  index.ftl




	Spring Boot Demo - FreeMarker
	
	
	


	

${title}

 index.css

h1{color: blue;}

 pom.xml



	4.0.0

	com.roncoo.education
	spring-boot-demo-6-1
	0.0.1-SNAPSHOT
	jar

	spring-boot-demo-6-1
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.0.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
	
		
			org.springframework.boot
			spring-boot-starter-freemarker
		
		
		
			org.webjars
			jquery
			2.1.4
		

		
			org.springframework.boot
			spring-boot-devtools
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

springboot教程1——freeMarket模板_第1张图片

你可能感兴趣的:(springboot)