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
view plainprint?
  1.     
  2. <web-app version="2.4"  
  3.  xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  6.   
  7.  <display-name>Spring MVC Application</display-name>  
  8.   
  9.     <servlet>  
  10.   <servlet-name>mvc-dispatcher</servlet-name>  
  11.   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  12.         <load-on-startup>1</load-on-startup>  
  13.  </servlet>  
  14.   
  15.     <servlet-mapping>  
  16.         <servlet-name>mvc-dispatcher</servlet-name>  
  17.         <url-pattern>/</url-pattern>  
  18.     </servlet-mapping>  
  19.   
  20. </web-app>   

3. The spring context XML is defined as follows

WEB-INF/mvc-dispatcher-servlet.xml
view plainprint?
  1.     
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  8.   
  9.     <context:component-scan base-package="pivotal.au.pas.springapp.mvc"/>  
  10.   
  11.     <bean id="templateResolver"  
  12.           class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">  
  13.         <property name="prefix" value="/WEB-INF/resources/templates/" />  
  14.         <property name="suffix" value=".html" />  
  15.         <property name="templateMode" value="HTML5" />  
  16.     </bean>  
  17.   
  18.     <bean id="templateEngine"  
  19.           class="org.thymeleaf.spring3.SpringTemplateEngine">  
  20.         <property name="templateResolver" ref="templateResolver" />  
  21.     </bean>  
  22.   
  23.     <bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">  
  24.         <property name="templateEngine" ref="templateEngine" />  
  25.     </bean>  
  26.   
  27. </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.
view plainprint?
  1.     
  2. package pivotal.au.pas.springapp.mvc;  
  3.   
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.ui.Model;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import pivotal.au.pas.springapp.beans.Person;  
  8.   
  9. import java.util.ArrayList;  
  10. import java.util.List;  
  11.   
  12. @Controller  
  13. public class PeopleController  
  14. {  
  15.     private static List<Person> people = null;  
  16.   
  17.     static  
  18.     {  
  19.         people = new ArrayList<Person>();  
  20.         people.add(new Person(1"pas"));  
  21.         people.add(new Person(2"lucia"));  
  22.         people.add(new Person(3"lucas"));  
  23.         people.add(new Person(4"siena"));  
  24.     }  
  25.   
  26.     @RequestMapping("/showallpeople")  
  27.     public String showallpeople(Model model)  
  28.     {  
  29.         model.addAttribute("people", people);  
  30.         return "people";  
  31.     }  
  32. }      

5. The view page is just a HTML page as follows

WEB-INF/resources/templates/people.html
view plainprint?
  1.     
  2. <!DOCTYPE html>  
  3. <html xmlns:th="http://www.thymeleaf.org">  
  4. <head>  
  5.     <title>Spring MVC with Thymeleaf - All people</title>  
  6.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  7. </head>  
  8. <body>  
  9. <h2> Spring MVC with Thymeleaf - All people </h2>  
  10.   
  11. <table border="1">  
  12.     <thead>  
  13.     <tr>  
  14.         <th>Id#</th>  
  15.         <th>Name</th>  
  16.     </tr>  
  17.     </thead>  
  18.     <tbody>  
  19.     <tr th:each="var : ${people}">  
  20.         <td th:text="${var.id}"></td>  
  21.         <td th:text="${var.name}"></td>  
  22.     </tr>  
  23.     </tbody>  
  24. </table>  
  25.   
  26.   
  27.   
  28. <hr />  
  29.   
  30.   
  31. <address>  
  32.     <a href="mailto:papicella@gopivotal.com">Pas Apicella</a>  
  33. </address>  
  34. </body>  
  35. </html>  

6. So here is a simple JSP page as the entry page, could of been a HTML page.

index.jsp
view plainprint?
  1.     
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  3. <html>  
  4. <head>  
  5.     <title>Spring MVC with Thymeleaf</title>  
  6. </head>  
  7. <body>  
  8. <h2>Spring MVC with Thymeleaf</h2>  
  9. <ul>  
  10.     <li><a href="/greeting">Greeting</a></li>  
  11.     <li><a href="/showallpeople">Show All People</a></li>  
  12. </ul>  
  13. <hr />  
  14.   
  15.   
  16. <address>  
  17.     <a href="mailto:papicella@gopivotal.com">Pas Apicella</a>  
  18. </address>  
  19. </body>  
  20. </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
原创粉丝点击