freemarker的集成
1.jar的引入
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
2.配置
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html;charset=UTF-8"></property>
<property name="requestContextAttribute" value="request" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
</bean>
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath">
<value>/WEB-INF/views/</value>
</property>
<property name="defaultEncoding" value="utf-8" />
<property name="freemarkerSettings">
<props>
<prop key="default_encoding">UTF-8</prop>
<prop key="output_encoding">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="classic_compatible">true</prop>
</props>
</property>
</bean>
3.表现层测试代码
package jyd.info.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/User/helloUser")
public String helloUser(ModelMap modelMap) {
List<String> list = new ArrayList<String>();
list.add("111");
list.add("222");
list.add("333");
modelMap.addAttribute("userDo", list) ;
return "/user_list";
}
}
4.测试页面(user_list.ftl)
<#setting classic_compatible=true>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User List</title>
<style type="text/css">
<!--
.STYLE1 {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
font-size: 36px;
color: #FF0000;
}
.STYLE13 {font-size: 24}
.STYLE15 {font-family: Arial, Helvetica, sans-serif; font-size: 24px; }
-->
</style>
</head>
<body>
<table width="1500" height="600" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="500" height="200"> </td>
<td width="500" height="200" align="center" valign="middle"><div align="center"><span class="STYLE1">User List </span></div></td>
<td width="500" height="200"> </td>
</tr>
<tr>
<td width="500" height="200"> </td>
<td width="500" height="200"><table width="500" height="200" border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="160" height="65" align="center" valign="middle"><span class="STYLE15">ID</span></td>
<td width="160" height="65" align="center" valign="middle"><span class="STYLE15">Username</span></td>
<td width="160" height="65" align="center" valign="middle"><span class="STYLE15">Password</span></td>
</tr>
<#list userDo as user>
<tr>
<td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${user}</span></td>
<td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${user}</span></td>
<td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${user}</span></td>
</tr>
</#list>
</table></td>
<td width="500" height="200"> </td>
</tr>
<tr>
<td width="500" height="200"> </td>
<td width="500" height="200"> </td>
<td width="500" height="200"> </td>
</tr>
</table>
</body>
</html>
5.测试结果

文章来源:
http://www.iteye.com/topic/1121801

浙公网安备 33010602011771号