Spring mvc整合FreeMarker

本文介绍如何在SpringMVC 3.2环境中整合FreeMarker 2.3.19,通过配置实现模板引擎的集成,并提供了一个简单的示例控制器来展示其用法。

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

1、新建freemarker.properties,放到src目录下面:

01#设置标签类型:square_bracket:[]     auto_detect:[]<>
02tag_syntax=auto_detect
03#模版缓存时间,单位:秒
04template_update_delay=0
05default_encoding=UTF-8
06output_encoding=UTF-8
07locale=zh_CN
08#设置数字格式 ,防止出现 000.00
09number_format=#
10#变量为空时,不会报错
11classic_compatible=true

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


2、在spring配置文件中,加入如下内容:

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>


3、创建IndexController类,进行测试:

01import javax.servlet.http.HttpServletRequest;
02
03import org.springframework.beans.factory.annotation.Autowired;
04import org.springframework.stereotype.Controller;
05import org.springframework.ui.Model;
06import org.springframework.web.bind.annotation.RequestMapping;
07import org.springframework.web.bind.annotation.RequestMethod;
08
09@Controller
10public 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}


4、新建:/WEB-INF/page/index.html

view sourceprint?

1${date?date}
2${user}


转载于:https://my.oschina.net/xiaojianyu/blog/212340

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值