问题
I am new to both spring and velocity
I want to configure velocity tools with spring 3.x
but I could not find a good article on that
+ the velocity template is not showing the session variable
I set from Spring controller
回答1:
As for configuring Velocity tools in spring 3.x, first put your tools.xml file somewhere useful, eg: /WEB-INF/vm/toolbox.xml THEN configure your view resovler to look in that place:
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
...
<!--Other config properties in here -->
...
<property name="toolboxConfigLocation" value="/WEB-INF/vm/toolbox.xml" />
</bean>
An example toolbox.xml file:
<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>
</toolbox>
Hope this bit helps, at least.
回答2:
It sounds like you might have part of this working, but you haven't given much detail so I don't know how far you've got. So I'll give you a general answer: first learn the basics of Spring MVC and then use the Spring documentation to understand the Velocity integration. Once you have a working web application using Spring MVC with JSPs, it's a pretty small step to use Velocity templates instead of JSPs.
So first run through a good tutorial about Spring MVC, such as this one: http://blog.springsource.com/2011/01/04/green-beans-getting-started-with-spring-mvc/
Then when you've got that working, read the (fairly short) Spring documentation about Velocity at: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/view.html#view-velocity
Essentially you'll need to use the VelocityViewResolver
documented there instead of the InternalResourceViewResolver
from the tutorial. Then create .vm Velocity templates in place of the .jsp files to display your pages.
来源:https://stackoverflow.com/questions/10470596/configure-velocity-tools-with-spring-3-x