【发布时间】:2014-09-27 02:14:48
【问题描述】:
我对 spring mvc 比较陌生,并且一直在探索一些表单提交。但是,目前,我有一个 HTTP 405 错误,这意味着我无法发布。
错误是不支持HTTP post。我已经用谷歌搜索并检查了我是否需要在我的代码中覆盖和实现 doPost 方法,但我不确定如何使用 servlet。
通过重写 doPost 方法,如何确保应用新的 doPost 方法?
这是我的控制器类:
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>
例外
2014 年 8 月 3 日下午 6:12:53 org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported 警告:不支持请求方法“POST”
这是我上一个问题的链接
POST not working in spring mvc 4
我将表单操作修改为
<form:form action="/<packagename>/Account/test" method="POST" modelAttribute = "user-entity">
注册页面的默认url是
http://localhost:8080/<package name>/
我将相应的控制器代码更新为
@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
【问题讨论】:
-
如果您使用 Spring,则无需覆盖
doPost。只需在您的@Controller中提供适当的处理程序方法即可。 -
@SotiriosDelimanolis 我确实做到了,但它根据我之前问题中的链接抛出了错误
-
请不要在网站上多次发布同一个问题。专注于一个并等待它。如果需要更多关注,请提供赏金。
-
另外,下次请为您的问题使用正确的标签。
-
与您之前发布的问题有什么不同?你是因为想要更多关注而重新发布它吗?
标签: java spring servlets post model-view-controller