ksplatform学习笔记
1.viewResolver配置中的:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix" value=".vm" />
<property name="exposeRequestAttributes" value="true" />
<property name="requestContextAttribute" value="request" />
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="exposeSessionAttributes" value="true" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="toolboxConfigLocation" value="WEB-INF/toolbox.xml"/>
<property name="viewClass" value="cn.com.ksplatform.core.expand.spring.velocity.VelocityToolbox20View"/>
</bean>
VelocityToolbox20View代码如下:
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; } }
标红部分的作用是:说白了就是velocity在模板解析过程中想使用tool.xml文件中配置的类
3、配置velocity-tool
在实际使用中,需要配置一些velocity-tool
在spring对最新的velocity tool2.0 的支持不是太好,所以需要扩展viewClass类,才能读取velocitytool.Xml文件。
在没有使用layout的velocity中,需要可以直接继承VelocityToolboxView类,然后重写createVelocityContext方法,但是在VelocityLayoutViewResolver中却无法加载继承VelocityToolboxView的类,而是需要继承VelocityLayoutView,去查找源文件的类,发现VelocityLayoutView继承一样VelocityToolboxView类,所以直接继承VelocityLayoutView然后重写createVelocityContext方法,如下:
- package com.cy.wxs.velocity;
- 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.VelocityLayoutView;
- public classVelocityToolbox2View extends VelocityLayoutView{
- @Override
- protected ContextcreateVelocityContext(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;
- }
- }
4、关于velocity tool2.0
Velocity的配置文件较与之前的配置的版本,在配置上有一些改变,具体的配置方法在velocity-tool2.0.jar包的位置:org.apache.velocity.tools.generic中