backend/Spring

[Spring] 스프링에서 VelocityTools 환경설정

버리야 2008. 3. 20. 09:05
반응형
스프링에서 벨로시티 뷰 템플릿 엔진을 사용하고 있는데 여기에 편리한 tools를 사용하기 위해서
필요한 설정을 기억하기 위해 기록합니다.

VelocityTools는 벨로시티 템플릿에서 숫자나 날짜, url등의 포맷팅을 지원하는 툴이고,
VelocityTools프로젝트에는 GenericTools, VelocityView, VelocityStruts 세개의 부분으로 나눠져 있습니다.

보통 GenericTools를 많이 쓸일이 많기 때문에~ 조금 정리해보면,

Generic Tools에는,

DateTool
A tool for manipulating and formatting dates.
MathTool
A tool for performing floating point math.
NumberTool
A tool for formatting numbers.
RenderTool
A tool to evaluate and render arbitrary strings of VTL (Velocity Template Language).
EscapeTool
A tool to help with common escaping needs in Velocity templates.
ResourceTool
A tool to simplify access to ResourceBundles for internationalization or other dynamic content needs.
Alternator and AlternatorTool
Utility class for easily alternating over values in a list and tool for easy creation of Alternators in templates.
ValueParser
A tool to retrieve and parse String values pulled from a map. This provides the basis for the ParameterParser in VelocityView.
ListTool
A tool to help when working with arrays or Lists. This tool transparently handles both the same way.
SortTool
A tool that allows a user to sort a collection (or array, iterator, etc) on any arbitary set of properties exposed by the objects contained within the collection.
IteratorTool
A convenience tool to use with #foreach loops. It wraps a list to let the designer specify a condition to terminate the loop, and reuse the same list in different loops.



EscapeTool
- It provides methods to escape outputs for Java, JavaScript, HTML, HTTP, XML and SQL. Also provides methods to render VTL characters that otherwise needs escaping.
을 이용하여 테스트 해보면...

toolbox.xml 파일을 하나 만들어서
<?xml version="1.0"?>
<toolbox>
 <tool>
  <key>esc</key>
  <scope>application</scope>
  <class>org.apache.velocity.tools.generic.EscapeTool</class>
 </tool>
</toolbox>

genericTool의 EscapeTool을 사용하는데 velocity에서 esc라는 키워드를 이용해서 쓰겠다~


스프링에서 Properties파일을 따로 정의했다고 하면(따로 빼 놓았다면)
# velocityViewResolver
velocity.resourceLoaderPath=/WEB-INF/velocity/
velocity.toolboxConfigLocation=/WEB-INF/spring/toolbox.xml
velocity.properties=/WEB-INF/spring/velocity.properties
velocity.overrideLogging=false
velocity.cache=false
velocity.prefix=
velocity.suffix=.vm


스프링의 설정파일에서

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
 <property name="cache" value="${velocity.cache}" />
 <property name="prefix" value="${velocity.prefix}" />
 <property name="suffix" value="${velocity.suffix}" />
 <property name="toolboxConfigLocation" value="${velocity.toolboxConfigLocation}" />
 <property name="exposeSpringMacroHelpers" value="true" />
</bean>


.vm라는 확장자의 velocity파일에서
$esc.xml("< >")


결과는
< >



참고 :

[Velocity] 스프링 프레임워크에서 사용하기

http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/EscapeTool.html
http://forum.springframework.org/archive/index.php/t-10926.html




반응형