2010-11-02 52 views
5

当我更新的bean:使用速度与工具3.0.3春季

<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,都没有这个包结构。我错过了什么明显的东西?什么版本的Velocity Tools是Spring/Velocity组件支持的?

回答

5

默认情况下,Spring对Velocity的支持非常过时。我从Spring中延伸VelocityView类,并覆盖createVelocityContext方法,其中我自己初始化Tools。 Here是它看起来如何结束。

+0

哦,太棒了,还有什么我可以要求的,谢谢你的参考和澄清我的一切! – 2010-11-02 05:02:45

1

使用3.0.5我用了一个类似于serg发布的类,唯一的修改是使用spring没有使用的更新类(通过VelocityToolboxView - > ServletToolboxManager(用在我们覆盖的createVelocityContext中)是它被废弃的课,所以我修改了initVelocityToolContext在SERG的回答是:

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" 
/> 
11

我使用了一种更简单的方法。由于缺少配置文档和示例,我也无法强制Velocity 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类。

1

由斯科特和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; 
    } 
} 
1

受上述所有答案的启发,这是我对VelocityLayoutView针对spring和velocity-tools 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; 
    } 
}