PDA

View Full Version : Question about session form and backing object persistence


dkane
Apr 2nd, 2008, 11:46 AM
Dear colleagues,

This is my first Spring MVC app, and the purpose is to make form backed by session-scoped object. That means having last submitted state of object fields displayed in form fields every time when user opens this form , with no depend on previous navigation path.

I found that "sessionForm" property works only when the same form is re-shown after submission of itself, e.g. in case of errors. When submission leads to other view, backing object is being removed from session. If I open form in question again, fields are empty.

The solution I am using is keeping object in session as Spring bean and retrieving it "manually" from session in Controller's formBackingObject method.

The only thing I'd like to know : was such approach expected by Spring developers, or some "configurable" way to set up backing session bean still exist ?

Thank you !

Marten Deinum
Apr 2nd, 2008, 02:31 PM
That is intended behavior. If you want the object available for multiple screens (i.e. some wizard like functionality) you might want to checkout Spring Web Flow and/or the AbstractWizardFormController.

lumpynose
Apr 2nd, 2008, 02:49 PM
Martin, what about the scoped proxy bean?

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<context:component-scan base-package="myapp" />

<context:annotation-config />

<beans:bean id="merchant"
class="myapp.web.domain.Merchant"
scope="session">
<aop:scoped-proxy proxy-target-class="false" />
</beans:bean>
</beans:beans>

Then in my controller I just have:
@Autowired
IMerchant merchant;

Marten Deinum
Apr 3rd, 2008, 01:09 AM
Session scoped beans are something else then a formBackingObject in the session.

YOu could do it, but your formBackingObject reference will still be removed from the session. (Although the scoped bean is still there the accessible formBackingObject is not)

lumpynose
Apr 3rd, 2008, 12:52 PM
True. I'm actually thinking of the world I'm in, using Spring @MVC;

@RequestMapping(method = RequestMethod.GET)
public String getStub(final Model model) {
log.debug("called");

model.addAttribute("merchant", merchant);

return startViewName;
}

Which makes it so much easier.