SpringMVC統合テストfreemarker
3928 ワード
1.freemarkerのjarを入れましょう
org.freemarker freemarker 2.3.19
2.freemarkerの構成
3.freemarkerビュー解析器の定義
4.テストコード
コントロールメソッド
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.テストコード
コントロールメソッド
<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>