java 如何在 Spring 3 中使用 Velocity Tools 获取 VelocityEngine

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14237862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:34:39  来源:igfitidea点击:

How get VelocityEngine with Velocity Tools in Spring 3

javaspringvelocity

提问by rdm

How get VelocityEngine with Velocity Tools in Spring 3? I need a method in the controller to process a template Velocity, but need to have Velocity Tools that are available to initialize the Spring 3. Now I'm doing it like this.

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

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>

In controller class:

在控制器类中:

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



}

In method:

在方法中:

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

回答by SRy

The above method to get velocity is good when you don't use Spring configuration.When you use Spring you don't need this much complexity.

当你不使用Spring配置时,上面获得速度的方法是好的。当你使用Spring时,你不需要这么复杂。

Define this bean in your spring.xml

在你的 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>

and In your java class Autowire this 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);
    }   
}

回答by Rags

There is a simpler way to do it in Spring 3

在 Spring 3 中有一种更简单的方法可以做到

add toolbox.xml to WEB-INF/velocity

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

Then add this path to your APPNAME-servlet.xml file

然后将此路径添加到您的 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>

Then update you pom.xml dependency

然后更新你的 pom.xml 依赖

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

save, update project, and run the server. You should be now able to use all tools stuff in vm's.

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