如何在spring应用程序中为session添加属性

kpbwa7wx  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(468)

我有一个网上商店的应用程序。我想让每个用户创建他们自己的篮子,他们可以把一些产品。在那之后,他可以编辑这个篮子,等等。什么是最好的方法来做这件事?我已经在这里问过这个问题了,但是没有足够的答案。我搜索了关于这个问题的信息,但也没有找到我需要的表格。
我目前正在使用以下方案
一旦有用户访问我的站点,我就为他创建一个购物车并添加到会话中,如下所示:

  1. @GetMapping("/")
  2. public String sayHello(HttpSession session) {
  3. session.setAttribute("bucket", new ArrayList<ProductDto>());
  4. return "index";
  5. }

要向此购物车添加内容,我使用以下方法:

  1. @GetMapping("/add/{id}")
  2. public String addProductToBucket(@SessionAttribute("bucket")ArrayList<ProductDto> bucket,
  3. @PathVariable("id") long id,
  4. Model model){
  5. bucket.add(productService.getById(id));
  6. return "redirect:/product";
  7. }

把车倒空我就写了

  1. bucket.clear ();

我做的每件事都是对的还是我需要改变工作方式?还有一个问题。如何在会话中设置此对象的生存期?我希望他有条件地在那里住20分钟

nwlqm0z1

nwlqm0z11#

下面带注解的代码将“value”设置为“item”:

  1. @RequestMapping(method = RequestMethod.GET)
  2. public String testMestod(HttpServletRequest request){
  3. request.getSession().setAttribute("item",value);
  4. return "index.html";
  5. }
sxpgvts3

sxpgvts32#

如果要在用户会话期间保留对象,有以下几种方法:

  1. //directly add one attribute to the session
  2. @RequestMapping(method = RequestMethod.GET)
  3. public String testMestod(HttpServletRequest request){
  4. ShoppingCart cart = (ShoppingCart)request.getSession().setAttribute("cart",value);
  5. return "testJsp";
  6. }
  7. //get it from the controller
  8. ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
  9. Make your controller session scoped
  10. //Scope the Objects, for example, you have a user object that should be in session every time
  11. @Component
  12. @Scope("session")
  13. public class User
  14. {
  15. String user;
  16. /* setter getter*/
  17. }
  18. //inject class in each controller that you want.
  19. @Autowired
  20. private User user
  21. //The AOP proxy injection : in spring -xml
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24. xmlns:aop="http://www.springframework.org/schema/aop"
  25. xsi:schemaLocation="http://www.springframework.org/schema/beans
  26. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  27. http://www.springframework.org/schema/aop
  28. http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
  29. <bean id="user" class="com.User" scope="session">
  30. <aop:scoped-proxy/>
  31. </bean>
  32. </beans>
  33. //Pass HttpSession to method
  34. String index(HttpSession session) {
  35. session.setAttribute("mySessionAttribute", "someValue");
  36. return "index";
  37. }
  38. //Make ModelAttribute in session By @SessionAttributes("ShoppingCart"):
  39. public String index (@ModelAttribute("ShoppingCart") ShoppingCart shoppingCart, SessionStatus sessionStatus) {
  40. //Spring V4
  41. //you can modify session status by sessionStatus.setComplete();
  42. }
  43. //or you can add Model To entire Controller Class like,
  44. @Controller
  45. @SessionAttributes("ShoppingCart")
  46. @RequestMapping("/req")
  47. public class MYController {
  48. @ModelAttribute("ShoppingCart")
  49. public Visitor getShopCart (....) {
  50. return new ShoppingCart(....); //get From DB Or Session
  51. }
  52. }
展开查看全部
dgjrabp2

dgjrabp23#

首先,通过添加@scope(“session”)使控制器会话具有作用域。

  1. @Controller
  2. @Scope("session")

然后向会话添加一个属性

  1. @RequestMapping(method = RequestMethod.GET)
  2. public String sayHello(HttpServletRequest request){
  3. request.getSession().setAttribute("cart",valueOfCart);
  4. return "index";
  5. }

对于会话超时,请在application.properties中设置property server.session.timeout=值以秒为单位。
然后确定每次应该在会话中的用户对象的范围

  1. @Component
  2. @Scope("session")
  3. public class User
  4. {
  5. String user;
  6. /*getter setter*/
  7. }

然后在每个需要的控制器中注入类

  1. @Autowired
  2. private User user
展开查看全部

相关问题