PDA

View Full Version : Tiles and Spring config


jjhancock
Apr 23rd, 2007, 07:21 PM
Hey everybody,

I've been working on a spring + hibernate app recently and have gotten to the point where I need to start adding pages. This isn't a problem except I've been using Tiles for a few years, and since I'm comfortable with Tiles I'd like to continue using them. I've been searching this forum as well as the rest of the web, and I can't seem to get my app configured correctly where I can return a ModelAndView from a class extending SimpleFormController where the view is a Tiles layout. Here are some code snippets:

app-servlet.xml

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles.TilesCo nfigurer">
<property name="factoryClass" value="org.apache.struts.tiles.xmlDefinition.I18nFactoryS et" />
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs.xml</value>
</list>
</property>
</bean>

<bean id="internalViewResolver" class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<property name="order" value="0" />
<property name="requestContextAttribute" value="requestContext" />
<property name="viewClass"><value>org.springframework.web.servlet.view.tiles.TilesJs tlView</value></property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundl eViewResolver">
<property name="basename" value="views"/>
</bean>

<bean id="viewController" class="org.springframework.web.servlet.mvc.UrlFilenameVie wController"/>



tiles-defs.xml

<tiles-definitions>

<!-- DEFAULT MAIN TEMPLATE -->
<definition name="layout" page="/WEB-INF/jsp/tiles/main.jsp">
<put name="header" value="" />
<put name="body" value="" />
<put name="footer" value="" />
</definition>

<definition name="login" extends="layout" >
<put name="header" value="/WEB-INF/jsp/tiles/header.jsp" />
<put name="body" value="/WEB-INF/jsp/login.jsp" />
<put name="footer" value="/WEB-INF/jsp/tiles/footer.jsp" />
</definition>

<definition name="homePage" extends="layout" >
<put name="header" value="/WEB-INF/jsp/tiles/header.jsp" />
<put name="body" value="/WEB-INF/index.jsp" />
<put name="footer" value="/WEB-INF/jsp/tiles/footer.jsp" />
</definition>
</tiles-definitions>



Controller class



public class LoginUserFormController extends BaseFormController {

protected static final String CLASS_NAME = LoginUserFormController.class
.getName();

protected static final Log LOG = LogFactory.getLog(CLASS_NAME);

public LoginUserFormController() {
super();

}

public ModelAndView onSubmit(Object command) throws ServletException {
ModelAndView mav = null;
mav = new ModelAndView(new RedirectView("homePage.url"));

return mav;

}
}


views.properties

login.class=com.magicsearch.mojo.web.UserLoginForm Controller
login.url=/WEB-INF/jsp/login.jsp

homePage.class=com.magicsearch.mojo.web.AddVehicle FormController
homePage.url=homePage


The app starts with no errors. I can navigate to my index.jsp page which is just a few links. I can click the login link that takes me to a tiles defined page and it works as it should. When I submit to the above controller class, the validator is called and works correctly, as I am returned to the login screen. However, upon passing validation, I always get a 404 error that the page cannot be found, as the Tiles definition isn't being found I don't think in the controller class when I return the new ModelAndView().

Thanks in advance for any help. I've been working on this for many hours and figured it was time to post :)

Joris Kuipers
Apr 24th, 2007, 04:27 AM
public ModelAndView onSubmit(Object command) throws ServletException {
return new ModelAndView(
new RedirectView("homePage.url"));
}
However, upon passing validation, I always get a 404 error that the page cannot be found, as the Tiles definition isn't being found I don't think in the controller class when I return the new ModelAndView().
Hi,

The constructor of the RedirectView requires a URL to redirect to, not a logical view name that maps to a Tiles definition. That's the whole idea of redirecting: you're telling the browser where to go, so you're using a URL. Either you redirect and use a URL (which is quite common after a POST), or you do not redirect and use a view name. The URL can be relative, of course.

jjhancock
Apr 24th, 2007, 11:05 AM
I actually figured out the problem after I posted last night, but before I read your response this morning. Here's what I did:


TilesView tilesView = new TilesView();
tilesView.setUrl("homePage");

return new ModelAndView(tilesView);


This seems to work like a champ so far.