SSM中加入freemarker教程

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">&nbsp;</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">&nbsp;</td>
  </tr>
  <tr>
    <td width="500" height="200">&nbsp;</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">&nbsp;</td>
  </tr>
  <tr>
    <td width="500" height="200">&nbsp;</td>
    <td width="500" height="200">&nbsp;</td>
    <td width="500" height="200">&nbsp;</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. 后面的就是业务逻辑和获取数据的方式,由于每个人所使用的框架不同,这里就不在累赘!    

 

 

 

    

 

 

转载于:https://my.oschina.net/u/2935618/blog/1456580

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值