簡體   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