在SpringMVC环境中要是使用Velocity模板引擎来解析View,首先想到是用JSP时,需要配置视图解析器,Velocity同样也需要配置解析器。先看一段简单的配置
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/WEB-INF/vm" /> <property name="configLocation" value="classpath:properties/velocity.properties"/> </bean> <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="prefix" value="/WEB-INF/vm/"/> <property name="suffix" value=".html"/> <!--解决中文乱码--> <property name="contentType" value="text/html;charset=UTF-8" /> </bean>
在模板中输出一句简单的话
<body> Welcome to $name!!! </body>
在SpringMVC中又该如何给这个变量赋值?在SpringMVC中赋值其实很简单
@RequestMapping("velocity") @Controller public class VelocityController { @RequestMapping(value = "entry", method = RequestMethod.GET) public ModelAndView entry() { ModelAndView mv = new ModelAndView(); mv.addObject("name", "Velocity先生"); mv.setViewName("user"); return mv; } }
同样也是会遇到ContextPath路径问题,如果只是针对路径这一个单一问题SpringMVC有一种很简便的解决方案,通过两个简单配置,在Velocity中就可以使用Request对象。
<!-- 开放request属性 --> <property name="exposeRequestAttributes" value="true"/> <property name="requestContextAttribute" value="rc"/>
在HTML中通过下面的方式就可以拿到ContextPath
<img src="$rc.contextPath/resources/images/head.jpg" width="60" heigth="60"/>
使用Velocity-tool2.0同样也可以拿到ContextPath而且不仅如此可以使用Velocity其他的工具对象,但是似乎Spring4对Velocity-tool2.0支持不是太友好,需要费点周折。
首先需要创建一个ViewClass对象,以下代码从网上拷贝过来如下
import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.velocity.context.Context; import org.apache.velocity.tools.Scope; import org.apache.velocity.tools.ToolManager; import org.apache.velocity.tools.view.ViewToolContext; import org.springframework.web.servlet.view.velocity.VelocityToolboxView; public class VelocityToolbox2View extends VelocityToolboxView { @Override protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { // Create a ChainedContext instance. ViewToolContext ctx; ctx = new ViewToolContext(getVelocityEngine(), request, response, getServletContext()); ctx.putAll(model); if (this.getToolboxConfigLocation() != null) { ToolManager tm = new ToolManager(); tm.setVelocityEngine(getVelocityEngine()); tm.configure(getServletContext().getRealPath(getToolboxConfigLocation())); if (tm.getToolboxFactory().hasTools(Scope.REQUEST)) { ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.REQUEST)); } if (tm.getToolboxFactory().hasTools(Scope.APPLICATION)) { ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.APPLICATION)); } if (tm.getToolboxFactory().hasTools(Scope.SESSION)) { ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.SESSION)); } } return ctx; } }
自定义ViewClass对象,下面将配置到SpringMVC解析器中
<!-- 配置 velocity工具类 --> <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/> <!-- Spring4 支持 velocity-tools2.0 --> <property name="viewClass" value="org.lian.util.VelocityToolbox2View"/>
toolbox.xml配置文件如下
<?xml version="1.0" encoding="UTF-8"?> <tools> <toolbox scope="request"> <tool key="link" class="org.apache.velocity.tools.view.LinkTool" forceRelative="true" includeRequestParams="true"/> </toolbox> </tools>
这时我们也可以通借助Velocity工具对象来拿到ContextPath
<img src="$link.contextPath/resources/images/head.jpg" width="60" heigth="60"/>
最后看一下完整配置
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/WEB-INF/vm" /> <property name="configLocation" value="classpath:properties/velocity.properties"/> </bean> <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="prefix" value="/WEB-INF/vm/"/> <property name="suffix" value=".html"/> <!--解决中文乱码--> <property name="contentType" value="text/html;charset=UTF-8" /> <!-- 配置 velocity工具类 --> <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/> <!-- Spring4 支持 velocity-tools2.0 --> <property name="viewClass" value="org.lian.util.VelocityToolbox2View"/> <!-- 开放request属性 --> <property name="exposeRequestAttributes" value="true"/> <property name="requestContextAttribute" value="rc"/> </bean>
相关推荐
springMVC+Velocity+iBATIS的整合小demo
springMVC 整合velocity博客地址http://blog.csdn.net/g290095142/article/details/78181767
Spring+SpringMVC+Mybatis+Velocity+Maven整合
J2EE开发中velocity获取项目地址,使用框架springmvc+velocity视图器,velocity配置toolboxConfigLocation使.vm文件获取项目资源地址和设置静态资源地址。 此处demo实现效果为:test.vm模板通过调用${ctx.rootPath}...
maven+springMVC+mybatis+velocity+mysql+junit项目框架搭建
velocity+springmvc+spring+mybaitsg整合案例 用户管理 带sql ,亲自整合调试,没有问题! /* SQLyog Ultimate v8.32 MySQL - 5.6.19 : Database - test ******************************************************...
maven+springmvc+spring+ibatis+velocity+mysql
一套基于SpringMVC+velocity框架的web应用 ,基于注解,页面使用sitemesh进行包含
2):采用spring3.2作为bean容器管理,springMVC处理请求转发,Velocity进行页面渲染,Mybatis作为数据持久层。CXF作为同步通信机制。 3):数据库进行加密访问。 4):注解的使用。 5):Vecity模板引擎页面渲染...
采用最新的springmvc3.2、mybatis3.2、velocity1.7整合,完整的配置文件和测试例子,注解完成,轻量级的视图搭配
SpringMVC+ibatis+velocity整合例子,虽然比较简单。可以为没有配置好的同学作为参考。
SpringMVC3+velocity最简单配置例子,入门级的demo
Spring4+Velocity2+Maven整合例子,idea开发。直接运行。
springMVC架构,maven , velocity , 数据源配置在项目中(springMVC的配置文件中)。
根据本人公司开发项目自己搭建的一个velocity小例子,就是想看看velocity如何运行加载前台页面
使用最流行的Springmvc+mybatis+velocity+easyui+jquery1.4.1架构
maven+springMVC+mybatis+velocity+mysql+junit 代码生成器
一个使用maven搭建spring mvc的例子(velocity+mybatis)
l springmvc整合velocity l 实现对用户数据的简单增删改查 4. 适用人群 本课程的定位是velocity的系统教学课程 , 从基础到进阶 , 所以只要对velocity感兴趣 , 想系统学习velocity的开发人员都可以学习本课程 ! 学习...
框架采用最流行技术springmvc4.0.6,和最流行持久层框架mybatis3.2.7,还有Velocity - 模板引擎,还有最新日志输出log4j2的配置并且分级别输出到不同文件, 感兴趣赶紧下载吧