SpringMvc整合Freemarker

1、 新建WebProject,工程名称是SpringFreemarker;然后手动添加jar包,需要的jar包如下: 

23012351_BhQa.jpg 

SpringFramework jar包下载地址: 

http://ebr.springsource.com/repository/app/library/version/detail?name=org.springframework.spring&version=3.0.5.RELEASE 

FreeMarker library下载地址: 

http://ebr.springsource.com/repository/app/bundle/version/detail?name=com.springsource.freemarker&version=2.3.15 

当然你也可以去官方下载 

2、 在web.xml中添加如下配置: 

Java代码  收藏代码

  1. <!-- 加载Spring容器配置 -->  

  2. <listener>  

  3.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

  4. </listener>  

  5. <!-- 设置Spring容器加载配置文件路径 -->  

  6. <context-param>  

  7.     <param-name>contextConfigLocation</param-name>  

  8.     <param-value>classpath*:applicationContext-*.xml</param-value>  

  9. </context-param>  

  10.    

  11. <servlet>  

  12.     <servlet-name>dispatcher</servlet-name>  

  13.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  

  14.     <init-param>  

  15.         <param-name>contextConfigLocation</param-name>  

  16.         <param-value>/WEB-INF/dispatcher.xml</param-value>  

  17.     </init-param>  

  18.     <load-on-startup>1</load-on-startup>  

  19. </servlet>  

  20.    

  21. <servlet-mapping>  

  22.     <servlet-name>dispatcher</servlet-name>  

  23.     <url-pattern>*.do</url-pattern>  

  24. </servlet-mapping>  



上面分别是添加Spring的监听器、以及配置Spring的配置文件、还有SpringMVC的控制器; 

3、 在WEB-INF中添加文件dispatcher.xml,和web.xml中的对应。内容如下: 

Java代码  收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"  

  4.     xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans >  

  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

  7.     http://www.springframework.org/schema/mvc  

  8.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  

  9.     http://www.springframework.org/schema/context   

  10.     http://www.springframework.org/schema/context/spring-context-3.0.xsd  

  11.     http://www.springframework.org/schema/util  

  12.     http://www.springframework.org/schema/util/spring-util-3.0.xsd"  

  13.    

  14.     <context:component-scan base-package="com.hoo" />  

  15.    

  16.     <!-- annotation的方法映射适配器   

  17.     <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  

  18.     -->  

  19.     <!--  annotation默认的方法映射适配器 -->  

  20.     <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>  

  21.       

  22.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  

  23.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  

  24.     </bean>  

  25.        

  26. </beans>  



上面是SpringMVC的基本配置 

4、 在src中添加applicationContext-beans.xml,内容如下: 


Java代码  收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

  4.     xmlns:aop="http://www.springframework.org/schema/aop"  

  5.     xmlns:tx="http://www.springframework.org/schema/tx"   

  6.     xmlns:util="http://www.springframework.org/schema/util"  

  7.     xmlns:context="http://www.springframework.org/schema/context"  

  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans >  

  9.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

  10.     http://www.springframework.org/schema/aop   

  11.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  

  12.     http://www.springframework.org/schema/tx   

  13.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  

  14.     http://www.springframework.org/schema/util   

  15.     http://www.springframework.org/schema/util/spring-util-3.0.xsd  

  16.     http://www.springframework.org/schema/context   

  17.     http://www.springframework.org/schema/context/spring-context-3.0.xsd"  

  18. </beans>  



里面可以添加一些bean的配置 

5、 在src目录添加freemarker.properties配置文件,这个文件是freemarker一些常用的转换,内容如下: 

Java代码  收藏代码

  1. tag_syntax=auto_detect  

  2. template_update_delay=2  

  3. default_encoding=UTF-8  

  4. output_encoding=UTF-8  

  5. locale=zh_CN  

  6. date_format=yyyy-MM-dd  

  7. time_format=HH:mm:ss  

  8. datetime_format=yyyy-MM-dd HH:mm:ss  



6、 在dispatcher.xml中添加freemarker的配置,配置如下: 

