通过@Scope("session") 在会话中存储和检索对象
Store and retrieve an object in session through @Scope("session")
我有一个 Spring 购物项目,我正在开发一个购物车,我可以向其中添加新产品并将其存储在会话中。我创建了一个购物车 class 以将其存储在会话
中
import java.util.HashMap;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Scope;
@Component
@Scope("session")
public class Cart {
// key: product id
// value: product information
private HashMap<Integer,Product> productlist;
public HashMap<Integer, Product> getProductlist() {
return productlist;
}
public void setProductlist(HashMap<Integer, Product> productlist) {
this.productlist = productlist;
}
}
我创建了一个控制器 class 以从会话中获取购物车并向其中添加产品
@Controller
@Scope("request")
public class AddToCartController {
@Autowired
private Cart cart;
@Autowired
ProductService proService;
@RequestMapping("/cart/add")
public String addToCart(@RequestParam Optional<String> pid) {
if(pid.isPresent()) {
Product productinfo = proService.getProductByPid(pid.get());
if(productinfo.getQuantity()>0) {
int pidInteger = Integer.parseInt(pid.get());
try {
Product product = cart.getProductlist().get(pidInteger);
// there is already product in cart. add 1 to quantity
cart.getProductlist().get(pidInteger).setQuantity(product.getQuantity()+1);
} catch(NullPointerException e) {
// add the new product to cart with quantity 1
productinfo.setQuantity(1);
cart.getProductlist().put(pidInteger, productinfo);
}
}
}
return "redirect:/cart";
}
}
但是当我调用这个控制器时它发回一个错误
java.lang.NullPointerException: null
at com.example.phonestore.controller.AddToCartController.addToCart(AddToCartController.java:45) ~[classes/:na]
您可以尝试使用代理吗,
@Scope(value = WebApplicationContext.SCOPE_SESSION,
proxyMode = ScopedProxyMode.TARGET_CLASS)
示例:
@Component
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class VisitorInfo implements Serializable {
private String name;
private int visitCounter;
private LocalDateTime firstVisitTime;
//getters/setters
.............
}
也可以使用注解@SessionScope
,与上面的配置相同:
Specifically, {@code @SessionScope} is a composed
annotation that acts as a shortcut for {@code> @Scope("session")} with the default {@link #proxyMode} set to
{@link ScopedProxyMode#TARGET_CLASS TARGET_CLASS}.
我认为 NullPointerException 是由于您没有初始化“productlist”造成的。您可以尝试这样的操作:“private HashMap productlist = new HashMap<>(); ”。
如果“请求范围”控制器是唯一依赖于它的 bean,则可以在不指定“@Scope”注释的“proxyMode”属性的情况下使用购物车上的“会话范围”。
但通常 Controller 应该是“单例”作用域,除非您有充分的理由选择另一种作用域。而如果Controller是“单例”作用域,则应指定“@Scope”注解的“proxyMode”属性。
我有一个 Spring 购物项目,我正在开发一个购物车,我可以向其中添加新产品并将其存储在会话中。我创建了一个购物车 class 以将其存储在会话
中import java.util.HashMap;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Scope;
@Component
@Scope("session")
public class Cart {
// key: product id
// value: product information
private HashMap<Integer,Product> productlist;
public HashMap<Integer, Product> getProductlist() {
return productlist;
}
public void setProductlist(HashMap<Integer, Product> productlist) {
this.productlist = productlist;
}
}
我创建了一个控制器 class 以从会话中获取购物车并向其中添加产品
@Controller
@Scope("request")
public class AddToCartController {
@Autowired
private Cart cart;
@Autowired
ProductService proService;
@RequestMapping("/cart/add")
public String addToCart(@RequestParam Optional<String> pid) {
if(pid.isPresent()) {
Product productinfo = proService.getProductByPid(pid.get());
if(productinfo.getQuantity()>0) {
int pidInteger = Integer.parseInt(pid.get());
try {
Product product = cart.getProductlist().get(pidInteger);
// there is already product in cart. add 1 to quantity
cart.getProductlist().get(pidInteger).setQuantity(product.getQuantity()+1);
} catch(NullPointerException e) {
// add the new product to cart with quantity 1
productinfo.setQuantity(1);
cart.getProductlist().put(pidInteger, productinfo);
}
}
}
return "redirect:/cart";
}
}
但是当我调用这个控制器时它发回一个错误
java.lang.NullPointerException: null
at com.example.phonestore.controller.AddToCartController.addToCart(AddToCartController.java:45) ~[classes/:na]
您可以尝试使用代理吗,
@Scope(value = WebApplicationContext.SCOPE_SESSION,
proxyMode = ScopedProxyMode.TARGET_CLASS)
示例:
@Component
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class VisitorInfo implements Serializable {
private String name;
private int visitCounter;
private LocalDateTime firstVisitTime;
//getters/setters
.............
}
也可以使用注解@SessionScope
,与上面的配置相同:
Specifically, {@code @SessionScope} is a composed annotation that acts as a shortcut for {@code> @Scope("session")} with the default {@link #proxyMode} set to {@link ScopedProxyMode#TARGET_CLASS TARGET_CLASS}.
我认为 NullPointerException 是由于您没有初始化“productlist”造成的。您可以尝试这样的操作:“private HashMap
如果“请求范围”控制器是唯一依赖于它的 bean,则可以在不指定“@Scope”注释的“proxyMode”属性的情况下使用购物车上的“会话范围”。
但通常 Controller 应该是“单例”作用域,除非您有充分的理由选择另一种作用域。而如果Controller是“单例”作用域,则应指定“@Scope”注解的“proxyMode”属性。