Calling Controller from Tiles Template using Spring 3
I just set up Apache Tiles 2 in my Spring MVC 3 application.
I created my template :
<definition name="baseLayout" template="/WEB-INF/jsp/baseLayout.jsp">
<put-attribute name="title" value="Template"/>
<put-attribute name="header" value="/WEB-INF/jsp/header.jsp"/>
<put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp"/>
<put-attribute name="body" value="/WEB-INF/jsp/body.jsp"/>
<put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp"/>
</definition>
and a Welcome page :
<definition name="welcome" extends="baseLayout">
<put-attribute name="title" value="Welcome"/>
<put-attribute name="body" value="/WEB-INF/jsp/home.jsp"/>
</definition>
And the properties for tile :
welcome.(class)=org.springframework.web.servlet.view.tiles2.TilesView
welcome.url=welcome
And my controller :
@RequestMapping(value="/welcome", method=RequestMethod.GET)
public ModelAndView home() {
logger.info("Welcome home yesssssss!");
ModelAndView mav = new ModelAndView();
logger.info("Loading Companies");
mav.addObject("companys", companyService.loadCompanys());
mav.setViewName("welcome");
return mav;
}
It works fine.
But how do i call another controller from within the template开发者_运维知识库 file like say menu.jsp that will call a controller like this one :
@RequestMapping(value="/menu", method=RequestMethod.GET)
public ModelAndView menu() {
logger.info("get MEnu");
ModelAndView mav = new ModelAndView();
logger.info("Loading menu");
mav.addObject("menu", menu.get());
mav.setViewName("menu");
return mav;
}
Any idea?
thanks
In order to call the 'menu' controller, you need to specify the 'menu' in your properties file together with a corresponding url, which has to be mapped to the controller (just like you did for the welcome).
May I suggest to use .htm for views or .do for forms etc.? Without, it's easy to get confused if you're trying to show a view, or you're trying to map a Request etc.
精彩评论