RedirectView无法识别Spring 4.1上的@RequestParam

时间:2015-01-21 07:55:31

标签: spring spring-mvc

以下代码在Spring Boot 1.1.10(Spring 4.0.8)上工作但在1.2.1(Spring 4.1.4)上没有工作

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'
}

将版本设置为&#39; 1.1.10&#39;它工作正常,但当设置为&#39; 1.2.1&#39;时,会出现一条错误消息:

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

如何在Spring 4.1.4上使用带有参数的RedirectView?

谢谢,

1 个答案:

答案 0 :(得分:1)

您可以在DefaultController方法中使用RedirectAttributes,如下所示:

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