Spring mvc整合FreeMarker

Spring mvc整合FreeMarker,使用的是Spring mvc 3.2 + FreeMarker 2.3.19,以下所示: 

html

一、新建freemarker.properties,放到src目錄下面:java

01 #設置標籤類型:square_bracket:[]     auto_detect:[]<>
02 tag_syntax=auto_detect
03 #模版緩存時間,單位:秒
04 template_update_delay=0
05 default_encoding=UTF-8
06 output_encoding=UTF-8
07 locale=zh_CN
08 #設置數字格式 ,防止出現 000.00
09 number_format=#
10 #變量爲空時,不會報錯
11 classic_compatible=true

12 #auto_import="/WEB-INF/templates/index.ftl" as do


二、在spring配置文件中,加入以下內容:web

01 <?xml version="1.0" encoding="UTF-8"?>
02 <beans xmlns="http://www.springframework.org/schema/beans"
03     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04     xmlns:context="http://www.springframework.org/schema/context"
05     xmlns:mvc="http://www.springframework.org/schema/mvc"
06     xsi:schemaLocation="
07         http://www.springframework.org/schema/beans
08         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd       
09         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd
11                 http://www.springframework.org/schema/mvc
12             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
13
14     <!-- 設置freeMarker的配置文件路徑 -->
15     <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
16         <property name="location" value="classpath:freemarker.properties"/>
17     </bean>
18
19     <!-- 配置freeMarker的模板路徑 -->
20     <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
21         <property name="freemarkerSettings" ref="freemarkerConfiguration"/>
22         <property name="templateLoaderPath">
23             <value>/WEB-INF/</value>
24         </property>
25     </bean>
26
27     <!-- 配置freeMarker視圖解析器 -->
28     <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
29         <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
30         <property name="contentType" value="text/html; charset=utf-8"/>
31         <property name="cache" value="true"/>
32     </bean>

33 </beans>


三、建立IndexController類,進行測試:spring

01 import javax.servlet.http.HttpServletRequest;
02
03 import org.springframework.beans.factory.annotation.Autowired;
04 import org.springframework.stereotype.Controller;
05 import org.springframework.ui.Model;
06 import org.springframework.web.bind.annotation.RequestMapping;
07 import org.springframework.web.bind.annotation.RequestMethod;
08
09 @Controller
10 public class IndexController {
11
12     @RequestMapping(value="/", method=RequestMethod.GET)
13     public String index(HttpServletRequest request, Model model){
14         model.addAttribute("user""張三");
15         model.addAttribute("date"new Date());
16         return "page/index.html";
17     }

18 }


四、新建:/WEB-INF/page/index.htmlspring-mvc

view sourceprint?緩存

1 ${date?date}
2 ${user}
相關文章
相關標籤/搜索