springMvc与Freemarker整合
Posted 河北凝讯科技订阅号
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springMvc与Freemarker整合相关的知识,希望对你有一定的参考价值。
项目路径
引jar包
(1)
(2)配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="网页链接
com/xml/ns/javaee" xsi:schemaLocation="网页链接
com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="false" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
(3)配置applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="网页链接
xmlns:xsi="网页链接
xmlns:p="网页链接
xmlns:aop="网页链接
xmlns:tx="网页链接
xmlns:context="网页链接
xmlns:task="网页链接
xsi:schemaLocation="网页链接
网页链接
网页链接
网页链接
网页链接
网页链接
网页链接
网页链接
网页链接
网页链接
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.lmy.freemarker.controller" />
<!-- freemarker的配置 -->
<bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.
freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/" />
<property name="defaultEncoding" value="UTF-8" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="locale">zh_CN</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="number_format">#.##</prop>
</props>
</property>
</bean>
<!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="com.lmy.freemarker.controller.RichFreeMarkerView" />
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.
CommonsMultipartResolver" p:defaultEncoding="UTF-8" />
<!-- 添加注解驱动 -->
</beans>
(4)配置spring-mvc.xml文件
<beans xmlns="网页链接
xmlns:xsi="网页链接
xmlns:p="网页链接
xmlns:context="网页链接
xmlns:mvc="网页链接
xsi:schemaLocation="
网页链接
网页链接
网页链接
网页链接
网页链接
网页链接
<!--
<context:property-placeholder location="classpath:resources.properties"/> -->
<!-- 开启controller注解支持 -->
<!-- 注意事项请参考:网页链接
<context:component-scan base-package="com.lmy.freemarker.controller"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.
Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.
annotation.ControllerAdvice"/>
</context:component-scan>
<mvc:annotation-driven>
</mvc:annotation-driven>
<!-- 当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时,
能映射静态资源 -->
<mvc:default-servlet-handler/>
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.
converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.
AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器-->
</list>
</property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.
CommonsMultipartResolver" p:defaultEncoding="UTF-8" />
</beans>
(5)RichFreeMarkerView.java中的代码
package com.lmy.freemarker.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
public class RichFreeMarkerView extends FreeMarkerView {
public static final String CONTEXT_PATH = "base";
@SuppressWarnings("unchecked")
protected void exposeHelpers(Map model, HttpServletRequest request)
throws Exception {
super.exposeHelpers(model, request);
model.put(CONTEXT_PATH, request.getContextPath());
}
}
(6)controller层(简单写了点逻辑)
package com.lmy.freemarker.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/from")
public class TestFreemarkerController {
@RequestMapping("/query")
public String TestFreemarker(ModelMap modelMap){
String mes="你好!";
String mes1="你好啊!";
int a=2;
modelMap.put("a",a);
modelMap.put("mes", mes);
modelMap.put("mes1", mes1);
return "MyJsp";
}
}
(7)页面
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<a href="from/query">点击</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>MyHtml.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<#if '${a!}'?number==2>
${mes!}<br>
<#else>
${mes1!}
</#if>
</body>
</html>
以上是关于springMvc与Freemarker整合的主要内容,如果未能解决你的问题,请参考以下文章
SpringMVC和Freemarker整合,带自定义标签的使用方法
SpringMvc 整合Freemarker后,ftl页面怎么获取httpSession
SpringMVC--07 视图解析器&整合freemarker
SpringMVC--07 视图解析器&整合freemarker