码迷,mamicode.com
首页 > 编程语言 > 详细

在Spring MVC和Spring Boot中使用thymeleaf模板

时间:2017-09-14 13:26:02      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:cti   map   http   mit   type   tle   nbsp   work   ase   

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

 

参考:

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和Spring Boot中使用thymeleaf模板

标签:cti   map   http   mit   type   tle   nbsp   work   ase   

原文地址:http://www.cnblogs.com/EasonJim/p/7519854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!