问题描述
我对Spring MVC还是比较陌生,并且一直在探索某种形式的提交方式.但是,当前,我有HTTP 405错误,这意味着我无法发布.
i am relatively new to spring mvc and have been exploring some form submit. However, currently, i have an error of HTTP 405 which would mean that i am unable to post.
错误是不支持HTTP发布.我已经用谷歌搜索过并检查我是否需要在代码中重写并实现doPost方法,但是我不确定如何使用该servlet.
The error is that the HTTP post is not supported. I have googled and checked that i would need to override and implement doPost method in my code but i am unsure of how to use the servlet.
通过重写doPost方法,如何确保应用新的doPost方法?
By overriding the doPost method, how do I ensure that the new doPost method is applied?
这是我的控制器类:
package com.**.web.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.**.dao.*;
import com.**.model.User;
@Controller
public class MainController {
@Autowired
private UserDAO UserDAO;
@RequestMapping(value = {"/hello", "/welcome**" }, method = RequestMethod.GET)
public ModelAndView defaultPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Login Form - Database Authentication");
model.addObject("message", "This is default page!");
model.setViewName("hello");
return model;
}
@RequestMapping(value = "/admin**", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Login Form - Database Authentication");
model.addObject("message", "This page is for ROLE_ADMIN only!");
model.setViewName("admin");
return model;
}
@RequestMapping(value = {"/h", "/login"}, method = RequestMethod.GET)
public ModelAndView login(@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "Invalid username and password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
model.setViewName("login");
return model;
}
//for 403 access denied page
@RequestMapping(value = "/402", method = RequestMethod.GET)
public ModelAndView accesssDenied() {
ModelAndView model = new ModelAndView();
//check if user is login
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!(auth instanceof AnonymousAuthenticationToken)) {
UserDetails userDetail = (UserDetails) auth.getPrincipal();
System.out.println(userDetail);
model.addObject("username", userDetail.getUsername());
}
model.setViewName("402");
return model;
}
@RequestMapping(value = "/Account/userManagement", method = RequestMethod.GET)
public ModelAndView accountPage() {
ModelAndView model = new ModelAndView();
model.setViewName("Account/userManagement");
return model;
}
@RequestMapping(value = "Notification/notification", method = RequestMethod.GET)
public ModelAndView NotificationPage() {
ModelAndView model = new ModelAndView();
model.setViewName("Notification/notification");
return model;
}
@RequestMapping(value = "test", method = RequestMethod.POST)
public ModelAndView register(@ModelAttribute("user-entity") User user, BindingResult result)
{
ModelAndView model = new ModelAndView();
UserDAO.create(user);
model.setViewName("hello");
return model;
}
@RequestMapping(value = {"/","/Account/registration"}, method = RequestMethod.GET)
public ModelAndView registerPage()
{
ModelAndView model = new ModelAndView("/Account/registration", "user-entity", new User());
return model;
}
}
这是我的表单代码
<form:form action="test" method="POST" modelAttribute = "user-entity">
<td> <td><form:label path="Username">Name:</form:label></td>
<td>
<form:input path="Username"></form:input>
</td>
</tr>
<tr>
<td>Password: </td>
<td><form:input type = "password" path = "Password" ></form:input></td>
</tr>
</form:form>
例外
这是我以前的问题的链接
This is a link to my previous question
我将表单操作修改为
<form:form action="/<packagename>/Account/test" method="POST" modelAttribute = "user-entity">
注册页面的默认网址是
http://localhost:8080/<package name>/
我将相应的控制器代码更新为
and i updated my corresponding controller code to
@RequestMapping(value = "/<package name>/Account/test", method = RequestMethod.POST)
public ModelAndView register(@ModelAttribute("user-entity") User user, BindingResult result)
{
ModelAndView model = new ModelAndView();
UserDAO.create(user);
model.setViewName("/Account/test");
return model;
}
我要访问的网址是/Account/test
The url i am trying to get to is /Account/test
推荐答案
问题是您的URL不支持POST方法.通过查看所有GET请求,它们都以相对路径开头,然后是真实URL,例如:
The problem is that your URL doesn't support a POST method. By looking at all your GET requests, they all start with a relative path and then the real URL, for example:
/Notification/notification
/Account/userManagement
/h <-- this seems ridiculous...
/admin
/hello
然后以您的表格形式发布到测试":
And in your form you post to "test":
<form:form action="test" method="POST" modelAttribute = "user-entity">
<!-- rest of your html code ... -->
</form:form>
这意味着任何帖子都将发布到/<whatever_goes_here>/test
,即(因为您未指定当前视图是哪个):
Which means that any post will go to /<whatever_goes_here>/test
i.e. (since you don't specify which one is your current view):
/Notification/test
/Account/test
/test <-- this may work as expected
/test <-- this may work as expected
/test <-- this may work as expected
而且您没有前两个映射中的任何一个.
And you don't have any of the first two mappings.
解决方案:修复您的URL或使用 HttpServletRequest#getContextPath
.注意: 避免使用脚本集 ,请在您的JSP中使用表达式语言:${request.contextPath}
Solution: fix your URLs or go to the real URL by using HttpServletRequest#getContextPath
. Note: avoid usage of scriptlets, instead use the Expression Language in your JSP: ${request.contextPath}
.
<form:form action="${request.contextPath}/test" method="POST" modelAttribute = "user-entity">
这篇关于覆盖servlet()中的doPost方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!