在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)

https://leibnizhu.gitlab.io/2016/11/26/Spring-Boot%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8%EF%BC%88%E4%B8%89%EF%BC%89%E2%80%94%E2%80%94%E4%BD%BF%E7%94%A8Thymeleaf%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E/(Spring Boot)

http://fanlychie.github.io/post/spring-boot-with-thymeleaf.html(Spring Boot)

转载于:https://www.cnblogs.com/EasonJim/p/7519854.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值