spring-mvc整合JSP、FreeMarker、Thymeleaf
使用标准的 maven-archetype-webapp
创建的工程目录
在java中整合模板引擎就不得不说 ModelAndView
对象,看字面意思就知道是模型和视图的对象,通过 ModelAndView
对象可将包含的数据对象与模板引擎进行绑定。通过 ModelAndView
对象的 addObject()
方法给视图设置需要返回的属性。
ModelAndView
对象默认使用请求转发跳转页面。如果要使用重定向跳转页面,则使用 new ModelAndView("redirect:/url")
来跳转。
spring-mvc 整合 JSP
spring-mvc
默认的模板引擎是JSP,可以看到在 application-context.xml
中什么配置都没有使用
-
项目结构
project │ pom.xml │ ├─.idea │ └─src └─main ├─java │ └─com │ └─cisbest │ └─controller │ JspController.java │ ├─resources │ application-context.xml │ └─webapp │ index.jsp │ └─WEB-INF web.xml
-
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>com.cisbest</groupId> <artifactId>template</artifactId> <version>1.0-SNAPSHOT</version> <description>spring-mvc整合JSP、Thymeleaf、FreeMarker,https://www.cnblogs.com/cisbest/p/13503601.html</description> <packaging>war</packaging> <name>spring-mvc整合JSP、Thymeleaf、FreeMarker</name> <url>https://www.cnblogs.com/cisbest/p/13503601.html</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> </dependencies> </project>
-
JspController.java
package com.cisbest.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/cisbest") public class JspController { // 测试地址 http://localhost:8080/cisbest/index?name=是否乱码 @GetMapping("/index") public ModelAndView view(String name){ System.out.println("测试接受的数据是否乱码:user="+name); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/index.jsp"); modelAndView.addObject("name" , name); return modelAndView; } }
-
application-context.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- 初始化Spring核心容器时,扫描com.cisbest包下及其子包下使用注解的类。自动创建并管理该类的对象 --> <context:component-scan base-package="com.cisbest"></context:component-scan> </beans>
-
index.jsp
<%@ page contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>响应数据=====>>>>> ${name}</h2> </body> </html>
-
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <!-- DispatcherServlet对象用于拦截HTTP请求,并且通过url来调用对应的Controller,最后通过Controller来处理对象的HTTP请求 --> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 加载spring IOC容器核心配置文件application-context.xml --> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-context.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 处理请求乱码问题 设置字符集为utf-8 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
spring-mvc 整合 FreeMarker
FreeMarker语法参考 https://freemarker.apache.org/docs/index.html
-
项目结构
project │ pom.xml │ ├─.idea │ └─src └─main ├─java │ └─com │ └─cisbest │ └─controller │ FreeMarkerController.java │ ├─resources │ application-context.xml │ └─webapp └─WEB-INF │ web.xml │ └─freeMarker index.ftl
-
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>com.cisbest</groupId> <artifactId>template</artifactId> <version>1.0-SNAPSHOT</version> <description>spring-mvc整合JSP、Thymeleaf、FreeMarker,https://www.cnblogs.com/cisbest/p/13503601.html</description> <packaging>war</packaging> <name>spring-mvc整合JSP、Thymeleaf、FreeMarker</name> <url>https://www.cnblogs.com/cisbest/p/13503601.html</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency> </dependencies> </project>
-
FreeMarkerController.java
package com.cisbest.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/f") public class FreeMarkerController { // 测试地址:http://localhost:8080/f/index?name=貂蝉 @GetMapping("/index") public ModelAndView view(String name){ System.out.println("接受的数据:" + name); ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("name",name); return modelAndView; } }
-
application-context.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- 初始化Spring核心容器时,扫描com.cisbest包下及其子包下使用注解的类。自动创建并管理该类的对象 --> <context:component-scan base-package="com.cisbest"></context:component-scan> <!-- 配置framework --> <bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="contentType" value="text/html;charset=utf-8"/> <property name="suffix" value=".ftl"/> </bean> <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/freeMarker"/> <property name="freemarkerSettings"> <props> <prop key="defaultEncoding">UTF-8</prop> </props> </property> </bean> </beans>
-
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <!-- DispatcherServlet对象用于拦截HTTP请求,并且通过url来调用对应的Controller,最后通过Controller来处理对象的HTTP请求 --> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 加载spring IOC容器核心配置文件application-context.xml --> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-context.xml</param-value> </init-param> <!-- load-on-startup 参数为0时,表示项目启动时就创建Spring IOC容器并且初始化DispatcherServlet --> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <!--"/" 代表拦截所有的请求--> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 处理请求乱码问题 设置字符集为utf-8 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
-
index.ftl
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p> 响应的数据>>>> ${name} </p> </body> </html>
spring-mvc 整合 Thymeleaf
-
项目结构
project │ pom.xml │ ├─.idea │ └─src └─main ├─java │ └─com │ └─cisbest │ └─controller │ ThymeleafController.java │ ├─resources │ application-context.xml │ └─webapp └─WEB-INF │ web.xml │ └─templates user.html
-
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>com.cisbest</groupId> <artifactId>template</artifactId> <version>1.0-SNAPSHOT</version> <description>spring-mvc整合JSP、Thymeleaf、FreeMarker,https://www.cnblogs.com/cisbest/p/13503601.html</description> <packaging>war</packaging> <name>spring-mvc整合JSP、Thymeleaf、FreeMarker</name> <url>https://www.cnblogs.com/cisbest/p/13503601.html</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.11.RELEASE</version> </dependency> </dependencies> </project>
-
ThymeleafController.java
package com.cisbest.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/t") public class ThymeleafController { // 测试地址:http://localhost:8080/t/user?name=小乔 @GetMapping("/user") public ModelAndView view(String name){ System.out.println("接受的数据:" + name); ModelAndView modelAndView = new ModelAndView("/user"); modelAndView.addObject("name",name); return modelAndView; } }
-
application-context.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- 初始化Spring核心容器时,扫描com.cisbest包下及其子包下使用注解的类。自动创建并管理该类的对象 --> <context:component-scan base-package="com.cisbest"></context:component-scan> <!-- 配置Thymeleaf --> <!-- SpringResourceTemplateResolver automatically integrates with Spring's own --> <!-- resource resolution infrastructure, which is highly recommended. --> <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/WEB-INF/templates/" /> <property name="suffix" value=".html" /> <!-- HTML is the default value, added here for the sake of clarity. --> <property name="templateMode" value="HTML" /> <!-- Template cache is true by default. Set to false if you want --> <!-- templates to be automatically updated when modified. --> <property name="cacheable" value="false" /> <property name="characterEncoding" value="UTF-8"></property> </bean> <!-- SpringTemplateEngine automatically applies SpringStandardDialect and --> <!-- enables Spring's own MessageSource message resolution mechanisms. --> <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver" /> <!-- Enabling the SpringEL compiler with Spring 4.2.4 or newer can speed up --> <!-- execution in most scenarios, but might be incompatible with specific --> <!-- cases when expressions in one template are reused across different data --> <!-- ypes, so this flag is "false" by default for safer backwards --> <!-- compatibility. --> <property name="enableSpringELCompiler" value="true" /> </bean> <!-- 配置参考地址: https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#spring-webflow-integration --> <bean id="thymeleafViewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine"></property> <property name="characterEncoding" value="UTF-8"></property> </bean> </beans>
-
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <!-- DispatcherServlet对象用于拦截HTTP请求,并且通过url来调用对应的Controller,最后通过Controller来处理对象的HTTP请求 --> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 加载spring IOC容器核心配置文件application-context.xml --> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-context.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 处理请求乱码问题 设置字符集为utf-8 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
-
user.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 响应的数据>>>> <span th:text="${name}"> </span> </body> </html>
参考汇总
spring-mvc参考 https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-view
FreeMarker语法参考 https://freemarker.apache.org/docs/index.html