如何在Spring 3中使用VelocityEngine和Velocity Tools

时间:2013-01-09 14:50:31

标签: java spring velocity

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

Spring Config:

<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();   

2 个答案:

答案 0 :(得分:5)

当你不使用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类中自动装配此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);
    }   
}

答案 1 :(得分: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中的所有工具。