如何将对象的实例作为会话属性放入 Spring MVC 项目中?

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

我正在开发 Spring MVC 应用程序,但遇到以下问题。

我有此 RegistrazioneInfo class,其中包含用户插入到表单中的一些信息:

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;
    }


}

然后我有这个控制器class:

@Controller
public class RegistrazioneController extends BaseController {

    private RegistrazioneInfo registrazioneInfo;

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

包含一些处理对某些资源的请求的方法。

好的,我的问题是我想通过使用 @ SessionAttributes Spring 注释如下所示:http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-sessionattrib

我的问题是,在前面的例子中做这样的事情:

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

那么宠物到底是什么?我认为它类似于标识必须用作会话属性或类似内容的对象的 id。我怎么能说把我的 RegistrazioneInfo 的一个实例作为会话属性?

来自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.

(重点是我的)

另请注意,如文档中所述,您不应将其用于 "non temporary" 元素。

@SessionAttributes 在控制器 Class (@Controller) 中声明,因此在 class 级别。 Pet 是一个持久存在于 HttpSession

中的 Bean 对象