繁体   English   中英

使用RedirectAttributes作为bean spring mvc

[英]use RedirectAttributes as bean spring mvc

有没有办法在春季mvc做到这一点; 我想@Autowired RedirectAttributes:

@Controller
public class RegistrationController {
    @Autowired private RedirectAttributes redirectAttributes;

    @RequestMapping(value = SIGNUP_ROUTE, method = RequestMethod.POST)
    public String signUpPage(ModelMap modelMap, User user) {
        save(user);
        redirectAttributes.addFlashAttribute("success", "Very good");
        return "redirect:/sign-in";
    }
}

我得到的例外情况:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.mvc.support.RedirectAttributes com.myhome.controller.RegistrationController.redirectAttributes; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.support.RedirectAttributes] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

在代码中注入依赖项的一种方法是添加类RedirectAttributes的额外参数

@Controller
public class RegistrationController {

    @RequestMapping(value = SIGNUP_ROUTE, method = RequestMethod.POST)
    public String signUpPage(ModelMap modelMap, RedirectAttributes rda, User user) {
        save(user);
        rda.addFlashAttribute("success", "Very good");
        return "redirect:/sign-in";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM