代码之家  ›  专栏  ›  技术社区  ›  David Parks

使用带弹簧的速度工具3.0.3

  •  5
  • David Parks  · 技术社区  · 14 年前

    当我更新bean时:

    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
      <property name="cache" value="true"/>
      <property name="prefix" value=""/>
      <property name="suffix" value=".vm"/>
      <property name="toolboxConfigLocation" value="tools.xml" />
    </bean>
    

    通过速度工具的tools.xml路径,我得到:

    Caused by: 
    java.lang.ClassNotFoundException: org.apache.velocity.tools.view.ToolboxManager
    

    我试过插入工具版本2和1.4,也没有这个包结构。我错过了明显的东西吗?弹簧/速度组件支持什么版本的速度工具?

    6 回复  |  直到 14 年前
        1
  •  5
  •   serg    14 年前

    默认情况下,弹簧的速度支持非常过时。我延长 VelocityView 从Spring和Override类 createVelocityContext 我自己初始化工具的方法。 Here 它是如何看待结局的。

        2
  •  10
  •   sphinks    9 年前

    我用一种简单一点的方法。由于缺乏配置文档和示例,我也无法强制使用Velocity工具。我刚得到了velocity-generic-tools-2.0.jar,并在视图解析器中做了一些更改:

    <bean id="velocityViewResolver" 
        class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="order" value="1"/>
        <property name="prefix" value="/WEB-INF/vm/"/>
        <property name="suffix" value=".vm"/>
    
        <property name="exposeSpringMacroHelpers" value="true"/>
        <property name="contentType" value="text/html;charset=UTF-8"/>
        <property name="attributesMap">
            <map>
                <!--Velocity Escape Tool-->
                <entry key="esc"><bean class="org.apache.velocity.tools.generic.EscapeTool"/></entry>
            </map>
        </property>        
    </bean>
    

    然后,在Velocity模板中,您可以像往常一样使用$esc.html($htmlcodevar)。这个解决方案非常简单,没有大量的配置和覆盖Spring类。

        3
  •  1
  •   Scott    14 年前

    对于3.0.5,我使用了一个与SERG发布的类类似的类,唯一的修改是使用Spring没有使用的更新类(tail-through-velocitytoolboxview->servlettoolboxmanager(在createvelocitycontext中使用,我们已重写),这是不推荐使用的类,因此我将SERG答案中的initvelocitytoolcontext修改为:

    private ToolContext getToolContext() throws IllegalStateException, IOException {
      if (toolContext == null) {
        XmlFactoryConfiguration factoryConfiguration = new XmlFactoryConfiguration("Default Tools");
        factoryConfiguration.read(getServletContext().getResourceAsStream(getToolboxConfigLocation()));
        ToolboxFactory factory = factoryConfiguration.createFactory();
        factory.configure(factoryConfiguration);
        toolContext = new ToolContext();
        for (String scope : Scope.values()) {
          toolContext.addToolbox(factory.createToolbox(scope));
        }
      }
      return toolContext;
    }
    

    我还必须更改创建VelocityContext的行,以明显地调用此方法。

    我的豆子现在看起来像:

    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"
          p:cache="false"
          p:prefix=""
          p:suffix=".vm"
          p:layoutUrl="templates/main.vm"
          p:toolboxConfigLocation="/WEB-INF/velocity/velocity-toolbox.xml"
          p:viewClass="path.to.overriden.class.VelocityToolsLayoutView"
    />
    
        4
  •  1
  •   Konrad Garus    12 年前

    受Scott和Serg回答的启发,下面是另一种不需要XML的方法: http://squirrel.pl/blog/2012/07/13/spring-velocity-tools-no-xml/

    public class MyVelocityToolboxView extends VelocityView {
        @Override
        protected Context createVelocityContext(Map<String, Object> model,
                HttpServletRequest request, HttpServletResponse response) {
            ViewToolContext context = new ViewToolContext(getVelocityEngine(),
                    request, response, getServletContext());
    
            ToolboxFactory factory = new ToolboxFactory();
            factory.configure(ConfigurationUtils.getVelocityView());
    
            for (String scope : Scope.values()) {
                context.addToolbox(factory.createToolbox(scope));
            }
    
            if (model != null) {
                for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) model
                        .entrySet()) {
                    context.put(entry.getKey(), entry.getValue());
                }
            }
            return context;
        }
    }
    
        5
  •  1
  •   tsl0922    11 年前

    受以上所有答案的启发,这是我对 VelocityLayoutView 对于弹簧和速度工具2.0,增加了一些改进!

    public class VelocityToolsView extends VelocityLayoutView {
    
        private static final String TOOL_MANAGER_KEY = ViewToolManager.class.getName();
    
        @Override
        protected Context createVelocityContext(
                Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
            ServletContext application = getServletContext();
    
            // use a shared instance of ViewToolManager
            ViewToolManager toolManager = (ViewToolManager)application.getAttribute(TOOL_MANAGER_KEY);
            if(toolManager == null) {
                toolManager = createToolManager(getVelocityEngine(), getToolboxConfigLocation(), application);
                application.setAttribute(TOOL_MANAGER_KEY, toolManager);
            }
    
            ViewToolContext toolContext = toolManager.createContext(request, response);
            if(model != null) { toolContext.putAll(model); }
    
            return toolContext;
        }
    
        private ViewToolManager createToolManager(VelocityEngine velocity, String toolFile, ServletContext application) {
            ViewToolManager toolManager = new ViewToolManager(application, false, false);
            toolManager.setVelocityEngine(velocity);
    
            // generic & view tools config
            FactoryConfiguration config = ConfigurationUtils.getVelocityView();
            // user defined tools config
            if(toolFile != null) {
                FactoryConfiguration userConfig = ConfigurationUtils.load(application.getRealPath(toolFile));
                config.addConfiguration(userConfig);
            }
            toolManager.configure(config);
    
            return toolManager;
        }
    }
    
        6
  •  0
  •   spume    12 年前

    我发现了 this variation 关于@serg的技术对我很有用。