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方法,如下:

  1. package com.cy.wxs.velocity;
  2. import java.util.Map;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.apache.velocity.context.Context;
  6. import org.apache.velocity.tools.Scope;
  7. import org.apache.velocity.tools.ToolManager;
  8. import org.apache.velocity.tools.view.ViewToolContext;
  9. import org.springframework.web.servlet.view.velocity.VelocityLayoutView;
  10. public classVelocityToolbox2View extends VelocityLayoutView{
  11. @Override
  12. protected ContextcreateVelocityContext(Map<String, Object> model,
  13. HttpServletRequest request,HttpServletResponse response)
  14. throws Exception {// Create a
  15. // ChainedContext
  16. // instance.
  17. ViewToolContext ctx;
  18. ctx = new ViewToolContext(getVelocityEngine(),request, response,
  19. getServletContext());
  20. ctx.putAll(model);
  21. if (this.getToolboxConfigLocation() != null) {
  22. ToolManager tm = new ToolManager();
  23. tm.setVelocityEngine(getVelocityEngine());
  24. tm.configure(getServletContext().getRealPath(
  25. getToolboxConfigLocation()));
  26. if (tm.getToolboxFactory().hasTools(Scope.REQUEST)) {
  27. ctx.addToolbox(tm.getToolboxFactory().createToolbox(
  28. Scope.REQUEST));
  29. }
  30. if (tm.getToolboxFactory().hasTools(Scope.APPLICATION)) {
  31. ctx.addToolbox(tm.getToolboxFactory().createToolbox(
  32. Scope.APPLICATION));
  33. }
  34. if (tm.getToolboxFactory().hasTools(Scope.SESSION)) {
  35. ctx.addToolbox(tm.getToolboxFactory().createToolbox(
  36. Scope.SESSION));
  37. }
  38. }
  39. return ctx;
  40. }
  41. }

4、关于velocity tool2.0

Velocity的配置文件较与之前的配置的版本,在配置上有一些改变,具体的配置方法在velocity-tool2.0.jar包的位置:org.apache.velocity.tools.generic中

ksplatform学习笔记的更多相关文章

  1. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  2. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  3. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  4. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

  5. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  6. seaJs学习笔记2 – seaJs组建库的使用

    原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...

  7. CSS学习笔记

    CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...

  8. HTML学习笔记

    HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...

  9. DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记

    今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...

随机推荐

  1. ubuntu中vim的设置

    问题:刚安装的VIM中,backspace不能删除字符,且上下左右箭头没反应. 解决方法: sudo vi  /etc/vim/vimrc.tiny 修改 set compatible为set noc ...

  2. github page更新后不生效

    昨晚在本地git仓库修改了页面内容后,git push上去,到页面去刷新发现,并没有改变.本来还想着是需要点时间来更新,就再等等. 没想到过了十几分钟后,还是没有更新. 然后同时习惯性地打开了邮箱,发 ...

  3. Go语言中cannot convert adminname (type interface {}) to type *: need type assertion的解决办法

    解决的办法是把string(adminname)替换为adminname.(string).其它类型也是类似.

  4. Python property() 函数

    Python property() 函数  Python 内置函数 描述 property() 函数的作用是在新式类中返回属性值. 语法 以下是 property() 方法的语法: class pro ...

  5. joinablequeue模块 生产者消费者模型 Manager模块 进程池 管道

    一.生产者消费者 主要是为解耦(借助队列来实现生产者消费者模型) import queue  # 不能进行多进程之间的数据传输 (1)from multiprocessing import Queue ...

  6. 【英宝通Unity4.0公开课学习 】(四)GUI到物理引擎

    今天老妈打电话来说和老爸吵架了... 真的是家家都有本难念的经啊.前后帮她分析了个半小时才帮她解开心结...现在想想老爸还是蛮可怜的,连分享的人都木有 讲的GUI都看睡着了...因为想着可以用NGUI ...

  7. Spring框架的AOP技术之通知类型

    1. 通知类型 * @Before -- 前置通知 * @AfterReturing -- 后置通知 * @Around -- 环绕通知(目标对象方法默认不执行的,需要手动执行) * @After - ...

  8. 利用委托机制处理.NET中的异常

    WinForm代码 private void button1_Click(object sender, EventArgs e) { try { Convert.ToInt32("abcd& ...

  9. Golang之Mysql操作

    话说当年武大郎对着电脑一顿噼里啪啦,,,对mysql增删改查 增加insert package main import ( "fmt" "github.com/jmoir ...

  10. html转义字符对照表

    常用的html转义字符 字符 描述 实体名称 实体编号 " quotation mark(双引号“半角”) " " ' apostrophe (单引号‘半角’) & ...