【发布时间】:2014-08-31 14:55:17
【问题描述】:
我遇到了关于 JSP 不接受 PUT 请求的问题。所以我想知道如何解决它。我在堆栈溢出中阅读了这个相关的问题,但它没有解释如何解决它。
HTTP Status 405 - JSPs only permit GET POST or HEAD
来自 Rails 背景,我正在努力做到这一点,所以我使用 Rails REST 样式,如 PUT 进行更新,并使用 DELETE 删除用户资源。
但每当此控制器出现错误时,它会尝试将请求返回给原始 JSP,但 Tomcat 8.0.9 不接受请求并给出以下错误:“HTTP 状态 405 - JSP 只允许 GET POST 或 HEAD ”。我尝试在 Tomcat web.xml 中禁用只读 - 它没有任何效果,我仍然得到错误。我已将其切换为 POST 方法,并且流程运行良好。
有没有办法强制转发为 POST 方法,同时仍接受 PUT 方法的请求?
/**
* Edit a user account.
* @return the edit user view
*/
@RequestMapping(value = {"/update/{userId}"}, method = RequestMethod.PUT)
public String updateUser(@Valid @ModelAttribute("user") User user, BindingResult result, final RedirectAttributes redirectAttributes)
{
logger.debug(user);
// we check for duplicate email addresses during the update operation.
List<User> userCheckList = userRepository.findByEmail(user.getEmail());
if (userCheckList.size() > 0)
{
// size of list should only ever be 1
User userCheck = userCheckList.get(0);
if (userCheck.getId() != user.getId())
{
result.rejectValue("email", "error.user", "An account already exists for this user email address.");
}
}
if (result.hasErrors())
{
return "admin.users.edit";
}
// we locate the user and add it to the model
userRepository.save(user);
// the save operation was successful so we show the user message.
redirectAttributes.addFlashAttribute("user", user);
redirectAttributes.addFlashAttribute("message", "Updated successfully");
String viewName = "redirect:/admin/users";
logger.debug(viewName);
return viewName;
}
【问题讨论】:
-
谢谢我已经试过了,但没有任何效果,同样的错误发生了。
标签: spring-mvc