Spring MVC with Thymeleaf HTML template pages
来源:互联网 发布:jquery.qrcode.js下载 编辑:程序博客网 时间:2024/04/30 13:49
Thymeleaf offers a set of Spring integrations that allow you to use it as a full-featured substitute for JSP in Spring MVC applications.
So here is a simple example just so we can compare how things differ in HTML versus JSP, does remind me of JSF / Viewlets to be honest.
1. Let start with what our simple project looks like in IntelliJ
2. The web.xml is defined as follows
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>Spring MVC Application</display-name>
- <servlet>
- <servlet-name>mvc-dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>mvc-dispatcher</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
3. The spring context XML is defined as follows
WEB-INF/mvc-dispatcher-servlet.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="pivotal.au.pas.springapp.mvc"/>
- <bean id="templateResolver"
- class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
- <property name="prefix" value="/WEB-INF/resources/templates/" />
- <property name="suffix" value=".html" />
- <property name="templateMode" value="HTML5" />
- </bean>
- <bean id="templateEngine"
- class="org.thymeleaf.spring3.SpringTemplateEngine">
- <property name="templateResolver" ref="templateResolver" />
- </bean>
- <bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
- <property name="templateEngine" ref="templateEngine" />
- </bean>
- </beans>
4. Now the PeopleController.java class looks just like it would when rendering a JSP page view and so it should all we have done here is change what the view page will be which in this case is HTML itself. I have created some mock up data , rather then connecting to a RDBMS here.
- package pivotal.au.pas.springapp.mvc;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import pivotal.au.pas.springapp.beans.Person;
- import java.util.ArrayList;
- import java.util.List;
- @Controller
- public class PeopleController
- {
- private static List<Person> people = null;
- static
- {
- people = new ArrayList<Person>();
- people.add(new Person(1, "pas"));
- people.add(new Person(2, "lucia"));
- people.add(new Person(3, "lucas"));
- people.add(new Person(4, "siena"));
- }
- @RequestMapping("/showallpeople")
- public String showallpeople(Model model)
- {
- model.addAttribute("people", people);
- return "people";
- }
- }
5. The view page is just a HTML page as follows
WEB-INF/resources/templates/people.html
- <!DOCTYPE html>
- <html xmlns:th="http://www.thymeleaf.org">
- <head>
- <title>Spring MVC with Thymeleaf - All people</title>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- </head>
- <body>
- <h2> Spring MVC with Thymeleaf - All people </h2>
- <table border="1">
- <thead>
- <tr>
- <th>Id#</th>
- <th>Name</th>
- </tr>
- </thead>
- <tbody>
- <tr th:each="var : ${people}">
- <td th:text="${var.id}"></td>
- <td th:text="${var.name}"></td>
- </tr>
- </tbody>
- </table>
- <hr />
- <address>
- <a href="mailto:papicella@gopivotal.com">Pas Apicella</a>
- </address>
- </body>
- </html>
6. So here is a simple JSP page as the entry page, could of been a HTML page.
index.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Spring MVC with Thymeleaf</title>
- </head>
- <body>
- <h2>Spring MVC with Thymeleaf</h2>
- <ul>
- <li><a href="/greeting">Greeting</a></li>
- <li><a href="/showallpeople">Show All People</a></li>
- </ul>
- <hr />
- <address>
- <a href="mailto:papicella@gopivotal.com">Pas Apicella</a>
- </address>
- </body>
- </html>
7. Run index.jsp
8. Finally when run and I click on "Show All People" link we get display as follows
More Information
http://spring.io/guides/gs/serving-web-content/
http://www.thymeleaf.org/index.html
0 0
- Spring MVC with Thymeleaf HTML template pages
- Spring MVC Thymeleaf
- spring mvc 配置 thymeleaf
- Thymeleaf MVC 集成Spring
- Spring MVC 集成 Thymeleaf
- spring mvc 集成 thymeleaf ----注解
- Spring MVC + thymeleaf 非配置
- ASP TO HTML WITH TEMPLATE
- ASP TO HTML WITH TEMPLATE
- Spring MVC view layer: Thymeleaf vs. JSP
- thymeleaf + titles + Spring MVC 中文乱码
- Spring MVC视图层:thymeleaf vs. JSP
- Spring MVC视图层:thymeleaf vs. JSP
- Spring MVC : Java模板引擎 Thymeleaf
- Thymeleaf MVC与Spring的集成
- Spring MVC中使用Thymeleaf模板引擎
- Thymeleaf模板与Spring MVC集成
- Spring MVC通用URI Template
- 深入分析java web 技术内幕_笔记_八
- epoll
- Leetcode全游戏问题
- 冒泡算法讲解
- 数据库开发(1)引言
- Spring MVC with Thymeleaf HTML template pages
- 变量人生
- C++回顾之前向声明、嵌套类、局部类
- Z-Code System
- “刘未鹏 我在南大的七年”
- static_cast、dynamic_cast、reinterdivt_cast、和const_cast
- CAN2.0A 和CAN2.0B
- 方块计算 nefu 561
- 用心去爱那些爱过我们的人