FreeMarker and Spring MVC

FreeMarker现在以我目前的水平看来——有个卵用啊。

实现多视图解析器 http://www.cnblogs.com/HD/p/4142822.html

1.jar包

FreeMarker.jar

2.Spring mvc.xml的配置

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

3.返回Map数据

@Controller
public class TextFreeMarker {

    @RequestMapping(value = "index")
    @ResponseBody
    public ModelAndView testfreemarkerview() {
        ModelAndView mav = new ModelAndView();

        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "xiaoming");
        map.put("age", "21");
        map.put("address", "硅谷");
        map.put("ad", "广州");

        mav.addObject("maplist", map);
        mav.setViewName("index");
        return mav;
    }

}

4.index.flt

<html>
 <body>
<p>你好吗?${maplist.name}</p>  
<p>你好?${maplist.age}</p>  
<p>你吗?${maplist.address}</p>  
<p>你?${maplist.ad}</p>  
</body>
</html>

案列代码:http://download.csdn.net/detail/qq_31179919/9516997

你可能感兴趣的:(spring,freemarker,mvc)