springmvc整合freemarker怎么弄

springmvc整合freemarker整合流程和原理是什么?最好能有个demo!

[code="java"]
一、 用macro实现自定义指令,例如:

自定义指令可以使用macro指令来定义。
<#macro greet person>
Hello ${person}!
</#macro>
macro指令自身不打印任何内容,它只是用来创建宏变量,所以就会有一个名为greet的变量。
使用这个宏:
<@greet person="Fred"/>
会打印出:
Hello Fred!
二、用java代码标签实现自定义指令:

可以使用TemplateDirectiveModel接口在Java代码中实现自定义指令。
简单示例如下:
1、实现TemplateDirectiveModel接口。
public class UpperDirective implements TemplateDirectiveModel {
public void execute(Environment env,
Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body)
throws TemplateException, IOException {
if (!params.isEmpty()) {
throw new TemplateModelException(
"parameters 此处没有值!");
}
if (loopVars.length != 0) {
throw new TemplateModelException(
" variables 此处没有值!");
}
if (body != null) {
//执行nested body 与FTL中 <#nested> 类似。
body.render(new UpperCaseFilterWriter(env.getOut()));
} else {
throw new RuntimeException("missing body");
}
}
private static class UpperCaseFilterWriter extends Writer {
private final Writer out;
UpperCaseFilterWriter (Writer out) {
this.out = out;
}

public void write(char[] cbuf, int off, int len)
throws IOException {
char[] transformedCbuf = new char[len];
for (int i = 0; i < len; i++) {
transformedCbuf[i] = Character.toUpperCase(cbuf[i + off]);
}
out.write(transformedCbuf);
}
public void flush() throws IOException {
out.flush();
}
public void close() throws IOException {
out.close();
}
}
}

 说明:<#nested>指令执行位于开始和结束标记指令之间的模板代码段。

2、注入FreeMarkerConfigurer的freemarkerVariables中。
例如:在jeecms-servlet-front.xml

说明:
FreeMarkerConfigurer. 、setFreemarkerVariables(Map variables)
底层调用了FreeMarker的Configuration.setAllSharedVariables()方法。
因为更好的实践是将常用的指令作为共享变量放到Configuration中。

3、调用自定义指令:

 [@upper]
         bar
         [#list ["red", "green", "blue"] as color]
              ${color}
        [/#list]
        baaz
 [/@upper]

4、显示输出结果:
BAR RED GREEN BLUE BAAZ
[/code]

[code="java"]
其实也没有什么原理之类的, 大致原理是:将页面中所需要的样式放入FreeMarker文件中,然后将页面所需要的数据动态绑定,并放入Map中,通过调用FreeMarker模板文件解析类process()方法完成静态页面的生成。了解了上面的原理,接下来我就一步 步带您实现FreeMarker生成静态页面。

Demo
http://blog.csdn.net/daryl715/article/details/1657149
[/code]

[code="java"]
freemarker的xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- freemarker的配置 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
    <property name="defaultEncoding" value="utf-8" />
    <property name="freemarkerSettings">
        <props>
            <prop key="template_update_delay">10</prop>
            <prop key="locale">zh_CN</prop>
            <prop key="datetime_format">yyyy-MM-dd</prop>
            <prop key="date_format">yyyy-MM-dd</prop>
            <prop key="number_format">#.##</prop>
        </props>
    </property>
</bean>
<!-- FreeMarker视图解析 。在这里配置后缀名ftl和视图解析器。。 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
    <property name="suffix" value=".ftl" />
    <property name="contentType" value="text/html;charset=utf-8" />
    <property name="exposeRequestAttributes" value="true" />
    <property name="exposeSessionAttributes" value="true" />
    <property name="exposeSpringMacroHelpers" value="true" />
</bean>


[/code]

模板ftl
${user.uId}
${user.uName}
${user.uPassword}
<#list userList as u>
${u.uId}
${u.uName}
${u.uPassword}
</#list>

[code="java"]
/**

  • @author kingcs
    */
    @Controller
    @RequestMapping("/springFreemarkerController")
    public class SpringFreemarkerController {

    /**

    • */
      @RequestMapping(value = "/freemarker", method = RequestMethod.GET)
      public String restList(Model model) {

      User user = new User();
      user.setuId(123);
      user.setuName("zhangsan");
      user.setuPassword("123456");
      user.setuBrithday(new Date());

      model.addAttribute("user", user);

      List users = new ArrayList();
      for(int i=0; i<10; i++) {
      User u = new User();
      u.setuId(i);
      u.setuName("zhangsan");
      u.setuPassword("123456");
      users.add(u);
      }
      model.addAttribute("userList", users);
      return "test";
      }
      }
      [/code]