springMVC中的Httpsession管理

我是初学MVC并开始通过做我学到的东西来做一个示例应用程序。 我打算在spring MVC中实现Session管理。 我觉得这个很有帮助。

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

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("thought")完全让我困惑,就像这个thought定义的那样,我也没有必要返回一个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 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 getGroups() { //logic to retrive objects from session and convert it as List and send it back return puppets; } } 

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

@SessionAttributes并没有完全取代传统的HttpServlet会话管理。 如果两个或更多Controller方法需要传递某些数据,请使用它。 但是,使用这个我们只能在单个控制器类中实现通信。 如果使用@SessionAttributes,则不使用显式读取和写入会话。 建议仅对短期通信使用@SessionAttributes 。 如果需要在会话中存储长期数据,建议显式使用session.setAttributesession.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将自动注入会话属性。 我们将从dbvalidation此用户名和密码。 为此,我们将使用一些服务方法,该方法将调用某些存储库方法来获取Member类的Object并将其返回到控制器中。 在应用程序方法中,您需要将会话中保存的信息传递给处理程序的参数。 您可以在http://faisalbhagat.blogspot.com/2014/09/session-management-with-spring-mvc.html上的每个方法调用中找到有关如何validation此信息的更多详细信息。