SSM中加入freemarker教程
1. SSM撘完后加入freemarker,首先加入对应的jar包:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
2. 在springMVC.xml中配置freeMarker视图解析器和模板文件配置:
<!-- 配置freeMarker视图解析器 -->
<bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<property name="contentType" value="text/html; charset=utf-8"/>
<property name="cache" value="true" />
<property name="suffix" value=".ftl" />
<property name="order" value="0"/> //这个是视图解析器的优先级
</bean>
<!-- zyj配置freeMarker的模板路径 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath">
<value>/WEB-INF/template/ftl/</value>
</property>
<property name="freemarkerVariables">
<props>
<prop key="template_update_delay">5</prop><!--刷新模板的周期,单位为秒 -->
<prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
<prop key="locale">UTF-8</prop><!-- 本地化设置 -->
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.####</prop>
<prop key="boolean_format">true,false</prop>
<prop key="whitespace_stripping">true</prop>
<prop key="tag_syntax">auto_detect</prop>
<prop key="url_escaping_charset">UTF-8</prop>
</props>
</property>
</bean>
3.freemarker模板文件docTest.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>
</tr>
<#list doc as docInfo>
<tr>
<td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${docInfo.docNo}</span></td>
<td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${docInfo.personName}</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>
4. 获取数据测试freemarker:
a.测试链接:<a href="${ctx }/doc/doc/docTest">测试</a>
b.控制器:
@RequestMapping(value="/docTest")
public String getDocList(HttpServletRequest request){
List<HashMap<String,Object>> doc = new ArrayList<HashMap<String,Object>>();
doc = docService.docTest();
request.setAttribute("doc", doc);
return "docTest";
}
5. 后面的就是业务逻辑和获取数据的方式,由于每个人所使用的框架不同,这里就不在累赘!