您的足迹:首页 > JAVA >Spring MVC整合Freemarker

Spring MVC整合Freemarker

我用的是maven管理包,所以首先要在pom.xml中引入包


<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>



然后在spring-mvc.xml文件中配置freemarker配置,springmvc配置可以配置多个ViewResolver,但要添加

<property name="order" value="0" />来分先后,我的freemarker的order为0,说明要优先于jsp的resolver



<!-- freemarker begin -->
<!-- 配置Freemarker屬性文件路徑 -->
<bean id="freemarkerConfiguration"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:conf/freemarker.properties" />
</bean>
<!-- 配置freeMarker模板加載地址 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 視圖解析器會在/WEB-INF/ftl/路徑下掃描視圖文件 -->
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
</bean>
<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
<!-- 配置freeMarker視圖解析器 -->
<bean id="freemakerViewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<!-- 掃描路徑內所有以ftl結尾的文件 -->
<property name="viewNames">
<array>
<value>*.ftl</value>
</array>
</property>
<property name="contentType" value="text/html; charset=UTF-8" />
<property name="cache" value="true" />
<!-- <property name="exposeRequestAttributes" value="true" /> -->
<!-- <property name="exposeSessionAttributes" value="true" /> -->
<!-- <property name="exposeSpringMacroHelpers" value="true" /> -->
<!-- <property name="requestContextAttribute" value="request" /> -->
<!-- 給視圖解析器配置優先級,你可以給之前jsp視圖解析器的值配為2 -->
<property name="order" value="0" />
</bean>
<!-- freemarker end -->


当然还要在classpath下配置freemarker的基本freemarker.properties


tag_syntax=auto_detect
template_update_delay=2
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
date_format=yyyy-MM-dd
time_format=HH:mm:ss
datetime_format=yyyy-MM-dd HH:mm:ss


然后配置helloworld.ftl


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>${word0}${word1}</h2>
${date?string('yyyy-MM-dd')}
</body>
</html>


controller代码


package com.ljtxh.enterprise.web.front;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/helloWorld")
public class HelloWorldController {
    @RequestMapping
    public String helloWorld(Model model) {
       String word0 = "Hello ";
       String word1 = "World!";
       Date date=new Date();
       //將數據添加到視圖數據容器中
       model.addAttribute("word0",word0);
       model.addAttribute("word1",word1);
       model.addAttribute("date", date);
        return "helloWorld.ftl";
    }
}

QQ图片20151207210506.png



本博客所有文章如无特别注明均为原创。作者:小天复制或转载请以超链接形式注明转自 钟天才的博客
原文地址《Spring MVC整合Freemarker

相关推荐

网友评论(0)