2010-11-02 81 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; 
    } 
}