在平时工作的项目中经常会在vm模板中添加自定义的工具类,由于上班时用的是sofa框架,里面的配置跟spring还是有区别的,以前自己也没有做过,今天就尝试了一下,主要配置如下:
<!-- 让Spring启用对annotation的支持 -->
<context:annotation-config></context:annotation-config>
<!-- 自动扫描org.yonge路径下的所有文件,并根据注解完成注入的工作 -->
<context:component-scan base-package="com.yonge"></context:component-scan>
<!-- 配置velocity引擎 -->
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/" /><!-- 模板存放的路径 -->
<property name="velocityProperties">
<props>
<prop key="input.encoding">gbk</prop>
<prop key="output.encoding">gbk</prop>
</props>
</property>
</bean>
<!-- 配置视图的显示 -->
<bean id="ViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="prefix" value="/" /><!-- 视图文件的前缀,即存放的路径 -->
<property name="suffix" value=".vm" /><!-- 视图文件的后缀名 -->
<property name="exposeRequestAttributes" value="true" /><!-- 暴露request属性 -->
<property name="toolboxConfigLocation" value="/WEB-INF/velocity/tools.xml" /><!-- 配置工具类 -->
<!-- VelocityToolboxView 类只支持1.X的velocitytools -->
<!--<property name="viewClass"
value="org.springframework.web.servlet.view.velocity.VelocityToolboxView" /> -->
<property name="viewClass"
value="com.yonge.web.velocity.VelocityToolbox20View" />
<!-- <property name="viewClass"
value="org.springframework.web.servlet.view.velocity.VelocityView" /> -->
</bean>
<!-- 将自定义工具类 注入到VelocityContext中-->
<!-- <bean name="velocityView"
class="org.springframework.web.servlet.view.velocity.VelocityView">
<property name="toolAttributes">
<map>
<entry key="dateUtil" value="com.yonge.web.util.DateUtil" />
</map>
</property>
<property name="url" value="/" />
</bean> -->
上面的配置文件中,VelocityToolbox20View是重写的一个类,是在网上copy的,如下:
public class VelocityToolbox20View 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;
}
}
velocityTool的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<tools>
<data type="boolean" key="xhtml" value="true"/>
<data type="boolean" key="isSimple" value="true"/>
<data type="number" key="version" value="2.0"/>
<data key="foo">this is foo</data>
<data key="bar">this is bar.</data>
<toolbox scope="request">
<!-- <tool key="toytool" class="ToyTool" restrictTo="index*"/> -->
</toolbox>
<toolbox scope="session">
<!-- <tool key="map" class="java.util.HashMap"/> -->
</toolbox>
<toolbox scope="application">
<!-- <tool key="map" class="java.util.HashMap"/> -->
<tool key="dateUtil" class="com.yonge.web.util.DateUtil"/>
</toolbox>
</tools>
用velocityTool很好的解决了添加自定义工具类的问题,但是我最开始的时候是想通过另一种VelocityView解决这个问题,向velocityView中注入toolAttributes属性(如上面的配置文件),该属性的类型是一个Map<String, Class>,key是vm模板上使用的变量名,value是对应自定义的工具类,它也会将toolAttributes属性添加到velocityContext中,但是我却遇到了velocityView不能注入viewClass的问题,因为velocityView是一个实例对象,而viewClass是一个class类型的,导致类型转换错误,希望知道的兄弟们回复一下,thx!!!
分享到:
相关推荐
Spring+SpringMVC+Mybatis+Velocity+Maven整合
maven+springMVC+mybatis+velocity+mysql+junit项目框架搭建
springMVC+Velocity+iBATIS的整合小demo
J2EE开发中velocity获取项目地址,使用框架springmvc+velocity视图器,velocity配置toolboxConfigLocation使.vm文件获取项目资源地址和设置静态资源地址。 此处demo实现效果为:test.vm模板通过调用${ctx.rootPath}...
maven+springmvc+spring+ibatis+velocity+mysql
Spring+Hibernate+SpringMVC+Velocity+Maven整合
SpringMVC+ibatis+velocity整合例子,虽然比较简单。可以为没有配置好的同学作为参考。
Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...
Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统。 Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统 Spring+SpringMVC+...
完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统...
J2ee开源模板后台是基于 springmvc+mybatis+easyui+velocity 技术实现的一套模板后台,把一些通用的功能模板化,可以在线打包生成整站源码包括 jsp页面,为开发人员节省时间 ,生成的功能有增删改查,查询包括按列...
采用最新的springmvc3.2、mybatis3.2、velocity1.7整合,完整的配置文件和测试例子,注解完成,轻量级的视图搭配
一套基于SpringMVC+velocity框架的web应用 ,基于注解,页面使用sitemesh进行包含
maven+springMVC+mybatis+velocity+mysql+junit 代码生成器
使用最流行的Springmvc+mybatis+velocity+easyui+jquery1.4.1架构
spring+springMVC+mybatis+quartz动态定时任务
以上是springmvc+spring+hibernate整合基于eclipse,oracle开发
自己做得简易 spring+springMvc+jdbc整合,可改进内容还很多,仅仅是个demo,tomcat使用的8.0
基于maven+springmvc+redis+mybatis整合案例框架,主要实现redis的读取案例
应用实例,可运行maven+springmvc+mybaits+freemarker+velocity。