Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bendilley
Created January 16, 2013 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendilley/4549746 to your computer and use it in GitHub Desktop.
Save bendilley/4549746 to your computer and use it in GitHub Desktop.
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

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