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

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

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

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

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

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

把车倒空我就写了

bucket.clear ();

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

nwlqm0z1

nwlqm0z11#

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

@RequestMapping(method = RequestMethod.GET)
public String testMestod(HttpServletRequest request){

   request.getSession().setAttribute("item",value);
   return "index.html";
}
sxpgvts3

sxpgvts32#

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

//directly add one attribute to the session

@RequestMapping(method = RequestMethod.GET)
public String testMestod(HttpServletRequest request){
   ShoppingCart cart = (ShoppingCart)request.getSession().setAttribute("cart",value);
   return "testJsp";
}

 //get it from the controller

ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
Make your controller session scoped

//Scope the Objects, for example, you have a user object that should be in session every time

@Component
@Scope("session")
public class User
 {
    String user;
    /*  setter getter*/
  }

//inject class in each controller that you want.

   @Autowired
   private User user

//The AOP proxy injection : in spring -xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

  <bean id="user"    class="com.User" scope="session">     
      <aop:scoped-proxy/>
  </bean>
</beans>

//Pass HttpSession to method

 String index(HttpSession session) {
            session.setAttribute("mySessionAttribute", "someValue");
            return "index";
        }

//Make ModelAttribute in session By @SessionAttributes("ShoppingCart"):

  public String index (@ModelAttribute("ShoppingCart") ShoppingCart shoppingCart, SessionStatus sessionStatus) {
//Spring V4
//you can modify session status  by sessionStatus.setComplete();
}
//or you can add Model To entire Controller Class like,

@Controller
    @SessionAttributes("ShoppingCart")
    @RequestMapping("/req")
    public class MYController {

        @ModelAttribute("ShoppingCart")
        public Visitor getShopCart (....) {
            return new ShoppingCart(....); //get From DB Or Session
        }  
      }
dgjrabp2

dgjrabp23#

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

@Controller
@Scope("session")

然后向会话添加一个属性

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

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

@Component
@Scope("session")
public class User
 {
    String user;
    /*getter setter*/
  }

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

@Autowired
   private User user

相关问题