初次使用Freemarker,在整合Spring mvc时出现在ftl页面中标签直接显示出来了,如下图:
搞了很久,都没找出问题所在,工程配置如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="cms" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/applicationContext*.xml
</param-value>
</context-param>
<!-- spring mvc 配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- spring 配置文件,名称的路径都可以配置,如果不配置此项则自动扫描/WEB-INF/springmvc-servlet.xml,此文件名规则为servlet名-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc-*.xml</param-value>
<!-- 加载多个配置文件 <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/applicationContext_*.xml,
/WEB-INF/conf/user-servlet.xml </param-value> -->
</init-param>
<!--servlet加载方式,启动容器时加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截所有请求 -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring 加载log4j配置文件 -->
<context-param>
<param-name>log4jConfigLoaction</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- 刷新Log4j配置文件的间隔,60秒 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!--可选配置,定义web 应用的别名,如果不设置,缺省为"webapp.root",当tomcat加载多个项目时,会发生名称冲突 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webName.root</param-value>
</context-param>
<!-- 静态资源处理 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
<url-pattern>*.gif</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<!-- Spring 加载 Log4j 的监听器 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 防止内存泄漏 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- init servlet -->
<servlet>
<servlet-name>SystemInitServlet</servlet-name>
<servlet-class>
com.dhf.cms.init.SystemInitServlet
</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>base/admin/index.action</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/view/error500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/view/error404.jsp</location>
</error-page>
</web-app>
springmvc-servlet.xml也加入了:
<!-- Freemarker配置 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/view/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="number_format">0.##########</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>