I’m currently developing ten tonne of portlets using the Spring Portlet MVC framework. It’s been a frustrating but rewarding experience. I’ve been struggling with general documentation and examples for a while, and I wanted to make a quick post about a problem I’ve been having. Mainly because I found lots of people with similar issues, but no help at a resolution.
I’ve been using the AbstractWizardFormController, and over-riding the
-
-
public ModelAndView showForm(RenderRequest request, RenderResponse response, BindException errors) throws Exception
-
method to display my view and the
protected Object formBackingObject(PortletRequest request) throws Exception
method to populate my command bean.
My XML looked like this;
-
-
<bean id="menuSpotlightController" class="uk.co.smileham.cms.controller.edit.SpotlightController">
-
<property name="viewName" value="spotlightMenu" />
-
<property name="commandName" value="preferenceCommand" />
-
<property name="commandClass" value="uk.co.smileham.cms.model.PreferenceCommand" />
-
</bean>
-
This all seemed fine to me, however whenever I then tried to use the porlet I’d have a huge exception that went something along the lines of;
-
-
2009-10-21 14:04:28,765 ERROR [http-8080-Processor19] org.springframework.web.servlet.tags.form.SelectTag – Neither BindingResult nor plain target object for bean name ‘preferenceCommand’ available as request attribute
-
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘preferenceCommand’ available as request attribute
-
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
-
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:172)
-
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:192)
-
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:158)
-
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:121)
-
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:379)
-
at org.springframework.web.servlet.tags.form.SelectTag.writeTagContent(SelectTag.java:198)
-
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:90)
-
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
-
at org.apache.jsp.WEB_002dINF.jsp.spotlightMenu_jsp._jspService(spotlightMenu_jsp.java:658)
-
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
-
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
-
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
-
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
-
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
-
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
-
After much frustration I discovered that the problem was that the “preferenceCommand” was being set in the model, but then being overridden by a new model in my showForm() method.
The solution was to read the documentation (funny that) and eventually found that when using the AbstractFormController you should not directly create a new ModelAndView object to return but instead get the current model from the “BindException errors” variable that has been passed in and then “return super.showForm(request, errors, viewName);”
So my final showForm method went from;
-
-
public ModelAndView showForm(RenderRequest request, RenderResponse response, BindException errors) throws Exception {
-
ModelAndView mav = new ModelAndView(viewName);
-
return mav;
-
}
-
to
-
-
public ModelAndView showForm(RenderRequest request, RenderResponse response, BindException errors) throws Exception {
-
Map<String,Object> model = errors.getModel();
-
return super.showForm(request, errors, viewName);
-
}
-
That may all seem like giberish, but hopefully it’ll mean that someone else seeing the same problem might figure out what’s going on sooner! (That and I shouldn’t fall into this problem again any time soon now.)

















Phew, glad I don’t have to use Java. Good job on fixing it.
Comment by Steve Johnstone — October 21, 2009 @ 3:38 pm