将 Velocity 工具与 Spring 3.0.3 结合使用

发布于 2024-09-30 13:03:04 字数 623 浏览 4 评论 0原文

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

When I update the 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>

With the tools.xml path for Velocity Tools, I get:

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

I've tried plugging in tools version 2 and 1.4, neither have this package structure. Did I miss something obvious? What version of Velocity Tools is the Spring/Velocity component supporting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猫腻 2024-10-07 13:03:04

我用的是稍微简单一点的方法。由于缺乏配置文档和示例,我也无法强制 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>

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

I use a little bit simpler of a way. I also cannot force Velocity Tools to work due to lack of configuration documentation and examples. I just get the velocity-generic-tools-2.0.jar and make a little change in my view resolver:

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

Then, in the velocity template you can use it as usual $esc.html($htmlCodeVar). This solution is very simple, without tons of configs and overriding spring classes.

歌枕肩 2024-10-07 13:03:04

Spring 默认情况下具有非常过时的 Velocity 支持。我从 Spring 扩展了 VelocityView 类,并重写了我自己初始化 Tools 的 createVelocityContext 方法。 这里是它的样子结束。

Spring has very outdated Velocity support by default. I extend VelocityView class from Spring and override createVelocityContext method where I initialize Tools myself. Here is how it looks at the end.

骄兵必败 2024-10-07 13:03:04

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

With 3.0.5 I used a similar class to what serg posted, with the only modification being to use the updated classes which spring did not use (tail through VelocityToolboxView -> ServletToolboxManager (used in the createVelocityContext we have overridden) That is the class which is deprecated, so I modified the initVelocityToolContext in serg's answer to be:

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

I also had to change the line which created the VelocityContext to call this method obviously.

My bean now looks like:

<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"
/>
氛圍 2024-10-07 13:03:04

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

Inspired by answers from Scott and serg, here's another way to do it that does not require 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;
    }
}
雪化雨蝶 2024-10-07 13:03:04

受到上面所有答案的启发,这是我对 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;
    }
}

Inspired by all the answers above, this is my implementation of VelocityLayoutView for spring and velocity-tools 2.0, added some improvement!

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;
    }
}
画离情绘悲伤 2024-10-07 13:03:04

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

I found that this variation on @serg's technique worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文