在Spring 3.0.3中使用Velocity Tools

时间:2010-11-02 02:19:27

标签: java spring-mvc velocity classnotfoundexception

当我更新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>

使用Velocity Tools的tools.xml路径,我得到:

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

我已经尝试插入工具版本2和1.4,没有这个包结构。我错过了一些明显的东西吗什么版本的Velocity Tools是Spring / Velocity组件支持的?

6 个答案:

答案 0 :(得分:10)

我使用的方式稍微简单一些。由于缺少配置文档和示例,我也无法强制Velocity Tools工作。我只是得到了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>

然后,在速度模板中,您可以像往常一样使用$ esc.html($ htmlCodeVar)。这个解决方案非常简单,没有大量的配置和最重要的弹簧类。

答案 1 :(得分:5)

默认情况下,Spring已经过时了Velocity支持。我从Spring扩展VelocityView类并覆盖我自己初始化Tools的createVelocityContext方法。 Here就是它最后的样子。

答案 2 :(得分:1)

使用3.0.5我使用了与serg发布的类似的类,唯一的修改是使用spring没有使用的更新类(通过VelocityToolboxView尾部 - &gt; 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"
/>

答案 3 :(得分:1)

受到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;
    }
}

答案 4 :(得分:1)

受上述所有答案的启发,这是我对spring {velocity-tools 2.0 VelocityLayoutView的实现,增加了一些改进!

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;
    }
}

答案 5 :(得分:0)

我发现@ {serg'技术的this variation对我有效。