使用velocity后,原来的很多标签无法使用了,必须借助velocity tools来完成,目前velocity tools最新版本是2.0,下面是velocity tools的一些注意事项:

1. 与Spring MVC 3.x/4.x的集成问题

Spring 3.x/4.x只支持1.3.x的velocity tools,要使用2.0必须自己扩展VelocityToolboxView类

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

相关文章:

  • 2021-08-06
  • 2021-09-23
  • 2020-07-13
  • 2019-11-04
  • 2021-09-21
  • 2021-11-28
  • 2019-10-31
  • 2021-08-14
猜你喜欢
  • 2021-11-30
  • 2021-09-28
  • 2021-12-05
  • 2021-11-27
  • 2021-11-19
  • 2021-11-03
  • 2018-04-03
  • 2021-09-27
相关资源
相似解决方案