一、从freemarker谈起 Freemarker使用模板技术进行视图的渲染。自从看了Struts标签、Freemarker、JSTL的性能对比后,我毅然决定放弃Struts标签了!效率太差…… Spring本身支持了对Freemarker的集成。只需要配置一个针对Freemarker的视图解析器即可。 二、Spring MVC视图解析器 视图解析器的工作流程大致是这样的: Controller的某个方法执行完成以后,返回一个视图(比如:listUser) 视图解析器要做的工作就是找到某个对象来完成视图的渲染,或者跳转到其他的逻辑视图。这里的渲染对象通常就是我们的jsp文件或者我们下面用的Freemarker(例如listUser.jsp或者listUser.ftl)。 渲染完成以后,将解析结果发送到客户端浏览器 下面介绍一下本文需要用到的解析器(更多解析器资料): InternalResourceViewResolver:这是一个最常用的解析器。通常使用它指定渲染对象为jsp页面 FreeMarkerViewResolver:这就是Spring与Freemarker整合需要用到的解析器 三、配置多视图,支持freemarker 我们通常不希望所有的动态页面请求都使用Freemarker来渲染,那就需要配置多个视图解析器。网上有很多这方面的帖子。我看到很多人的做法是在web.xml中配置两个DispatcherServlet,一个拦截.do,一个拦截.ftl;然后再写两个dispatcherServlet.xml,配置两个视图解析器;jsp页面、ftl模板就各司其职。
好不容易找到了Spring3.0的中文手册,毫不犹豫分享一下。 注:这个手册重点讲述的是 Spring3.0新增的一些特性。 没有用过的Spring的同学可以再看看Spring2.0的手册。
下面来看Spring的配置: applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/
下面来看web.xml的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.
废话不多说,我们先来看项目所需要的jar包: Spring3: org.springframework.aop-3.1.0.RELEASE.jar org.springframework.asm-3.1.0.RELEASE.jar org.springframework.aspects-3.1.0.RELEASE.jar org.springframework.beans-3.1.0.RELEASE.jar org.springframework.context-3.1.0.RELEASE.jar org.springframework.context.support-3.1.0.RELEASE.jar org.springframework.core-3.1.