Search code examples
springspring-mvcspring-annotations

How can I put an instance of an object as session attribute in a Spring MVC project?


I am working on a Spring MVC application and I have the following problem.

I have this RegistrazioneInfo class that contains some information inserted into a form by the user:

public class RegistrazioneInfo {

    @NotNull
    @Size(min=16, max=16)
    private String codiceFiscale;

    String gRecaptchaResponse;

    public String getCodiceFiscale() {
        return codiceFiscale;
    }

    public void setCodiceFiscale(String codiceFiscale) {
        this.codiceFiscale = codiceFiscale;
    }

    public String getgRecaptchaResponse() {
        return gRecaptchaResponse;
    }

    public void setgRecaptchaResponse(String gRecaptchaResponse) {
        this.gRecaptchaResponse = gRecaptchaResponse;
    }


}

Then I have this controller class:

@Controller
public class RegistrazioneController extends BaseController {

    private RegistrazioneInfo registrazioneInfo;

    ...............................................
    ...............................................
    ...............................................
}

that contains some methods handling request towards some resources.

Ok, my problem is that I want to use an instance of the previous RegistrazioneInfo class as session attribute by the use of the @SessionAttributes Spring annotation as shown here: http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-sessionattrib

My problem is, in the previous example do something like this:

@SessionAttributes("pet")
public class EditPetForm {
    // ...
}

So what exactly is pet? I think that it is something like an id that identify the object that have to be used as a session attribute or something like this. How can I say to put an instance of my RegistrazioneInfo as session attribute?


Solution

  • @SessionAttributes is declared in a Controller Class (@Controller), so on the class level. Pet is an Bean Object that persist in HttpSession