FreeMarker与Spring MVC的结合应用
Freemarker是一种基于java的模板引擎。SpringMVC对FreeMarker进行一些配置的支持,能够利用Freemarker只关注表现层以及Spring MVC的三层分离的特点,向前端输出HTML代码,实现代码和页面完全的隔离。
在SpringMVC中使用FreeMarker需要在spring的配置文件中做配置,首先是在web.xml中配置SpringMVC的Servlet,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>freemarker_springmvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
然后在spring的配置文件applicationContext.xml中加入FreeMarker的配置:
<!-- freemarker配置 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/templates" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
</props>
</property>
</bean>
其中templateLoaderPath是指定模板文件的位置,freemarkerSetting是对FreeMarker的配置,包括boolean和date时间的显示格式,比如可以设置date_format属性的值为yyyy-MM-ss,格式化格式采用SimpleDateFormat。更多的配置可以参考Configuration和Configurable的API。
最后一步是配置FreeMarker在SpringMVC里面的视图解析器,在freemarker-servlet.xml的文件中添加:
<!-- 针对freemarker的视图配置项 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="/" />
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="requestContextAttribute" value="request" /><!-- 定义request对象名称 -->
<property name="exposeSessionAttributes" value="true" /> <!-- 暴露session属性 -->
</bean>
其中requestContextAttribute属性通过查看springmvc的mvc模板的源码得知,在FreeMarkerViewResolver的父类UrlBasedViewResolver中定义的:
/**
* Set the name of the RequestContext attribute for all views.
* @param requestContextAttribute name of the RequestContext attribute
* @see AbstractView#setRequestContextAttribute
*/
public void setRequestContextAttribute(String requestContextAttribute) {
this.requestContextAttribute = requestContextAttribute;
}
设置requestContext对象的变量名的,这样每个页面都能取得这个对象了。
exposeSessionAttributes的属性设置源码如下:
/**
* Set whether all HttpSession attributes should be added to the
* model prior to merging with the template. Default is "false".
* @see AbstractTemplateView#setExposeSessionAttributes
*/
public void setExposeSessionAttributes(boolean exposeSessionAttributes) {
this.exposeSessionAttributes = exposeSessionAttributes;
}
更多属性设置可以参考spring的文档,这些属性对于velocity模板引擎也是通用的。
Controller代码如下,其中ModelAndView是org.springframework.web.servlet下的:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class TestController { @RequestMapping("/mv")
public ModelAndView test(){
ModelAndView mv = new ModelAndView();
mv.setViewName("main_f");
mv.addObject("user", "bigbang");
return mv;
}
@RequestMapping("/main_first.html")
public String main_first(Model model){
model.addAttribute("user", "bigbang");
return "main_f";
} @RequestMapping("/main_second.html")
public String main_second(Model model){
model.addAttribute("user", "bigbang");
return "main_s";
} @RequestMapping("/anonymous.html")
public String test2(){
return "main_f";
}
}
此处为体现了FreeMarker的使用,所以写了比较多的模块,在此只贴出文件位置图,其中main_f.ftl包含top.ftl、left.ftl和center.ftl,main_s.ftl包含top.ftl、customLeft.ftl和center.ftl:

正常访问下的效果图如下:
请求main_first.html:

请求main_second.html的效果图如下:

FreeMarker与Spring MVC的结合应用的更多相关文章
- FreeMarker与Spring MVC 4集合的HelloWorld示例
0.整体的项目结构 1.引入POM <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...
- 【FreeMarker】Spring MVC与FreeMarker整合(二)
前一篇介绍了FreeMarker的基本使用,本例介绍Spring MVC与FreeMarker整合 不熟悉项目搭建,可参考 [FreeMarker]FreeMarker快速入门(一) 整合 1.新建S ...
- FreeMarker与Spring MVC 4结合错误:Caused by: java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfiguration
添加spring-context-support的依赖到POM: <!-- spring-context-support --> <!-- https://mvnrepository ...
- spring mvc+myBatis配置详解
一.spring mvc Spring框架(框架即:编程注解+xml配置的方式)MVC是Spring框架的一大特征,Spring框架有三大特征(IOC(依赖注入),AOP(面向切面),MVC(建模M- ...
- Spring MVC+FreeMarker简介
最近做项目,刚接触到SpringMVC与FreeMarker框架,就简单介绍一下自己的理解,不正确的地方请大家指教!! 1.Spring MVC工作原理: 用户发送请求--->前端服务器去找相对 ...
- 【转载】Spring MVC 整合 Freemarker
前言 1.为什么要使用Spring MVC呢? 2.为什么要使用Freemarker呢? 3.为什么不使用Struts2呢? 此示例出现的原因就是发现了struts2的性能太差,所以学习Spring ...
- [Spring MVC] - JSP + Freemarker视图解释器整合
Spring MVC中如果只使用JSP做视图,可以使用下面这段即可解决: <!-- 视图解释类 --> <bean class="org.springframework.w ...
- Spring mvc 中使用ftl引用共通文件出错 FreeMarker template error: Error reading included file "/WEB-INF/ftl/common/errormessage.ftl"
初次接触spring mvc,想做一个小的练习项目,结果在ftl文件中引用其它的共通ftl文件时出错.
- spring mvc + freemarker优雅的实现邮件定时发送
1. spring mvc工程中引入相关freemarker\mail的包 如:pom.xml中加入类似 <dependency> <groupId>javax.mail< ...
随机推荐
- PreparedStatement解决sql注入问题
总结 PreparedStatement解决sql注入问题 :sql中使用?做占位符 2.得到PreparedStatement对象 PreparedStatement pst=conn.prepar ...
- 10,SFDC 管理员篇 - 流程自动化
1,Process Builder Setup | Build | Create | Workflow & Approvals | Process Builder 当我们在对象中创建或者修改一 ...
- 配置VS使用winteracter
一.winteracter是什么?为什么要用VS代替wide?winteracter 是一款强大的 Fortran 图形界面函数库,可方便的用 Fortran 生成GUI界面,对话框,菜单,绘图等操作 ...
- primefaces 知识点整理
database组件中设置数据中一行的颜色 需要设置属性: rowStyleClass="#{data.tradeMoney le 0 ? 'exp' : null}"exp样式: ...
- 开源文档管理工具Joomla的网站安装
1.配置PHP开发环境(Apache.PHP.MySQL) 2.安装Joomla网站: 1. 下载安装包 http://www.joomla.org/download.html 2. 登陆Jo ...
- Spark Idea Maven 开发环境搭建
一.安装jdk jdk版本最好是1.7以上,设置好环境变量,安装过程,略. 二.安装Maven 我选择的Maven版本是3.3.3,安装过程,略. 编辑Maven安装目录conf/settings.x ...
- git免密码pull,push
执行git config --global credential.helper store
- KMP算法简明扼要的理解
KMP算法也算是相当经典,但是对于初学者来说确实有点绕,大学时候弄明白过后来几年不看又忘记了,然后再弄明白过了两年又忘记了,好在之前理解到了关键点,看了一遍马上又能理解上来.关于这个算法的详解网上文章 ...
- swift 取出中间文本
func stringmid (wholestring:String,front:String,behind:String)->String { if wholestring.isEmpty ...
- oracle 方向及资料
总结了一下大家的意见,也加了一些个人的看法,Oracle的学习路径,可供参考: 初级阶段: 可以从OCP教材开始,还有文档中的Administrator's Guide.Concepts.Perfor ...