스프링 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();
사용할 수없는 개체 속도 도구. VelocityToolboxView 클래스의 컨텍스트에 추가됩니다. 이렇게하면 작동하지 않습니다. – rdm