gpt4 book ai didi

java - Spring MVC PUT 方法我得到 HTTP 状态 405,无法重定向到另一个内部 View

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:23 24 4
gpt4 key购买 nike

我正在使用 Spring MVC 来添加和更新表单。不知道为什么在提交的 BindingResult (表单验证、错误输入等)出现任何错误时无法重定向回同一表单以显示错误消息。当使用 PUT 方法(更新内容)而不是 POST 方法(添加内容)时会发生这种情况...请任何人建议我应该做什么。提前致谢。

我收到的浏览器错误:

HTTP Status 405 - HTTP method PUT is not supported by this URL
type Status report
messageHTTP method PUT is not supported by this URL
descriptionThe specified HTTP method is not allowed for the requested resource.
GlassFish Server Open Source Edition 4.1.1

控制台没有显示任何内容...只有我制作的日志显示哪个输入字段出现错误。

这里是 Controller 内部的方法:

//Add - no problem can show validation error message in the JSP form
@RequestMapping(value = "/userDetail", method = RequestMethod.POST)
public String saveUserDetail(@Valid UserDetail userDetail,
Errors errors) {
if (errors.getErrorCount() > 0) {
log.info("User attempt create user: " + userDetail.toString());
for (FieldError error : errors.getFieldErrors()) {
log.info(error.getField() + ": " + error.getDefaultMessage());
}
//redirect to same page to show error return infor (NotEmpty etc...)
return "/user/inputUserDetail"; //same structure for PUT method below, but not working show me 405 error
} else {
try {
userDetailService.addUserDetail(userDetail);
} catch (Exception e) {
log.error("Failed to add new UserDetail: " + userDetail.toString());
}
return "redirect:/user/userDetails";
}
}

//Update - need UserDetail model attribute
@RequestMapping(value = "/userDetail", method = RequestMethod.PUT)
public ModelAndView updateUserDetail(@Valid UserDetail userDetail,
Errors errors, Map<String, Object> map) {
if (errors.getErrorCount() > 0) {
log.info("User attempt update user: " + userDetail.toString());
for (FieldError error : errors.getFieldErrors()) {
log.info(error.getField() + ": " + error.getDefaultMessage());
}
//I want to redirect to same form but show validation of previous inputted got error (NotEmpty, Email, typeMissMatch etc...)
//Below tested all failed

//Use this when return type is String
//return "/user/inputUserDetail"; //405 error
//return inputUserDetail(map); //405 error, direct call another method in same controller to show update form
//return "redirect:/user/userDetail/" + userDetail.getUdId(); //ok, but this one is redirect, no current errors bind to the update form

//Use this when return type is ModelAndView
map.put(BindingResult.MODEL_KEY_PREFIX + "userDetail", errors);
map.put("userDetail", userDetail);

ModelAndView mv = new ModelAndView();
mv.addObject(new RedirectView("FaddistWardrobe/user/userDetail"));
mv.addAllObjects(map);
return mv;
} else {
try {
//update user detail
userDetailService.updateUserDetail(userDetail);
} catch (Exception e) {
log.error("Failed to update UserDetail: " + userDetail.toString());
}
//This one okay can perform while form data correct
//If return String like above method, result same, both ok
return new ModelAndView("redirect:/user/userDetails");
}
}

我遵循 REST url 样式 PUT 方法更新用户的映射,不知道使用 PUT 方法时是否可能无法调用内部 JSP?或者我错过了 PUT 方法设置的任何配置?这个web应用程序已经配置了org.springframework.web.filter.HiddenHttpMethodFilter...

感谢并欢迎任何建议,我刚刚开始学习 Spring MVC...希望有人帮忙...再次感谢。

最佳答案

问题是尝试像 PUT => Redirect => PUT 一样执行

(1) 当尝试添加正确的数据时,上面的代码没问题,因为它的执行方式类似于 PUT => Redirect => GET

(2)尝试使用PUT方法在同一页面显示错误导致失败

解决方案是将 PUT => Redirect => GET 更改为以下编码:

//Update - need UserDetail model attribute
@RequestMapping(value = "/userDetail", method = RequestMethod.PUT)
public String updateUserDetail(@Valid UserDetail userDetail,
Errors errors, Map<String, Object> map,
final RedirectAttributes redirectAttributes) {
if (errors.getErrorCount() > 0) {
//log.info("User attempt update user: " + userDetail.toString());
for (FieldError error : errors.getFieldErrors()) {
log.info(error.getField() + ": " + error.getDefaultMessage());
}
//redirect to same page to show error return infor (NotEmpty etc...)
redirectAttributes.addFlashAttribute("userDetail", userDetail);
redirectAttributes.addFlashAttribute(BindingResult.MODEL_KEY_PREFIX + "userDetail", errors);
return "redirect:/user/userDetail/error";
} else {
try {
//update user detail
userDetailService.updateUserDetail(userDetail);

} catch (Exception e) {
String tmp = e.getLocalizedMessage();
String tmp2 = e.toString();
//log.error("Failed to update UserDetail: " + userDetail.toString());
}
return "redirect:/user/userDetails";
}
}


//Display form with validation error after update fail (PUT -> Redirect -> GET)
@RequestMapping(value = "/userDetail/error", method = RequestMethod.GET)
public String inputUserDetail_error() {
//Here will auto retrieve previous redirectAttributes data and put into the JSP page.
return "/user/inputUserDetail";
}

只需使用redirectAttributes存储临时模型,当用户重定向后,可以检索回以前的模型并使用它。希望能够通过这个解决方案帮助其他刚接触 SpringMVC 的人。谢谢。

关于java - Spring MVC PUT 方法我得到 HTTP 状态 405,无法重定向到另一个内部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38220770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com