2015-01-21 63 views
0

繼春季引導工作的代碼1.1.10(春季4.0.8),但不是在1.2.1(春季4.1.4)RedirectView的不承認@RequestParam於Spring 4.1

DefaultController.java

@Controller 
@RequestMapping(value = "/") 
public class DefaultController { 

    @RequestMapping(method= RequestMethod.GET) 
    public RedirectView show(Map<String, Object> model) throws MissingServletRequestParameterException { 
     model.put("req", "expected"); 
     return new RedirectView("redirect"); 
    } 
} 

RedirectController.java

@Controller 
@RequestMapping(value = "/redirect") 
public class RedirectController { 
    private static final Logger logger = LoggerFactory.getLogger(RedirectController.class); 

    @RequestMapping(method= RequestMethod.GET) 
    public String show(@RequestParam("req") String req) throws MissingServletRequestParameterException { 
     logger.info("parameter is {}", req); 
     return "/redirect"; 
    } 
} 

依賴於的build.gradle是以下內容:

dependencies { 
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf:' + version + '.RELEASE' 
    compile 'org.springframework.boot:spring-boot-starter-security:' + version + '.RELEASE' 
} 

集版本爲「1.1.10」它工作正常,但是當設置爲「1.2.1」,會出現一個錯誤信息:

Wed Jan 21 16:13:45 JST 2015 
There was an unexpected error (type=Bad Request, status=400). 
Required String parameter 'req' is not present 

如何使用RedirectView的與彈簧參數4.1.4?

感謝,

回答

1

您可以使用RedirectAttributes在DefaultController方法是這樣的:

@RequestMapping(method= RequestMethod.GET) 
public RedirectView show(Map<String, Object> model, RedirectAttributes redirectAttributes) throws MissingServletRequestParameterException { 
    redirectAttributes.addAttribute("req","foo"); 
    return new RedirectView("redirect"); 
} 
+0

這工作!謝謝您的回答! – 2015-01-21 09:11:50