java - SessionAttributes cause problems with multiple tabs -
i have spring web app (spring 3.2) , have used following scenario handle edit pages:
@controller @sessionattributes(value = { "packet" }) public class packetcontroller { @requestmapping(value = "/edit-packet/{packet_id}", method = requestmethod.get) public string editpacketform(@pathvariable(value = "packet_id") long packet_id, model model) { model.addattribute("packet", packetservice.findbyid(packet_id)); return "packets/packetedit"; }
post method:
@requestmapping(value = "/edit-packet/{packet_id}", method = requestmethod.post) public string packeteditaction(model model, @valid @modelattribute(value = "packet") packet packet, bindingresult result, sessionstatus status) { if (result.haserrors()) { return "packets/packetedit"; } packetservice.update(packet); status.setcomplete(); return "redirect:/"; }
now problem if tries open multiple tabs /edit-packet/{id} different ids. every new open tab session 'packet' object overwritten. after trying submit forms on multiple tabs, first tab submitted change second packet because in session second object , second tab cause error because setcomplete has been invoked there no 'packet' object in session.
(this known issue https://jira.spring.io/browse/spr-4160).
i trying implement solution http://duckranger.com/2012/11/add-conversation-support-to-spring-mvc/ solve problem. copied conversationalsessionattributestore.java conversationidrequestprocessor.java classes , in servlet-config.xml made this:
<mvc:annotation-driven /> <bean id="conversationalsessionattributestore" class="com.xx.session.conversationalsessionattributestore"> </bean> <bean name="requestdatavalueprocessor" class="com.xx.session.conversationidrequestprocessor" />
but doesnt work, in post methods dont see new parameters, miss something?
update: actually, started working, maybe has better idea solve issue?
my other idea force new session on every new tab not nice solution.
don't use session attributes, make controller stateless , use path variable retrieve correct model attribute.
@controller public class packetcontroller { @modelattribute public packet packet(@pathvariable(value = "packet_id") long packet_id) { return packetservice.findbyid(packet_id); } @requestmapping(value = "/edit-packet/{packet_id}", method = requestmethod.get) public string editpacketform() { return "packets/packetedit"; } @requestmapping(value = "/edit-packet/{packet_id}", method = requestmethod.post) public string packeteditaction(model model, @valid @modelattribute(value = "packet") packet packet, bindingresult result) { if (result.haserrors()) { return "packets/packetedit"; } packetservice.update(packet); return "redirect:/"; } }
something should trick.
Comments
Post a Comment