Spring MVC + Thymeleaf

Source: Internet
Author: User
Tags button type

Reference URL: https://www.cnblogs.com/litblank/p/7988689.html

First, Introduction

1, Thymeleaf in a network and no network environment can be run, and completely do not need to start the Web application, that is, it can let the artist in the browser to view the static effect of the page, but also allows programmers to view the server with Data dynamic page effect. When the browser interprets HTML, the undefined label attribute is ignored, so the thymeleaf template can be run statically, and when data is returned to the page, the thymeleaf tag dynamically replaces the static content, making the page dynamic.

2, Thymeleaf out of the box to use the characteristics. It provides standard and spring standard dialect, can directly apply the template to achieve jstl, OGNL expression effect, avoid the daily set of templates, the Jstl, change the label of the puzzle. Developers can also expand and create custom dialects.

3. Thymeleaf provides a spring standard dialect and an optional module that is perfectly integrated with the SPRINGMVC, enabling quick implementation of form bindings, attribute editors, International

and other functions.

Second, the purpose

You do not need to start the server to modify the style. Open HTML directly.

Third, the parser

Thymeleaf Template View Parser configuration steps: The view resolver, template-parser, template engine, commented out code is the personal JSP, Tiles View parser test code, and this example is irrelevant.

Iv. Springmvc+thymeleaf

Pom.xml
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.springthymeleaf</groupId> <artifactId> Springmvc-thymeleaf</artifactid> <packaging>war</packaging> <version>1.0-snapshot</ version> <name>spring Thymeleaf example</name> <url>http://maven.apache.org</url> < Properties> <spring.version>4.1.3.RELEASE</spring.version> <thymeleaf.version>2.1.2.rel ease</thymeleaf.version> </properties> <dependencies> <dependency> <gro Upid>org.springframework</groupid> <artifactId>spring-core</artifactId> <ver Sion>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid&gt        ;spring-web</artifactid> <version>${spring.version}</version> </dependency> <!--<dependency> <groupId>javax.servlet</groupId> <artifactid>servlet        -api</artifactid> <version>2.5</version> <scope>provided</scope>            </dependency> <dependency> <groupId>org.springframework</groupId>        <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> & Lt;artifactid>spring-beans</artifactid> <version>${spring.version}</version> </d Ependency> <d ependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4< /artifactid> <version>${thymeleaf.version}</version> </dependency> <depen Dency> <groupId>org.thymeleaf</groupId> <artifactid>thymeleaf</artifactid&gt            ;        <version>${thymeleaf.version}</version> </dependency> </dependencies> <build> <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory> <plugins> &lt                     ;p lugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> &L t;/configuration> </plugin> </plugins> </build></project>
Xml
<web-app id= "webapp_id" 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>springmvc thymeleaf</display-name> <servlet> &LT;SERVLET-NAME&G T;mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping&        Gt <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-ma pping> <context-param> <param-name>contextConfigLocation</param-name> <param-valu e>/web-inf/mvc-dispatcher-servlet.xml</param-value> </context-param> <listener> <list Ener-class>org.springframework.Web.context.contextloaderlistener</listener-class> </listener></web-app> 
Mvc-dispatcher-servlet.xml

Note: You can learn more about Thymeleef Template Parser's source code, life cycle, etc.

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/ Schema/context "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.spri Ngframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context http://www. Springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http://w Ww.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <context:component-scan base-package=" Com.springthymeleaf "/> <mvc:annotation-driven/> <mvc:resources location="/static/"mapping="/static /** "/> <!--template parser--<bean id=" Templateresolver "class=" Org.thymeleaf.templateresolver.ServletCon Texttemplateresolver "> <property name=" prefix "value="/web-inf/templates/"/> <property name=" suffix "value=". html "/> <property name=" Templatemode "value=" HTM L5 "/> <property name=" cacheable "value=" false "/> <property name=" characterencoding "value=" UTF -8 "/> </bean> <bean id=" Templateengine "class=" Org.thymeleaf.spring4.SpringTemplateEngine "> &L T;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></beans>
Hello.html
 Hellocontroller.java
Package Com.springthymeleaf.controller;import Org.springframework.stereotype.controller;import Org.springframework.ui.modelmap;import Org.springframework.web.bind.annotation.pathvariable;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.RequestParam; @Controller @requestmapping ("/") public class Hellocontroller {@RequestMapping (value = "/{name}", method = requestmethod.get) public String Getmovie (@PathVariable string name, M        Odelmap model) {Model.addattribute ("name", name);        Model.addattribute ("Query", "" ");        Model.addattribute ("Submit", "");    return "Hello"; } @RequestMapping (value = "/query", method = requestmethod.get) public string query (@RequestParam ("name") string nam        E, Modelmap model) {Model.addattribute ("name", "" ");        Model.addattribute ("Query", name);        Model.addattribute ("Submit", ""); Return "HelLo "; } @RequestMapping (value = "/submit", method = Requestmethod.get) public string Submit (@RequestParam ("name") string n        AME, Modelmap model) {Model.addattribute ("name", "");        Model.addattribute ("Query", "" ");        Model.addattribute ("Submit", name);    return "Hello"; }}
Applicationcontext.java
Package Com.springthymeleaf;import Javax.servlet.servletcontext;import org.springframework.stereotype.Component; Import org.springframework.web.context.servletcontextaware;/** * Writes ContextPath to application, for static file references, and URL link addresses */ @Componentpublic class ApplicationContext implements Servletcontextaware {    @Override public    Void Setservletcontext (ServletContext context) {        String ctx = Context.getcontextpath ();        System.out.println ("ctx=" + ctx);        Context.setattribute ("CTX", CTX);    }}
Five, reference links:

Thymeleaf Template Language Introduction http://blog.csdn.net/mlin_123/article/details/51816533

Org.thymeleaf.spring4.templateresolver Template View Parser http://http://blog.csdn.net/mayi92/article/details/77720663

Spring MVC + Thymeleaf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.