In the Java World MVC Framework, the use of the view technology is many, the most basic is the JSP, there are well-known freemarker and velocity template engine. Thymeleaf is also an excellent template engine that behaves well in the Html5/xhtml view layer and can process any XML file offline. It can be completely substituted for Jsp+jstl.
The following is the official q&a from Thymeleaf:
Q: How does thymeleaf behave compared to freemarker,velocity?
A:freemarker and Velocity are outstanding works in the field of software, but they do not have the same philosophy and thymeleaf in solving template problems.
Thymeleaf emphasizes the natural template, which allows the template to be used as a prototype (the author notes: Because the suffix is. html, which can be opened directly in the browser), and freemarker and velocity do not. And Thymeleaf's syntax is more concise and consistent with current trends in web development. Second, from an architectural point of view,freemarker and velocity are more like text processors, so they can handle many types of content, whereas Thymeleaf is XML-based and can handle only XML-formatted data. So,freemarker and velocity are more generic, but that's exactly what thymeleaf can do with XML, especially in Web applications.
The following is a basic MVC implementation using THYMELEAF, based on the official Spring example: (built with Maven)
The first is the list of Pom.xml:
<?xml version= "1.0" encoding= "UTF-8"? ><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/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId> Org.springframework</groupid> <artifactId>gs-serving-web-content</artifactId> <version> 0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifact Id>spring-boot-starter-parent</artifactid> <version>1.1.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies& Gt <properties> <start-class>hello. Application</start-class> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-maven-plugin</a rtifactid> </plugin> </plugins> </build> <repositories> <repo Sitory> <id>spring-milestone</id> <url>http://repo.spring.io/libs-release</u rl> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestone</id> <url>http://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories></project>
To create a new directory according to the MAVEN specification:
└──SRC └──main └──java └──hello
New Controller,
Package Hello;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestParam, @Controllerpublic class TestController { @RequestMapping ("/greeting") public string Greeting (the @RequestParam (value= "name", Required=false, defaultvalue= "World") string name, model model) { Model.addattribute ("name", name); return "greeting"; }}
Main function,
Package Hello;import Org.springframework.boot.autoconfigure.enableautoconfiguration;import Org.springframework.boot.springapplication;import org.springframework.context.annotation.componentscan;@ Componentscan@enableautoconfigurationpublic class Application {public static void Main (string[] args) { Springapplication.run (Application.class, args);} }
View Templates in
└──main └──resources └──regreeting.html
<! DOCTYPE html>
The meanings of some of the annotations here are no longer described here (as explained in the spring documentation I translated earlier).
Execute MCN Clean Package
Execute the executable jar file under target.
You can then access it in the browser. The next article describes the usage of thymeleaf in detail.
Spring Mvc:java template engine Thymeleaf (i)