Java springMVC中的Httpsession管理

Java springMVC中的Httpsession管理,java,session,spring-mvc,httpsession,Java,Session,Spring Mvc,Httpsession,我是SpringMVC的新手,通过学习,我开始制作一个示例应用程序。我计划在SpringMVC中实现会话管理。我发现其中一个很有用 但是我不能清楚地理解它。我们向会话添加值,如 HttpSession session = request.getSession(false); session.setAttribute("key", value); session.setAttribute("key1", value1); 然后我们根据键获取值,如 session.getAttrubute("k

我是SpringMVC的新手,通过学习,我开始制作一个示例应用程序。我计划在SpringMVC中实现会话管理。我发现其中一个很有用

但是我不能清楚地理解它。我们向会话添加值,如

HttpSession session = request.getSession(false);
session.setAttribute("key", value);
session.setAttribute("key1",  value1);
然后我们根据键获取值,如

session.getAttrubute("key");
但在春季MVC中,我看不到任何类似的东西,这让我完全困惑

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

}
在上面的代码中,
@SessionAttributes(“think”)
完全把我弄糊涂了,就像这个
think
定义了什么一样,我也不需要返回
模型和视图
,因为我使用的是
主干.marionete.js

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

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

更新

以下是我的控制员,并提供了意见

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


}
package com.hexgen.puppet;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.ResponseBody;
导入org.springframework.web.bind.annotation.SessionAttributes;
导入com.hexgen.puppet.CreatePuppet;
导入java.util.ArrayList;
导入java.util.List;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpSession;
@控制器
公营木偶服务{
@RequestMapping(method=RequestMethod.POST,value=“/create”)
公共@ResponseBody
作废createOrder(@RequestBody CreatePuppet请求){
//在会话中添加/更新值的逻辑
}
@RequestMapping(method=RequestMethod.GET,value=“/list”)
公共@ResponseBody
列表getGroups(){
//从会话中检索对象并将其转换为列表并发送回的逻辑
回归木偶;
}
}

并在需要时转换对象并继续进行

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

您可以像这样在springmvc中处理会话管理。下面是一个控制器方法

@RequestMapping(value = { "/login" }, method = RequestMethod.POST)
@ResponseBody
public String login(HttpSession session,String username,String password) throws Exception {
    Member member=userService.authenticateUser(username, password);
    if(member!=null) {
        session.setAttribute("MEMBER", member);
    } else {
        throw new Exception("Invalid username or password");
    }
    return Utils.toJson("SUCCESS");
}
用户将传递用户名和密码,而Spring将自动注入会话属性。我们将验证此用户名和密码 来自db。为此,我们将使用一些服务方法,这些服务方法将依次调用存储库的一些方法来获取成员类的对象并返回它
这里是控制器。在应用程序方法中,您需要会话中保存的信息,将其在参数中传递给处理程序。您可以在

上找到关于如何在每次方法调用时验证此信息的更多详细信息。此示例对我很有帮助。。我想知道如何声明和使用
HttpSession
以及和平代码。请告诉我,谢谢。。。