@Autowired在这段代码中做了什么

时间:2022-02-26 19:25:02

Need to understand what is @Autowired doing in this code. I am kind of confused with the scope=session? Does it mean now the user is available in the HttpSession? How do I retrieve it from a HttpSession? If I remove redirectAttrs.addFlashAttribute("user", user); then I don't see the user in JSP page?

需要了解@Autowired在此代码中做了什么。我对scope = session感到困惑?这是否意味着现在用户在HttpSession中可用?如何从HttpSession中检索它?如果我删除redirectAttrs.addFlashAttribute(“user”,user);然后我在JSP页面中看不到用户?

User class and its mapped as below

用户类及其映射如下

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

Controller below redirects to another Controller which does not contain anything but a location to the landing page.jsp

下面的控制器重定向到另一个控制器,该控制器除了登陆页面的位置之外不包含任何内容.jsp

@Autowired
@Qualifier("user")
private User user;

@RequestMapping(method=RequestMethod.POST)
public String post(@ModelAttribute User user, BindingResult result, SessionStatus status, final RedirectAttributes redirectAttrs) {
    logger.info("post");
    new UserValidator().validate(user, result);
    if (result.hasErrors()) {
        return "login";
    }
    else {
        status.setComplete();           
        logger.info("Email Id: " + user.getEmailId());          
        redirectAttrs.addFlashAttribute("user", user);          
        return "redirect:/landing.htm";
    }
}   

1 个解决方案

#1


2  

Scope session means that user object will be kept for until the clients session is destroyed. It doesn't mean that you have it in HttpSession object. @Autowired annotation is used to wire user bean automatically. It's used to get bean from the application context and assign it to the local variable in your Java class.

范围会话意味着将保留用户对象,直到客户端会话被销毁。这并不意味着你在HttpSession对象中拥有它。 @Autowired注释用于自动连接用户bean。它用于从应用程序上下文中获取bean并将其分配给Java类中的本地变量。

#1


2  

Scope session means that user object will be kept for until the clients session is destroyed. It doesn't mean that you have it in HttpSession object. @Autowired annotation is used to wire user bean automatically. It's used to get bean from the application context and assign it to the local variable in your Java class.

范围会话意味着将保留用户对象,直到客户端会话被销毁。这并不意味着你在HttpSession对象中拥有它。 @Autowired注释用于自动连接用户bean。它用于从应用程序上下文中获取bean并将其分配给Java类中的本地变量。