RedirectView 在 Spring 4.1 上无法识别 @RequestParam
RedirectView doesn't recognize @RequestParam on Spring 4.1
以下代码适用于 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'
}
将版本设置为“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
如何在 Spring 4.1.4 上使用带参数的 RedirectView?
谢谢,
您可以像这样在 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");
}
以下代码适用于 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'
}
将版本设置为“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
如何在 Spring 4.1.4 上使用带参数的 RedirectView?
谢谢,
您可以像这样在 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");
}