Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spring View which supports the Velocity Tools 2.0 sub-project. Configured using the Spring Bean xml snippet below.
public class VelocityTools2View extends VelocityLayoutView {
private ViewToolManager toolManager;
@Override
protected Context createVelocityContext (Map<String, Object> model,
HttpServletRequest request,
HttpServletResponse response) {
ToolContext toolContext = toolManager.createContext(request, response);
VelocityContext context = new VelocityContext(toolContext);
if (model != null) context.putAll(model.entrySet());
return context;
}
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
XmlFactoryConfiguration config = new XmlFactoryConfiguration();
config.read(getServletContext().getResourceAsStream(getToolboxConfigLocation()));
boolean autoConfigure = false;
boolean includeDefaults = false;
toolManager = new ViewToolManager(getServletContext(), autoConfigure, includeDefaults);
toolManager.configure(config);
toolManager.setVelocityEngine(getVelocityEngine());
}
}
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
...
<property name="viewClass" value="com.eo3.lib.spring.VelocityTools2View"/>
<property name="toolboxConfigLocation" value="/WEB-INF/velocity-tools.xml"/>
...
</bean>
@bendilley
Copy link
Author

bendilley commented Jan 16, 2013

Note that

  • I'm extending the VelocityLayoutView but could equally extend VelocityToolboxView
  • context.putAll(model.entrySet()) was enabled by extending VelocityContext with my own version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment