在Spring MVC和Spring Boot中使用thymeleaf模板
Spring MVC:
POM:
<!-- thymeleaf模板 -->
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
Bean:
<!-- thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML" />
<property name="cacheable" value="false" />
</bean> <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean> <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<!--解决中文乱码-->
<property name="characterEncoding" value="UTF-8"/>
</bean>
Controller:
package com.jsoft.testspringmvc.controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.jsoft.testspringmvc.model.Entry; @Controller
public class IndexController { @RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model) {
Entry entry = new Entry();
entry.setText("Text");
entry.setTitle("Title");
model.addAttribute("entries", entry);
model.addAttribute("entry", new Entry());
return "index";
} }
HTML:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"> <head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head> <body> <form action="#" th:action="@{/}" th:object="${entry}" method="post">
<label>Title</label>
<input type="text" th:field="*{title}"/>
<label>Text</label>
<input type="text" th:field="*{text}"/>
<br/>
<input type="submit" value="Add"/>
</form> <div>
<div th:each="entry: ${entries}">
<h2 th:text="${entry.title}">Title</h2>
<p th:text="${entry.text}">Text</p>
</div>
</div> </body> </html>
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/thymeleaf/test1
Spring Boot:
POM:
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
properties:
#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#\u5F00\u53D1\u65F6\u5173\u95ED\u7F13\u5B58,\u4E0D\u7136\u6CA1\u6CD5\u770B\u5230\u5B9E\u65F6\u9875\u9762
spring.thymeleaf.cache=false
#thymeleaf end
其余的代码和Spring MVC是一致的。
引入外界静态资源的方式@{/xxx},默认的静态资源的根是"static",默认放置在resources下,templates文件夹也是如此。
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/thymeleaf/test2
参考:
https://github.com/mendlik/spring-mvc-thymeleaf(基于全注解的形式的SpringMVC项目中使用thymeleaf模板)
https://www.tianmaying.com/tutorial/spring-mvc-thymeleaf(特别说明,此篇文章的集成Spring MVC是不行的,不要参照)
http://www.cnblogs.com/asdop/p/6093599.html(Spring MVC)
https://my.oschina.net/wangnian/blog/661393(Spring Boot)
http://www.cnblogs.com/java-zhao/p/5503168.html(Spring Boot)
http://jisonami.iteye.com/blog/2301387(Spring Boot)
http://blog.csdn.net/zhouseawater/article/details/71082664(Spring Boot)
http://blog.csdn.net/u012706811/article/details/52185345(Spring Boot)
http://blog.csdn.net/u014695188/article/details/52347318(Spring Boot)
https://github.com/kolorobot/spring-boot-thymeleaf(Spring Boot配置的项目)
https://www.mkyong.com/spring-boot/spring-boot-hello-world-example-thymeleaf/(Spring Boot)
https://spring.io/guides/gs/serving-web-content/(Spring Boot)
https://springframework.guru/spring-boot-web-application-part-2-using-thymeleaf/(Spring Boot)
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix(官方配置项,可以通过搜索关键字thymeleaf来查找,这个链接也是所有标准配置的参考)
http://www.jianshu.com/p/36c3ae042d09(Spring Boot)
http://fanlychie.github.io/post/spring-boot-with-thymeleaf.html(Spring Boot)
在Spring MVC和Spring Boot中使用thymeleaf模板的更多相关文章
- spring mvc 和spring boot 中注解的使用
1 spring mvc和spring boot之间的关系 spring boot包含spring mvc.所以,spring mvc的注解在spring boot总都是可以用的吗? spring b ...
- Spring,Spring MVC及Spring Boot区别
什么是Spring?它解决了什么问题? 我们说到Spring,一般指代的是Spring Framework,它是一个开源的应用程序框架,提供了一个简易的开发方式,通过这种开发方式,将避免那些可能致使代 ...
- Spring MVC和Spring Boot的理解以及比较
Spring MVC是什么?(1)Spring MVC是Spring提供的一个强大而灵活的模块式web框架.通过Dispatcher Servlet, ModelAndView 和 View Reso ...
- Spring MVC 到 Spring Boot 的简化之路(山东数漫江湖)
背景 从Servlet技术到Spring和Spring MVC,开发Web应用变得越来越简捷.但是Spring和Spring MVC的众多配置有时却让人望而却步,相信有过Spring MVC开发经验的 ...
- 【转】Spring,Spring MVC及Spring Boot区别
对于一个Java开发者来说,Spring可谓如雷贯耳,无论是Spring框架,还是Spring引领的IOC,AOP风格,都对后续Java开发产生的深远的影响,同时,Spring社区总能及时响应开发者的 ...
- spring、spring mvc与spring boot的区别是什么?
Spring 的功能 Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 ao ...
- Spring,Spring MVC,Spring Boot 三者比较
Spring,Spring MVC,Spring Boot 三者比较 Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 io ...
- spring mvc和spring boot的区别
spring boot只是一个配置工具,整合工具,辅助工具. springmvc是框架,项目中实际运行的代码 Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等 ...
- Spring MVC 到 Spring BOOT 的简化之路
背景 Spring vs Spring MVC vs Spring Boot Spring FrameWork Spring 还能解决什么问题 Spring MVC 为什么需要Spring Boot ...
随机推荐
- [Usaco2011 Jan]道路和航线
Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...
- Linux上安装wine qq的方法
linxu上安装QQ的发 百度网盘 提取码:f2sn 步骤一.安装wine(详见:https://www.winehq.org/download) // ubuntu/ubuntukylin/mint ...
- linux学习之路6 Vi文本编辑器
vim是vi的增强版本 vim拥有三种模式: 命令模式(常规模式) vim启动后,默认进入命令模式.任何模式都可以通过按esc键回到命令模式(可以多按几次.命令模式可以通过键入不同的命令完成选择.复制 ...
- 二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement
题目传送门 /* 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 */ #include ...
- Selenium基于Python web自动化基础二 -- 免登录、等待及unittest单元测试框架
一.免登录在进行测试的过程中难免会遇到登录的情况,给测试工作添加了工作量,本文仅提供一些思路供参考解决方式:手动请求中添加cookies.火狐的profile文件记录信息实现.人工介入.万能验证码.去 ...
- SVN的三种merge方式【转】
SVN的merge操作是为了保证主干(trunk)和分支(branch)同步,merge方式有: 1.Merge a range of revisions(合并一个范围的版本) 2.Reintegra ...
- iOS从手机相册选择一张照片并显示 Objective-C
要先给app设置访问相册的权限: 在项目的Info.plist文件里添加Privacy - Photo Library Usage Description权限 ViewController.h: #i ...
- [转]最值得拥有的免费Bootstrap后台管理模板
在PHP开发项目中,后台管理因为面向群体相对比较固定,大部分以实现业务逻辑和功能.使用Bootstrap后台模板可以让后端开发很轻松的就展现给客户一个响应式的后台,节约前端开发的时间.下面PHP程序员 ...
- bzoj3940 censoring 题解(AC自动机)
题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...
- cstring 转string
(1)CString转换为string CString cs(_T("cs")); string s; s = (LPCSTR)(CStringA)(cs); (2)string转 ...