一、从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模板就各司其职。
下面来看java代码: Control层代码: package com.hl.usersmanager.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.hl.usersmanager.model.Users; import com.hl.usersmanager.service.IUserService; @Controller public class
下面来看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/
经过两个星期的学习,基本掌握了Spring3+mybatis+mysql的整合。附件为我整理的demo源码。内容包括了Spring mvc、Spring aop、myBatis技术。其中Spring mvc、Spring aop都是采用注解方式实现的。 Demo 所需的Jar