Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am confused about how to use @SessionAttributes

I am trying to understand architecture of Spring MVC. However, I am completely confused by behavior of @SessionAttributes.

Please look at SampleController below , it is handling post method by SuperForm class. In fact, just field of SuperForm class is only binding as I expected.

However, After I put @SessionAttributes in Controller, handling method is binding as SubAForm. Can anybody explain me what happened in this binding.

-------------------------------------------------------

@Controller
@SessionAttributes("form")
@RequestMapping(value = "/sample")
public class SampleController {

    @RequestMapping(method = RequestMethod.GET)
    public String getCreateForm(Model model) {
        model.addAttribute("form", new SubAForm());
        return "sample/input";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String register(@ModelAttribute("form") SuperForm form, Model model) {
        return "sample/input";
    }
}

-------------------------------------------------------

public class SuperForm {

    private Long superId;

    public Long getSuperId() {
        return superId;
    }

    public void setSuperId(Long superId) {
        this.superId = superId;
    }

}

-------------------------------------------------------

public class SubAForm extends SuperForm {

    private Long subAId;

    public Long getSubAId() {
        return subAId;
    }

    public void setSubAId(Long subAId) {
        this.subAId = subAId;
    }

}

-------------------------------------------------------

<form:form modelAttribute="form" method="post">
    <fieldset>
        <legend>SUPER FIELD</legend>
        <p>
            SUPER ID:<form:input path="superId" />
        </p>
    </fieldset>
    <fieldset>
        <legend>SUB A FIELD</legend>
        <p>
            SUB A ID:<form:input path="subAId" />
        </p>
    </fieldset>
    <p>
        <input type="submit" value="register" />
    </p>
</form:form>
like image 494
zono Avatar asked Feb 06 '11 15:02

zono


People also ask

What is @SessionAttributes?

@SessionAttributes annotation is used to store the model attribute in the session. This annotation is used at controller class level.

What is @SessionAttributes in Spring MVC?

@SessionAttribute annotation retrieve the existing attribute from the session. This annotation allows you to tell Spring which of your model attributes will also be copied to HttpSession before rendering the view.

What is HTTP session spring?

Spring Session) is being used to manage the HTTP Session state. The HTTP Request count is simply incremented every time a client HTTP Request is made to the HTTP server (e.g. Servlet Container) before the HTTP Session expires.


2 Answers

When processing POST request, Spring does the following:

  • Without @SessionAttributes: Spring instantiates a new instance of SuperForm (type is inferred from the signature of register()), populates its properties by values from the form fields and passes it to the register() method.

  • With @SessionAttributes: Spring obtains an instance of model attribute from the session (where it was placed when processing GET due to presence of @SessionAttributes), updates its properties by values from the from fields and passes it to the register() method.

That is, with @SessionAttributes , register() gets the same instance of the model attribute object that was placed into the Model by getCreateForm().

like image 105
axtavt Avatar answered Oct 06 '22 05:10

axtavt


Adding on to what @axtavt said: Suppose, in getCreateForm you are putting some values for a drop-down (list or map), or you are putting some values in form that you want in register method but you don't want them to show in form (not even in hidden fields). Now suppose that an error occurred in register method and you need to show the form again. To populate drop down values and other values that you would need in next post, you would have to repopulate them in form. The @SessionAttribute helps here as @axtavt very well described above.

like image 32
Ritesh Avatar answered Oct 06 '22 05:10

Ritesh