Intro
In my previous post I was playing with Velocity and the Spring framework. In this post I will show how to build a Velocity layout. Then I will inform Spring to use our layout when rendering a view.
Let’s build the layout
<html> <body> <div> #parse ("head.vm") </div> <div> $screen_content </div> <div> #parse ("foot.vm") </div> </body> </html>
The $screen_content variable is used by the VelocityLayoutViewResolver to include the result of an other VM in the layout. Let’s name this template.vm. Take a look at the content of the head.vm and foot.vm files.
I'm the header.
I'm the footer.
What are we showing?
I’ll show what I know about the X-Men and the Avenger. So I need a VM that will show some rosters from the X-Men and a VM that will show some rosters from the Avenger. Take a look at the content of the xmen.vm and the anvenger.vm files.
#foreach ($xmen in $xmens) $xmen<br /> #end
#foreach ($avenger in $avengers) $avenger<br /> #end
We need some controllers too
The XmenController provides some names for the X-Men’s crew and the AvengerController provides some names for the Avenger’s crew.
/** * Knows a little about the X-Men. * * @author Sébastien Ayotte * */ final class XmenController extends ParameterizableViewController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { List<String> xmens = new ArrayList<String>(); xmens.add("Professor X"); xmens.add("Cyclops"); xmens.add("Iceman"); xmens.add("Archangel"); xmens.add("Beast"); xmens.add("Phoenix"); Map<String, List<String>> model = new HashMap<String, List<String>>(); model.put("xmens", xmens); return new ModelAndView(getViewName(), model); } }
/** * Knows a little about the Avenger. * * @author Sébastien Ayotte * */ final class AvengerController extends ParameterizableViewController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { List<String> avengers = new ArrayList<String>(); avengers.add("Thor"); avengers.add("Iron Man"); avengers.add("Henry Pym"); avengers.add("Wasp"); avengers.add("Hulk"); Map<String, List<String>> model = new HashMap<String, List<String>>(); model.put("avengers", avengers); return new ModelAndView(getViewName(), model); } }
We need to configure the Spring context
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/WEB-INF/mods/core/sample/velocity/" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityLayoutView" /> <property name="layoutUrl" value="template.vm" /> <property name="cache" value="true" /> <property name="prefix" value="" /> <property name="suffix" value=".vm" /> </bean> <bean name="/core/sample/velocity/showxmens.do" class="sample.velocity.XmenController"> <property name="viewName" value="xmen" /> </bean> <bean name="/core/sample/velocity/showavengers.do" class="sample.velocity.AvengerController"> <property name="viewName" value="avenger" /> </bean> </beans>
Try it!
I’m using Tomcat to deploy my application. I can now call http://localhost:8080/sample/core/sample/velocity/showxmens.do to have a list of the X-Men’s crew or I can call http://localhost:8080/sample/core/sample/velocity/showavengers.do to get a list of the Avenger’s crew. The two lists will be shown in the layout we define previously.