Java代码  收藏代码

  1. <!-- 设置freeMarker的配置文件路径 -->  

  2. <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  

  3.     <property name="location" value="classpath:freemarker.properties"/>  

  4. </bean>  

  5.    

  6. <!-- 配置freeMarker的模板路径 -->  

  7. <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  

  8.     <!--property name="freemarkerSettings" ref="freemarkerConfiguration"/-->  

  9.     <property name="templateLoaderPath">  

  10.         <value>/WEB-INF/ftl/</value>  

  11.     </property>  

  12.     <property name="freemarkerVariables">  

  13.         <map>  

  14.             <entry key="xml_escape" value-ref="fmXmlEscape" />  

  15.         </map>  

  16.     </property>  

  17. </bean>  

  18.    

  19. <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>  

  20.    

  21. <!-- 配置freeMarker视图解析器 -->  

  22. <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  

  23.     <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>  

  24.     <property name="viewNames" value="*.ftl"/>  

  25.     <property name="contentType" value="text/html; charset=utf-8"/>  

  26.     <property name="cache" value="true" />  

  27.     <property name="prefix" value="" />  

  28.     <property name="suffix" value="" />  

  29.     <property name="order" value="2"/>  

  30. </bean>  



上面最关键的就是freeMarker的视图解析器viewResolver的配置,viewClass是使用哪个视图解析器,这里是类路径;其他的和jsp的视图解析器都很类似。 

7、 下面在WEB-INF中添加2个ftl模板,在WEB-INF添加ftl,然后添加hello.ftl/hi.ftl,内容分别是: 

hello.ftl 

Java代码  收藏代码

  1. <html>  

  2. <body>  

  3.     <h1>say hello ${name}</h1><br/>  

  4.     ${(1 == 1)?string("yes""no")}  

  5. </body>  

  6. </html>  


hi.ftl 

Java代码  收藏代码

  1. <html>  

  2. <body>  

  3.     <h1>say hello ${name}</h1><br/>  

  4.     ${(1 != 1)?string("yes""no")}  

  5. </body>  

  6. </html>  



8、 添加Controller控制器,代码如下: 

Java代码  收藏代码

  1. package com.hoo.controller;  

  2.    

  3. import org.springframework.stereotype.Controller;  

  4. import org.springframework.ui.ModelMap;  

  5. import org.springframework.web.bind.annotation.RequestMapping;  

  6.    

  7. /** 

  8.  * <b>function:</b> FreeMarker示例控制器 

  9.  * @author hoojo 

  10.  * @createDate 2011-3-3 下午04:50:10 

  11.  * @file HelloWorldController.java 

  12.  * @package com.hoo.controller 

  13.  * @project SpringFreemarker 

  14.  * @version 1.0 

  15.  */  

  16. @Controller   

  17. @RequestMapping("/freeMarker")  

  18. public class HelloWorldController {  

  19.       

  20.     @RequestMapping("/hello")  

  21.     public String sayHello(ModelMap map) {  

  22.         System.out.println("say Hello ……");  

  23.         map.addAttribute("name"" World!");  

  24.         return "/hello.ftl";  

  25.     }  

  26.       

  27.     @RequestMapping("/hi")  

  28.     public String sayHi(ModelMap map) {  

  29.         System.out.println("say hi ……");  

  30.         map.put("name""jojo");  

  31.         return "/hi.ftl";  

  32.     }  

  33.       

  34.     @RequestMapping("/jsp")  

  35.     public String jspRequest(ModelMap map) {  

  36.         System.out.println("jspRequest ……");  

  37.         map.put("name""jsp");  

  38.         return "/temp.jsp";  

  39.     }  

  40. }  



9、 添加index.jsp的测试链接或temp.jsp的内容: 

index.jsp 

Java代码  收藏代码

  1. <body>  

  2.       <a href="freeMarker/hello.do">say hello</a><br/>  

  3.       <a href="freeMarker/hi.do">say hi</a><br/>  

  4.       <a href="freeMarker/jsp.do">jspRequest</a>  

  5. </body>  


temp.jsp 

Java代码  收藏代码

  1. <body>  

  2.     <h3>${name }</h3>  

  3. </body>  



转载于:https://my.oschina.net/u/1170778/blog/225350

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值