springMVC中的Httpsession管理 [英] Httpsession management in springMVC

查看:180
本文介绍了springMVC中的Httpsession管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring MVC的新手,并开始通过我学到的东西制作示例应用程序。我打算在spring MVC中实现Session管理。我发现这个很有帮助。



但我无法清楚地理解它。我们在会话中添加值,如

  HttpSession session = request.getSession(false); 
session.setAttribute(key,value);
session.setAttribute(key1,value1);

稍后我们会根据


$ b等密钥获取值$ b

  session.getAttrubute(key); 

但是在春季MVC中,我看不到任何相似之处,这让我完全糊涂了。

  @Controller 
@SessionAttributes(思考)
公共类SingleFieldController {

@RequestMapping(value =/ single-field)
public ModelAndView singleFieldPage(){
return new ModelAndView(single-field-page);
}

@RequestMapping(value =/ remember)
public ModelAndView rememberThought(@RequestParam String thoughtParam){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject(思想,thoughtParam);
modelAndView.setViewName(single-field-page);
返回modelAndView;
}

}

在上面的代码中 @SessionAttributes(思考)让我很困惑,就像这个想法一样,我也没有必要返回 ModelAndView 因为我使用 backbone.marionette.js



所以如何在会话中设置值并在需要时使用它们?我还必须将会话对象转换为我的用户定义对象,因为在将值返回到屏幕时,我只返回会话中可用的UserDefined对象列表。



<请你帮助我更好地理解它。也许我对使用jsp / servlet的方式感到困惑。



UPDATE



以下是我的控制器并提供了评论

  package com.hexgen.puppet; 

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.hexgen.puppet.CreatePuppet;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
公共类PuppetService {

@RequestMapping(method = RequestMethod.POST,value =/ create)
public @ResponseBody
void createOrder(@RequestBody CreatePuppet request){
//在session
中添加/更新值的逻辑}

@RequestMapping(method = RequestMethod.GET,value = / list)
public @ResponseBody
List< Puppet> getGroups(){
//逻辑从会话中检索对象并将其转换为List并将其发送回

返回木偶;
}


}

并转换对象如果需要继续

解决方案

@SessionAttributes 并不能完全取代传统的 HttpServlet 会话管理。如果两个或更多Controller方法需要传递某些数据,请使用它。但是,使用这个我们只能在单个控制器类中实现通信。如果您使用的是 @SessionAttributes ,则不会使用它来显式读取和写入会话。建议仅对短期通信使用 @SessionAttributes 。如果需要在会话中存储长期数据,建议显式使用 session.setAttribute session.getAttribute ,而不是 @SessionAttributes 。有关详细信息检查出来


I am new to spring MVC and started to make a sample application by doing what I learned. I am planning to implement Session management in spring MVC. I found this one helpful.

But I am not able to get it clearly. We add values to the session like

HttpSession session = request.getSession(false);
session.setAttribute("key", value);
session.setAttribute("key1",  value1);

and later on we fetch values based on the keys like

session.getAttrubute("key");

but in spring MVC, I could not see anything similar and it totally confuses me.

@Controller
@SessionAttributes("thought")
public class SingleFieldController {

    @RequestMapping(value="/single-field")
    public ModelAndView singleFieldPage() {
        return new ModelAndView("single-field-page");
    }

    @RequestMapping(value="/remember")  
    public ModelAndView rememberThought(@RequestParam String thoughtParam) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("thought", thoughtParam);
        modelAndView.setViewName("single-field-page");
        return modelAndView;
    }

}

In the above code @SessionAttributes("thought") is totally confusing me like what is this thought defined also I have no need to return a ModelAndView since I am using backbone.marionette.js

So how can I set values in a session and use them whenever required? I also have to convert the session object to my user defined object since, while returning the values to screen, I return only a List of UserDefined objects which are available in the session.

So please help me to understand it better. Maybe I am confused with the way I used jsp / servlet.

UPDATE

Below is the controller I have and provided comments

package com.hexgen.puppet;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.hexgen.puppet.CreatePuppet;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class PuppetService {

    @RequestMapping(method = RequestMethod.POST, value = "/create")
    public @ResponseBody
    void createOrder(@RequestBody CreatePuppet request) {
        //logic to add/update values in session
    }

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public @ResponseBody
    List<Puppet> getGroups() {
        //logic to retrive objects from session and convert it as List and send it back

        return puppets;
    }


}

and convert the object if needed and go on

解决方案

@SessionAttributes doesn't fully replaces the traditional HttpServlet session management. Use it if two or more Controller methods need to communicate some data. But, using this we can only achieve communication within single controller class. You do not use to read and write from and to the session explicitly if you are using @SessionAttributes. Usage of @SessionAttributes is suggested only for short lived communications. If you need to store long term data in session, it is suggested to use session.setAttribute and session.getAttribute explicitly, instead of @SessionAttributes. For more information check this out.

这篇关于springMVC中的Httpsession管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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