Spring 4.2.4.RELEASE MVC 学习笔记 - FreeMarker
Spring mvc 整合 Freemarker 作为视图解析控制器
PS:最近看了看自己写的文章,发现有好多人都在看,但是没有一个评论的,这是为什么呢?算了,还是继续写吧。
Freemarker简单介绍(来自网上)
Freemarker是什么
模板引擎:一种基于模板的、用来生成输出文本的通用工具,基于Java的开发包和类库。
Freemarker能做什么
MVC框架中的View层组件、Html页面静态化、代码生成工具、CMS模板引擎、页面栏目动态定制。
为什么要用FreeMarker
1.程序逻辑(Java 程序)和页面设计(FreeMarker模板)分离。
2.分层清晰,利于分工合作。
3.主流Web框架良好的集成(struts2,springmvc)。
4.简单易学、功能强大。
5.免费开源。
FreeMarker优点
1.FreeMarker不依赖于Servlet,网络或Web 环境
2.FreeMarker一开始就是为MVC设计的,它仅仅专注于展示
3.你可以从任意位置加载模板;从类路径下,从数据库中等
4.易于定义特设的宏和函数
Spring mvc 整合 Freemarker
前面简单介绍了一下Freemarker,内容都是来自互联网了,目的让大家有个了解,具体的使用可以去百度。好了,下面开始正式的实际操作。
Maven导入Freemarker依赖jar
打开framework_spring项目pom.xml文件,添加Freemarker依赖jar包配置。这里需要注意的是多配置了spring-context-support.jar,因spring mvc整合freemarker需要此jar(此jar我理解是spring对其他工具支持补丁)。
<!-- spring-context-support,该jar引入能使spring mvc非常好的对freemark的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- 导入freemarker视图解析框架jar与sprig集成 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.22</version>
</dependency>
创建SpringMVC整合Freemarker的配置文件
在spring-mvc-servlet.xml目录下创建spring-mvc-freemarker.xml配置文件,因为前面我在web.xml中配置spring servlet的时候,使用的通配符的方式指定spring mvc的配置文件,所以这里只需要遵循规则起名字就可以了,spring容器会自动的扫描到该文件并且加载。
在spring-mvc-servlet.xml目录下创建spring-mvc-view-resolver.xml配置文件,用来存放Spring mvc所有的视图解析器的注入描述(无聊就改动改动)。
web.xml
<!-- 配置spring mvc 支持 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/spring-mvc-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spring-mvc-freemarker.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/ftl" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="number_format">0.##########</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
</props>
</property>
</bean>
<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean" />
</beans>
spring-mvc-view-resolver.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 默认视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="contentType" value="text/html; charset=UTF-8" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- FreeMarker视图解析器,使用FreeMarker视图解析器时,不需要对prefix属性做配置,如果配置了prefix返回会有500或406错误。-->
<!-- 因为在FreeMarker配置时已经指定了模版存放目录。 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="order" value="0" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="cache" value="true" />
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html; charset=UTF-8" />
</bean>
</beans>
构造FreeMarker的视图模版
上面在spring-mvc-freemarker.xml中描述了FreeMarker的模版存放路径,所以需要在项目中创建相应的目录,然后在创建模版文件。
PS:在下图中你会看到一个freemarker.html文件,其实这个文件没啥用处,因为我的模版文件后缀用的是”.flt”所以Eclipse不识别,打开之后编辑太过难看,不方便我编辑,所以构造对应的html文件用来做编写,编写完成之后再把代码复制到flt文件中。
freemarker.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>FreeMarker</title>
</head>
<body>
<h1>FreeMarker</h1>
</body>
</html>
构造FreeMarker视图解析器Controller
创建一个POJO类为FreeMarkerController.java,创建位置和目录结构见下图。此Controller这整个Spring学习中只处理和Freemarker请求应答有关系的请求方法。
FreeMarkerController.java
package cn.vfire.framework.spring.mvc.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RequestMapping("/ftl")
@RestController
public class FreeMarkerController {
@RequestMapping("/index")
public ModelAndView index() {
return new ModelAndView("freemarker");
}
}
测试
到这里,最基本最简单的spring mvc整合freemarker就完成了,下面做一下测试。
测试地址:http://127.0.0.1:8000/ftl/index