2013-01-09 2 views
1

스프링 3에서 VelocityEngine과 VelocityEngine을 얻는 방법은 무엇입니까? Velocity 템플릿을 처리하는 컨트롤러에 메소드가 필요하지만 Spring 3을 초기화하는 데 사용할 수있는 Velocity Tools가 필요합니다. 이제 이와 같이합니다.스프링 3의 Velocity Tools로 VelocityEngine을 얻는 방법

봄 구성 : 컨트롤러 클래스에서

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

답변

5

당신이 봄을 사용하지 않는 경우 속도가 좋은 얻을 수있는 위의 방법 Spring을 사용할 때 이렇게 복잡하지 않아도됩니다.

봄 3

추가 그것을 할 수있는 간단한 방법이 있습니다 자동으로 묶어 당신의 spring.xml

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

에 당신의 자바 클래스에서이 빈

@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); 
    } 
} 
+0

사용할 수없는 개체 속도 도구. VelocityToolboxView 클래스의 컨텍스트에 추가됩니다. 이렇게하면 작동하지 않습니다. – rdm

2

을이 빈을 정의 toolbox.xml to 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> 

그런 다음

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

그런 다음 당신에게

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

저장의 pom.xml 종속, 업데이트 프로젝트를 업데이트하여 APPNAME-servlet.xml 파일에이 경로를 추가하고 서버를 실행합니다. 이제 VM에서 모든 도구를 사용할 수 있어야합니다.

관련 문제