Sunday, October 01, 2006

Spring Error - ModelAndView [ModelAndView: materialized View is [null]

I've been struggling with this stupid error for an hour now. Well, don't they all look stupid after they are resolved! If you searched on this exception, read on, you might just find the solution here :).
I kept getting the following exception in my controller and I just couldn't figure out what I had done wrong.

javax.servlet.ServletException: ModelAndView [ModelAndView: materialized View is [null]; model is {org.springframework.validation.BindException.recipe=org.springframework.validation.BindException: BindException: 0 errors, recipe=com.foo.bar.Recipe@cf710e}] neither contains a view name nor a View object in servlet with name 'action'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:919)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:705)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:346)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:445)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:356)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:226)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:627)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:149)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:123)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:141)
at org.mortbay.jetty.Server.handle(Server.java:269)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:430)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:687)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:492)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:199)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:339)
at org.mortbay.jetty.nio.HttpChannelEndPoint.run(HttpChannelEndPoint.java:270)
at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:475)

After much googling, I found nothing. Finally I turned on the DEBUG logging for spring and tried to solve it the hard way - logically.
Long story short, here's why I was getting the exception:


I had a bean mapping as follows:

<bean id="barManager"
class="com.foo.ui.controller.BarManager"
autowire="byName">
<property name="commandName" value="bar" />
<property name="commandClass"
value="com.foo.dao.Bar" />
</bean>

I also had a URL mapping as follows:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/barlink.html">barManager</prop>
</props>
</property>
</bean>
I kept getting the above exception when I tried to access http://localhost:8080/myapp/barlink.html
The solution to the problem was adding the following line to the barManager definition:
<property name="formView" value="bars" />
Also, the value "bars" MUST map to an existing view name. In my case, it mapped to bars.jsp.

After adding this, the definition looks like this:
<bean id="barManager"
class="com.foo.ui.controller.BarManager"
autowire="byName">
<property name="commandName" value="bar" />
<property name="commandClass"
value="com.foo.dao.Bar" />
<property name="formView" value="bars" />
</bean>


That's it. It took me an hour and a half to figure out and about 15 min to write this. Hopefully this will save you some time :).

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?