SpringBoot+Mybatis+SpringMVC环境配置(五):templates模板使用

Spring Boot中,

在抚远等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、网站设计、外贸营销网站建设 网站设计制作定制制作,公司网站建设,企业网站建设,成都品牌网站建设,成都营销网站建设,成都外贸网站制作,抚远网站建设费用合理。

  • 静态资源(css、js、图片等)默认放在resources/static下面。如果要修改默认存放目录,可以通过设置属性 spring.mvc.static-path-pattern来实现。

  • 模板文件默认放在 templates目录下

  •  Spring boot支持使用模板来开发web应用,支持的模板类型包括

     FreeMarker

       Groovy

       Thymeleaf

        Mustache

Spring boot不建议使用jsp开发web。


本文使用Thymeleaf作为模板引擎来开发web项目。

一、在application.properties中设置Thymeleaf相关属性

# Check that the template exists before rendering it.
spring.thymeleaf.check-template=true
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true
# Content-Type value written to HTTP responses.
spring.thymeleaf.content-type=text/html
# Enable Thymeleaf view resolution for Web frameworks.
spring.thymeleaf.enable=true
# Template files encoding.
spring.thymeleaf.encoding=UTF-8
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.exclude-view-names=index
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5
# Disable template caching.
spring.thymeleaf.cache=false

二、在pom.xml添加依赖

		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		

三、在resources目录下创建templates文件夹,并添加模板html

Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用

test.html





Insert title here


姓名:
邮箱:
昵称:

四、增加Controller

@Controller
public class TemplatesController {
	
	@Autowired
	private UserService userService;	
	@RequestMapping("/index")
	String test(ModelMap map) {
		map.addAttribute("key", "thymeleaf");
		return "index";
	}  
	
	@RequestMapping("/test")
	public String testThymeleaf(ModelMap map) {
		map.addAttribute("user", userService.selectByPrimaryKey(1));
		return "test";
    }
}

五、运行结果

Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用

到这里,Spring Boot + Mybatis +Spring MVC环境就搭建完成了

完整环境下载地址: https://github.com/CatherineHu/Spring-Boot-Mybatis-MVC  


新闻名称:SpringBoot+Mybatis+SpringMVC环境配置(五):templates模板使用
当前路径:http://scyanting.com/article/ggdeso.html