-1

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • Just put it in the session like you normally would. You shouldn't abuse `@SessionAttributes` for that. The intend for `@SessionAttributes` is to store an object (or multiple) in between requests for a **single** controller not to store it for the lifetime of the session. – M. Deinum May 02 '16 at 10:28

2 Answers2

1

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

0

From the documentation:

This will typically list the names of model attributes which should be transparently stored in the session or some conversational storage, serving as form-backing beans. Declared at the type level, applying to the model attributes that the annotated handler class operates on.

(emphasis is mine)

Also note that, as indicated in the documentation, you should not use that for "non temporary" elements.