2

如何在 Spring 3 中使用 Velocity 工具获取 VelocityEngine?我需要控制器中的一个方法来处理模板 Velocity,但需要有可用于初始化 Spring 3 的 Velocity 工具。现在我正在这样做。

弹簧配置:

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>        
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>                
            </props>
        </property>                 
    </bean>

 <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="cache" value="false"/>
        <property name="prefix" value=""/>
        <property name="suffix" value=".html"/>        
        <property name="contentType" value="text/html; charset=UTF-8"/>     
        <property name="toolboxConfigLocation" value="/WEB-INF/velocity/config/toolbox.xml"/>
        <property name="viewClass" value="my.tools.VelocityToolsView"/> 
    </bean>

在控制器类中:

@Autowired
private VelocityConfigurer configurer;

private VelocityEngine velocityEngine;


private ToolContext toolContext;

@PostConstruct
public void init() {                    

        velocityEngine = configurer.getVelocityEngine();

        ToolManager toolManager = new ToolManager();
        toolManager.configure("fuulPath/WEB-INF/velocity/config/toolbox.xml");
        toolContext = toolManager.createContext();



}

在方法:

    VelocityContext velocityContext = new VelocityContext(map, toolContext);                
    StringWriter writer = new StringWriter();        
    velocityEngine.mergeTemplate("myTeplate.html", "UTF-8", velocityContext, writer);        
    String templateString = writer.toString();   
4

2 回答 2

6

当您不使用 Spring 配置时,上述获取速度的方法很好。当您使用 Spring 时,您不需要这么复杂。

在你的 spring.xml 中定义这个 bean

<bean id="velocityEngine"
        class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <value>
            resource.loader=class
            class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
        </value>
    </property>
</bean>

并在你的java类Autowire这个bean

@Component
public class Sample {

    private VelocityEngine velocityEngine;

    public VelocityEngine getVelocityEngine() {
        return velocityEngine;
    }

    @Autowired
    @Required
    public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }

    public String getSomething(Object variable) {
        Map model = new HashMap();
        model.put("variable",variable);

        return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/templates/sometemp.vm", model);
    }   
}
于 2013-01-09T17:48:10.187 回答
2

在 Spring 3 中有一种更简单的方法

将 toolbox.xml 添加到 WEB-INF/velocity

<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
<xhtml>true</xhtml>
<tool>
    <key>date</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.DateTool</class>
    <parameter name="format" value="dd/MM/yyyy" />
</tool>
<tool>
    <key>display</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.DisplayTool</class>
</tool>
<tool>
    <key>math</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
<tool>
    <key>iter</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.IteratorTool</class>
</tool>
<tool>
    <key>sort</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.SortTool</class>
</tool>
<tool>
  <key>esc</key>
  <scope>application</scope>
  <class>org.apache.velocity.tools.generic.EscapeTool</class>
</tool>
</toolbox>

然后将此路径添加到您的 APPNAME-servlet.xml 文件中

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver" p:cache="false" p:order="1">
  <property name="prefix" value="/com/aol/dragon/template/"/>
  <property name="suffix" value=".vm"/>
  <property name="toolboxConfigLocation" value="/WEB-INF/velocity/toolbox.xml" />
</bean>

然后更新你的 pom.xml 依赖

<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>

保存、更新项目并运行服务器。您现在应该能够使用 vm 中的所有工具。

于 2013-06-03T18:15:29.680 回答