springMVC整合Freemarker例子

jopen 11年前

freemarker是Java模板引擎

springmvc的搭建流程就不详细讲解了,需要的同学可以看我之前的博文。

这次主要分享的是:springMVC整合Freemarker

需要的Jar包:

       ——freemarker-2.3.15.jar

      ——jstl.jar

在已搭建好的spring的配置文件的基础上添加以下代码:

(用于解析freemarker的视图文件)

    <!-- freemarker的配置 -->         <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">           <property name="templateLoaderPath" value="/WEB-INF/view/" />           <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视图解析   如返回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>                    <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>    
</div> </div>

 

java文件:

    package com.app.controller;                import java.util.HashMap;        import java.util.Map;                import javax.servlet.http.HttpServletRequest;        import javax.servlet.http.HttpServletResponse;                import org.springframework.stereotype.Controller;        import org.springframework.web.bind.annotation.RequestMapping;        import org.springframework.web.servlet.ModelAndView;                        @Controller        public class FreemarkerDemo {                         @RequestMapping(value="xuan/freemarker")            public ModelAndView testfreemarkerview(HttpServletRequest request,HttpServletResponse response){                ModelAndView mav=new ModelAndView();                System.out.println("你的上传空间");                String str="这是返回给freemarker页面的值";                mav.addObject("haha", str);                         ///这里顺便回顾下HashMap的使用方法        /*      创建:Map<String,String> map = new HashMap<String,String>();               插入元素:map.put("1","a");                移除元素: map.remove("1");               清空: map.clear();*/                Map<String,String> map=new HashMap<String,String>();                map.put("name", "xiaoming");                map.put("age", "21");                map.put("address", "硅谷");                map.put("ad", "广州");                map.remove("ad");                mav.addObject("maplist", map);                mav.setViewName("freemaeker/xuan");                return mav;            }        }  
</div> </div>
freemarker页面:(xuan.ftl)
<html>    <p>我们${haha}</p>    <p>你好吗?${maplist.name}</p>    <p>你好?${maplist.age}</p>    <p>你吗?${maplist.address}</p>    <p>你?${maplist.ad!}</p>        Welcome ${user!}!    Welcome ${user!'your name'}!    或者    ${user?if_exists}        ${user?default('your name')}        如果user找不到值,会输出    Welcome !    Welcome your name!    否则freemarker会报错        </html>  
</div> </div>