最近笔者被spring mvc和“百里香叶”搞得死去活来,因此在这里利用一个简单实例来展示如何使用Spring MVC与thymeleaf模版引擎集成来完成WEB应用,并记录其中可能出现的问题与解决办法。这里,笔者使用的IDE是IntelliJ IDEA 2016版。
1.新建一个maven项目:
2. 设置好相关的项目名称等参数,项目目录如下:
3.在pom.xml中添加简单的相关依赖:
<properties>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<springframework.version>4.3.3.RELEASE</springframework.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
以上依赖是运行一个Spring MVC与thymeleaf模版引擎集成web应用的最简单的依赖,缺一不可!
4.新建一个servlet,一般Spring MVC使用的都是DispatcherServlet来实现:
<display-name>MVC Study</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5.新建与上面这个servlet有联系的的xml文件,命名一定要和servlet的<servlet-name>的命名一样,不然运行后会报错
(若<servlet-name>new</servlet-name>这个样子的话,那么xml文件名一定要为new-servlet.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.zhtian.web.controller" />
<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" />
</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" />
</bean>
</beans>
6.实现User类:
public class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
7.实现MainController,这里是用来将URLmapping到hello.html以实现跳转:
@Controller
public class MainController {
@RequestMapping("/")
public String home(Model model) {
model.addAttribute("user", new User("Skye"));
return "hello";
}
}
8.写一个简单的HTML页面。这里为了显示使用了thymeleaf模板引擎,特地加了部分thymeleaf方言,并从Controller中获得传进来的user对象:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div>Hello!</div>
<p th:text="${user.getName()}">user</p>
</body>
</html>
9.尝试运行:
运行结果:
然而,Spring MVC还要学习的还有很多很多很多。。。。= =