springmvc+velocity 在vm模板下添加通用工具类对象变量
springmvc+velocity 在vm模板上添加通用工具类对象变量
我的第二种方法就是向VelocityContext中插入自定义的对象,但是不知道怎么注入,你知道吗?
在平时工作的项目中经常会在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!!!
1 楼
JetMah
2012-05-20
可以自定义VelocityContext
2 楼
yonge812
2012-05-20
JetMah 写道
可以自定义VelocityContext
我的第二种方法就是向VelocityContext中插入自定义的对象,但是不知道怎么注入,你知道吗?
3 楼
cjjwzs
2012-05-20
for example toolbox.xml
<tool>
<key>math</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
then you config toolAttributes ,,math
<tool>
<key>math</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
then you config toolAttributes ,,math