springMVC 整合测试 freemarker

1. 加入 freemarker 的 jar 吧包

<!-- freemarker start -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
</dependency>

<!-- freemarker end-->

2. 配置 freemarker

<!-- freeMarker配置 -->
	 <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <!-- 与springmvc中的prefix共同构成模板前缀(即模板路径,与controller中的返回结果共同定位模板文件),是string[],可以配置多个 -->
<!--         <property name="templateLoaderPath" value="/ftl/" /> -->
        <property name="templateLoaderPath" value="" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="freemarkerSettings">
            <props>
                <prop key="tag_syntax">auto_detect</prop>
                <prop key="template_update_delay">5</prop>
                <prop key="defaultEncoding">UTF-8</prop>
                <prop key="url_escaping_charset">UTF-8</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="boolean_format">true,false</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="date_format">yyyy-MM-dd</prop>
                <prop key="time_format">HH:mm:ss</prop>
                <prop key="number_format">0.######</prop>
                <prop key="whitespace_stripping">true</prop>
                <!--空值处理<prop key="classic_compatible">true</prop>-->
                <!--  <prop key="auto_import">/ftl/tags/index.ftl as p,/ftl/spring.ftl as s</prop>-->
            </props>
        </property>


3. 定义freemarker视图解析器

<!-- freemarker 视图解析器 -->
	<bean
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="cache" value="false" />
        <property name="prefix" value="/WEB-INF/ftl/"></property>
        <property name="suffix" value=".ftl"></property>
        <property name="viewClass"
            value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
        <property name="contentType" value="text/html;charset=UTF-8"></property>
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <property name="exposeSpringMacroHelpers" value="true" />
        <property name="requestContextAttribute" value="base"></property>
        <property name="order" value="1"></property>
    </bean>

4. 测试代码

controller 方法

<span style="font-family: Arial, Helvetica, sans-serif;">	@RequestMapping("/testFreemarker")</span>
	public ModelAndView testFreeMarker(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("testFreeMarker");
		mv.addObject("name", "this is testFreemarker");
		return mv;
	}
ftl模板

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
<title>Insert title here</title>  
</head>  
<body>  
Hello ${name}  
</body>  
</html>




你可能感兴趣的:(springMVC 整合测试 freemarker)