Multiple session or multi-window problem and solution where multiple class uses a same form extended from SimpleFormController of Spring MVC

Problem:
Lets think of a scenario, we are running a hospital management system. We need to search for patient’s record, doctor’s record, medicine stock record and many other things. Where searching criteria are completely different to each other. In order to implement the OO pattern, we develop a common search class and different command class for different type of search. Following figure illustrates the scenario:

If have created separate bean for Medicine, Doctor and Patient with Search class and define the separate command class for medicine, doctor and Patient. Works fine if you run all these separately. But Open a search page for Doctor then open a search page for Medicine in new window or tab and now press search button for doctor. What happens? You got the search result for Medicine though you suppose to get search result for Doctor.

Observation:
This is one of the most interesting problem of session related task in Spring MVC. Lets go into tail. SimpleFormController of Spring MVC extended from AbstractFromController of spring MVC this contains a method called getFormSessionAttributeName(). The spring framework source code 2.5 returns a string from this method in following way.

protected String getFormSessionAttributeName() {
return getClass().getName() + ".FORM." + getCommandName();
}

This is called 2 times, one while showing the form and one while submitting the form. If your class name is GeneralSearch and package name is net.project.web.search then this method will return the attribute name as net.project.web.search.GeneralSearch.FORM.command. The form kept in session by this name for both searching by different command class of Doctor and Medicine because you have set setSessionForm=true by. Now how do you expect to distinguish both form while submitting it in onSubmit() method? It always replace the old command class by the newest command class thats why in method onSubmit(), out preferred command object is not retrieved accurately.

Solution:
All that we need to do in this case is to make sure the form session attribute name is different for two different command class in our whole search system. You can pass a hidden value
into into your .jsp or .ftl and add it with form session attribute name to make a different session name for each search. You can override the method getFormSessionAttributeName and add the value with the attribute name that is different for each form like following

protected String getFormSessionAttributeName( HttpServletRequest request) {
String sessionId = ServletRequestUtils.getStringParameter( request, "sessionId", null);
if (sessionId!=null) {
return (super.getFormSessionAttributeName(request) + "." +sessionId);
}
return super.getFormSessionAttributeName();
}

You can pass a specific session id as the hidden variable and retrieve it by httpServletRequest and add it with the session attribute name while showing and submitting the form. That will help you to enable to work with multiple session and multi window.

By: Md. Shahjalal

15 Responses to Multiple session or multi-window problem and solution where multiple class uses a same form extended from SimpleFormController of Spring MVC

  1. Bjorn says:

    Interesting article, but I have a problem. My flow is page containing links of usernames => click on a username => Controller which implements the Controller interface, adding the User object to ModelAndView => show page containing form => submit to SimpleFormController.

    As you can see, before the form is shown, there ‘s a regular controller. Is it possible to manipulate the name of the form, without using the getFormSessionAttributeName method? This way, I can retrieve the name of the form, when the form is submitted.

  2. Bjorn says:

    I have the same issue in my current project. The problem is that our flow is as follows:

    Page containing links => Controller which implements the Controller interface => Page containing the form => FormController which implements SimpleFormController.

    The problem is that in the Controller, there is no way to add the session id. If you just add that method to the FormController, Spring doesn’t find that form name in the session, so the object is null. Is there a way to get around this?

  3. First of all there is no way to work without getFormSessionAttributeName() method in SimpleFormController of Spring MVC. Coz it saves the bean into a session. For keeping n e thing in session u must use a name. by default the name is command as i have mentioned in my post.

    U can not simply add the getFormSessionAttributeName() method in a single controller. There must be an abstract controller where u will add this method by with a default session id. all the class extends this will get the default form id. If u need specific one, simply override the method and make a new name. thats it. it will not get null again.

  4. Dinesh Keshari says:

    How to solve multiple session or multiple windows problem through @SessionAttributes in spring 2.5

    Thanking you.

  5. SessionAttributes in spring a annotation for the uses of a specific handler. Can be used a any other annotations. Better to check out SUN JAVA site for the tutorial.

  6. This is very hot information. I’ll share it on Twitter.
    p.s. Year One is already on the Internet and you can watch it for free.

  7. maheshadikte says:

    hi SJ,

    i was asked this Q in an iview n i was dumb…

    a page which will present the user with a list of countries . Once the user selects a country he will either see a text box or another dropdown select list. If we have an entry of all the states present in that country, we show him a select. If we don’t have the list of states for a country we show him a box to enter the name of the state into.

    i cudnot find an implen of this in spring ,i jus know we can use isformchangerequest and onformchange…i wish u help me on this with some code samples

    tks n rgds
    mahes

  8. You can do it in two ways. Easy one could be done through a simple java script function that checks state list for the country, if not found will show a textbox.

    Or you can do it by form submission. Once the user select the country list submit the form then redirect to showForm(). then put the relevant state list. Now in view layer if state list is not present just display the text box.

  9. Maxx79 says:

    Returning to unexpected authority, I think I see how this is so. ,

  10. 7 osi layers says:

    7 osi layers…

    […]Multiple session or multi-window problem and solution where multiple class uses a same form extended from SimpleFormController of Spring MVC « Programming and Algorithm Tutorial[…]…

  11. webpage says:

    I enjoy, result in I discovered exactly what I used to be looking for.
    You have ended my four day long hunt! God Bless you man.

    Have a great day. Bye

  12. I absolutely love your site.. Pleasant colors & theme.
    Did you create this amazing site yourself? Please
    reply back as I’m trying to create my very own site and would love to learn where you got this from or just what the theme is called. Kudos!

  13. From our website you can download and employ Clash of Clans Community
    Forums, you’ll find the usual free-to-play trappings here, such as lengthy timers for
    completing tasks and premium currency to skip those clash of clans free gems waits.

    Except the setting and artwork, there’s nothing
    unique or different from modern day RTS game’s mechanics. You will be in control of a small village where all the base-building and resource-collection takes
    place. The first time to go into the game itself,
    maybe even work with members of the clan now cheat tool.

  14. Ty says:

    Waitt from ɑ few seconds until generaating
    boom beach generator process іs done! All variations of Boom Beach Hacck is
    a boom beach generstor strategy game ԝҺere you fight ɑn epic war аgainst thе evil Blackguard.
    Select yoyr device Android οr iOS devices.
    Аs a result, tɦе game. Сomputer games аre now able
    to operate this webb site and supply you fresh cheat tools.

Leave a comment