如何在Spring中存储数据是会话范围 [英] How to store data is session scope in Spring

查看:48
本文介绍了如何在Spring中存储数据是会话范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的控制器中创建了两个像这样 @SessionAttributes({"userObj","simpleValue"}) 会话范围内的对象.

I have created two object in session scope like this @SessionAttributes({"userObj","simpleValue"}) in my controller.

我在控制器中的这些变量中添加了一个用户对象和字符串,如下所示:

I am adding a user object and String at these variables in my controller like this:

modelAndView.addObject("userObj", user);
modelAndView.addObject("simpleValue", "Hello World");

User 类是一个简单的类,具有 2 个属性 id &名称.

User class is a simple class with 2 properties id & name.

假设我在一个名为 Controller1 的控制器中创建了这个,它显示了 Page1.我可以在 page1 中看到我的会话变量的数据.

Lets say I created this in a controller called Controller1 which shows Page1. I can see the data of my session variables in page1.

现在我创建了另一个控制器说 Controller2(这个控制器与 page1Controller1 没有关系)它显示 page2,现在在这个新页面中,我只能访问 simpleValue 的单个会话属性,我无法访问 userObj,结果为空.

Now I created another controller say Controller2 (this controller has no relation with page1 or Controller1) which displays page2, now in this new page I am able to access only the single session attribute for simpleValue, I am not able to access userObj, I am getting empty result.

根据 @SessionAttributes,它说:

注意:使用此注释指示的会话属性对应到特定处理程序的模型属性,透明地存储在会话会话中.一旦这些属性将被删除处理程序指示其会话会话的完成.所以,将此功能用于此类对话属性应该在会话期间临时存储在会话中特定处理程序的对话.

NOTE: Session attributes as indicated using this annotation correspond to a specific handler's model attributes, getting transparently stored in a conversational session. Those attributes will be removed once the handler indicates completion of its conversational session. Therefore, use this facility for such conversational attributes which are supposed to be stored in the session temporarily during the course of a specific handler's conversation.

所以我在这里有两个问题:

So I have 2 questions here:

1) 我不明白为什么 Spring 允许我访问 page2 中的简单属性而不是 User 对象.

1) I am not able to understand why Spring allowing me to access the simple property but not the User object in page2.

2) 该文档还说我们必须使用传统的 sessionWebRequest.我可以使用 sessison 并访问变量,但是有人可以帮助我如何使用 WebRequest 在会话中存储对象吗?

2) The document also says we have to use either traditional session or WebRequest. I able to use the sessison and access the variables, but can someone please help me how to use WebRequest for storing objects in session?

这是我正在使用的代码:

This is the code that I am using:

Controller1.java

@Controller
@SessionAttributes({"mySessionAttr","userObj"})
public class Controller1 {

    @RequestMapping(value="/page1")
    public ModelAndView singleFieldPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("mySessionAttr", "Hello World");
        modelAndView.addObject("userObj", new User(1,"Scott"));
        modelAndView.setViewName("page1");
        return modelAndView;
    }
}

Controller2.java

@Controller
public class Controller2 {

    @RequestMapping(value="/page2")
    public ModelAndView singleFieldPage(HttpSession session) {
        return new ModelAndView("page2");
    }   
}

page1.jsp &page2.jsp,两者代码相同.

<p>Session variable : ${simpleValue}</p>
<p>Product's name is ${userObj.name}. The id is ${userObj.id}

这是我的User.java:

公共类用户{

private int id;
private String name;

public User() {
}

public User(int id, String name) {
    this.id = id;
    this.name = name;
}

   // Setters & Getters
}

这是我的配置文件:

基于 Spring 的配置

@Configuration
@ComponentScan("com.examples")
@EnableWebMvc
public class WebAppConfig {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

}

基于网络的配置

public class Initializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(WebAppConfig.class);

        ctx.setServletContext(servletContext);  

        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);

    }

}

推荐答案

在我看来,用户对象的问题在于类 User 没有实现 Serializable.

From my point of view the problem with the user object lies on the fact that the class User does not implementing Serializable.

关于 WebRequest,它基本上是 NativeWebRequestAsyncWebRequest 的抽象,正如您在 文档:

And regarding the WebRequest, it is basically an abstraction for NativeWebRequest and AsyncWebRequest, as you can see at the documentation:

Web 请求的通用接口.主要用于通用网络请求拦截器,让他们可以访问通用请求元数据,不用于实际处理请求.

Generic interface for a web request. Mainly intended for generic web request interceptors, giving them access to general request metadata, not for actual handling of the request.

采用这种方法,WebRequestInterceptor 可用于 Servlet 和 Portlet 请求,如 文档:

一般网页请求拦截的接口.允许被应用于 Servlet 请求以及 Portlet 请求环境,通过基于 WebRequest 抽象构建.

Interface for general web request interception. Allows for being applied to Servlet request as well as Portlet request environments, by building on the WebRequest abstraction.

这篇关于如何在Spring中存储数据是会话范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