5

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

4

6 回答 6

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)。这个解决方案非常简单,没有大量的配置和覆盖 spring 类。

于 2012-04-20T06:45:16.153 回答
5

默认情况下,Spring 具有非常过时的 Velocity 支持。我从 Spring 扩展VelocityView类并覆盖createVelocityContext我自己初始化工具的方法。是它最后的样子。

于 2010-11-02T03:00:24.250 回答
1

在 3.0.5 中,我使用了与 serg 发布的类类似的类,唯一的修改是使用 spring 未使用的更新类(通过 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 现在看起来像:

<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"
/>
于 2011-02-17T14:10:53.997 回答
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;
    }
}
于 2012-07-13T09:06:24.817 回答
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;
    }
}
于 2014-03-18T17:05:55.063 回答
0

我发现@serg 技术的这种变化对我有用。

于 2013-01-16T19:22:18.763 回答