前一篇介绍了FreeMarker的基本使用,本例介绍Spring MVC与FreeMarker整合
不熟悉项目搭建,可参考
【FreeMarker】FreeMarker快速入门(一)
整合
1、新建Spring MVC项目test-freemarker ,参考:【Maven】Eclipse 使用Maven创建SpringMVC Web项目
web.xml文件如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 5 6 <display-name>test-freemarker</display-name> 7 8 <!--指定Spring的配置在文件所在的目录,默认配置在WEB-INF目录下--> 9 <context-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:spring-context.xml</param-value> 12 </context-param> 13 14 <!-- Spring配置:needed for ContextLoaderListener --> 15 <!-- Bootstraps the root web application context before servlet initialization --> 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 18 </listener> 19 20 <!-- Spring MVC配置 --> 21 <!-- The front controller of this Spring Web application, responsible for 22 handling all application requests --> 23 <servlet> 24 <servlet-name>spring</servlet-name> 25 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 26 <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml --> 27 <init-param> 28 <param-name>contextConfigLocation</param-name> 29 <param-value>classpath:spring-mvc.xml</param-value> 30 </init-param> 31 <load-on-startup>1</load-on-startup> 32 </servlet> 33 34 <!-- Map all requests to the DispatcherServlet for handling --> 35 <servlet-mapping> 36 <servlet-name>spring</servlet-name> 37 <url-pattern>/</url-pattern> 38 </servlet-mapping> 39 40 <!-- 中文过滤器 --> 41 <filter> 42 <filter-name>CharacterEncodingFilter</filter-name> 43 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 44 <init-param> 45 <param-name>encoding</param-name> 46 <param-value>UTF-8</param-value> 47 </init-param> 48 <init-param> 49 <param-name>forceEncoding</param-name> 50 <param-value>true</param-value> 51 </init-param> 52 </filter> 53 <filter-mapping> 54 <filter-name>CharacterEncodingFilter</filter-name> 55 <url-pattern>/*</url-pattern> 56 </filter-mapping> 57 58 </web-app>
2、添加FreeMarker依赖
1 <!-- Spring对外部依赖支持 --> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-context-support</artifactId> 5 <version>5.1.4.RELEASE</version> 6 </dependency> 7 8 <!-- freemarker --> 9 <dependency> 10 <groupId>org.freemarker</groupId> 11 <artifactId>freemarker</artifactId> 12 <version>2.3.28</version> 13 </dependency>
完整POM如下:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.test</groupId> 6 <artifactId>test-freemarker</artifactId> 7 <packaging>war</packaging> 8 <version>0.0.1-SNAPSHOT</version> 9 <name>test-freemarker Maven Webapp</name> 10 11 <!-- 定义maven变量 --> 12 <properties> 13 <!-- spring --> 14 <spring.version>5.1.4.RELEASE</spring.version> 15 16 17 <!-- logback --> 18 <slf4j-api.version>1.7.5</slf4j-api.version> 19 <logback.version>0.9.30</logback.version> 20 21 <!-- Servlet --> 22 <servlet.version>3.0.1</servlet.version> 23 <jsp-api.version>2.2</jsp-api.version> 24 25 <!-- jstl --> 26 <jstl.version>1.2</jstl.version> 27 <standard.version>1.1.2</standard.version> 28 29 <!-- test junit --> 30 <junit.version>3.8.1</junit.version> 31 32 <!-- jdk --> 33 <jdk.version>1.8</jdk.version> 34 <maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version> 35 </properties> 36 37 38 <dependencies> 39 40 <!-- Spring IOC 核心容器 --> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-core</artifactId> 44 <version>${spring.version}</version> 45 </dependency> 46 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-beans</artifactId> 50 <version>${spring.version}</version> 51 </dependency> 52 53 <dependency> 54 <groupId>org.springframework</groupId> 55 <artifactId>spring-context</artifactId> 56 <version>${spring.version}</version> 57 </dependency> 58 59 <dependency> 60 <groupId>org.springframework</groupId> 61 <artifactId>spring-expression</artifactId> 62 <version>${spring.version}</version> 63 </dependency> 64 65 <!-- Spring AOP 切面 模块 --> 66 <dependency> 67 <groupId>org.springframework</groupId> 68 <artifactId>spring-aop</artifactId> 69 <version>${spring.version}</version> 70 </dependency> 71 72 <!-- Spring WEB MVC 模块 --> 73 <dependency> 74 <groupId>org.springframework</groupId> 75 <artifactId>spring-web</artifactId> 76 <version>${spring.version}</version> 77 </dependency> 78 79 <dependency> 80 <groupId>org.springframework</groupId> 81 <artifactId>spring-webmvc</artifactId> 82 <version>${spring.version}</version> 83 </dependency> 84 85 <!-- Spring对外部依赖支持 --> 86 <dependency> 87 <groupId>org.springframework</groupId> 88 <artifactId>spring-context-support</artifactId> 89 <version>5.1.4.RELEASE</version> 90 </dependency> 91 92 <!-- freemarker --> 93 <dependency> 94 <groupId>org.freemarker</groupId> 95 <artifactId>freemarker</artifactId> 96 <version>2.3.28</version> 97 </dependency> 98 99 100 101 102 <!-- logback --> 103 <dependency> 104 <groupId>org.slf4j</groupId> 105 <artifactId>slf4j-api</artifactId> 106 <version>${slf4j-api.version}</version> 107 <type>jar</type> 108 <scope>compile</scope> 109 </dependency> 110 111 <dependency> 112 <groupId>ch.qos.logback</groupId> 113 <artifactId>logback-core</artifactId> 114 <version>${logback.version}</version> 115 <type>jar</type> 116 </dependency> 117 118 <dependency> 119 <groupId>ch.qos.logback</groupId> 120 <artifactId>logback-classic</artifactId> 121 <version>${logback.version}</version> 122 <type>jar</type> 123 </dependency> 124 125 <dependency> 126 <groupId>ch.qos.logback</groupId> 127 <artifactId>logback-access</artifactId> 128 <version>${logback.version}</version> 129 </dependency> 130 131 132 <!-- Servlet --> 133 <dependency> 134 <groupId>javax.servlet</groupId> 135 <artifactId>javax.servlet-api</artifactId> 136 <version>${servlet.version}</version> 137 <scope>provided</scope> 138 </dependency> 139 <dependency> 140 <groupId>javax.servlet.jsp</groupId> 141 <artifactId>jsp-api</artifactId> 142 <version>${jsp-api.version}</version> 143 <scope>provided</scope> 144 </dependency> 145 146 147 <!-- test --> 148 <dependency> 149 <groupId>junit</groupId> 150 <artifactId>junit</artifactId> 151 <version>${junit.version}</version> 152 <scope>test</scope> 153 </dependency> 154 155 </dependencies> 156 157 <build> 158 <plugins> 159 <!-- define the project compile level --> 160 <plugin> 161 <groupId>org.apache.maven.plugins</groupId> 162 <artifactId>maven-compiler-plugin</artifactId> 163 <version>${maven.compiler.plugin.version}</version> 164 <configuration> 165 <source>${jdk.version}</source> 166 <target>${jdk.version}</target> 167 </configuration> 168 </plugin> 169 </plugins> 170 <finalName>test-freemarker</finalName> 171 </build> 172 173 174 </project>
3、讲freemarker配置类注入,spring容器,编辑spring-context.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 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 xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 11 12 <context:component-scan base-package="com.test.freemarker"></context:component-scan> 13 14 <bean id="freemarkerConfig" 15 class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 16 <property name="templateLoaderPath" value="" /> 17 <property name="freemarkerSettings"> 18 <props> 19 <!-- 设置标签类型 两种:[] 和 <> , []这种标记解析要快些 --> 20 <prop key="tag_syntax">auto_detect</prop> 21 <!-- 检查模板更新时间间隔,默认5s --> 22 <prop key="template_update_delay">1</prop> 23 <prop key="default_encoding">UTF-8</prop> 24 <prop key="url_escaping_charset">UTF-8</prop> 25 <prop key="locale">zh-CN</prop> 26 <!-- 时间格式化 --> 27 <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> 28 <prop key="date_format">yyyy-MM-dd</prop> 29 <prop key="time_format">HH:mm:ss</prop> 30 <prop key="number_format">0.##########</prop> 31 <!-- 去掉多余的空格 --> 32 <prop key="whitespace_stripping">true</prop> 33 <prop key="classic_compatible">true</prop> 34 <prop key="template_exception_handler">ignore</prop> 35 </props> 36 </property> 37 </bean> 38 </beans>
4、在springmvc配置文件spring-mvc.xml中,配置freemarker视图解析器,(如果其中包含jsp视图解析器,可以删除)
1 <?xml version="1.0" encoding="UTF-8"?> 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 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 10 <!--SpringMVC只是控制网站跳转逻辑 --> 11 <!-- 只扫描控制器 --> 12 <context:component-scan base-package="com.test.freemarker.controller" use-default-filters="false"> 13 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 14 </context:component-scan> 15 16 <!-- 视图解析器 17 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 18 <property name="prefix" value="/WEB-INF/pages/"></property> 19 <property name="suffix" value=".jsp"></property> 20 </bean>--> 21 22 23 <!-- 视图解析器 :针对freemarker的视图配置 --> 24 <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 25 <property name="prefix" value="/page/" /> 26 <property name="suffix" value=".html" /> 27 <property name="cache" value="true" /> 28 <property name="contentType" value="text/html;charset=UTF-8"></property> 29 <property name="requestContextAttribute" value="request" /> 30 <property name="exposeSpringMacroHelpers" value="true" /> 31 <property name="exposeRequestAttributes" value="true" /> 32 <property name="exposeSessionAttributes" value="true" /> 33 </bean> 34 35 <!-- 默认的注解映射的支持 --> 36 <mvc:annotation-driven></mvc:annotation-driven> 37 38 <!-- 对静态资源文件的访问 --> 39 <mvc:default-servlet-handler/> 40 41 </beans>
5、编辑freemarker页面模板,webapp/page/first.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <h1>${username}</h1> 9 </body> 10 </html>
6、编辑controller,FreemarkerController.java
1 package com.test.freemarker.controller; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import org.springframework.stereotype.Controller; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.servlet.ModelAndView; 12 13 import com.test.freemarker.method.SortMethod; 14 15 @Controller 16 @RequestMapping("/freemarker") 17 public class FreemarkerController { 18 19 20 @RequestMapping("/first") 21 public ModelAndView first() { 22 ModelAndView modelAndView = new ModelAndView("first"); 23 modelAndView.addObject("username", "Hello Freemarker!"); 24 return modelAndView; 25 } 26 27 } 28
7、发布项目到tomcat,并运行,输入地址:http://localhost:8080/test-freemarker/freemarker/first