【问题标题】:overriding doPost method in servlet()在 servlet() 中重写 doPost 方法
【发布时间】: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:&nbsp;</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


【解决方案1】:

问题是您的 URL 不支持 POST 方法。通过查看所有 GET 请求,它们都以相对路径开始,然后是真实 URL,例如:

/Notification/notification
/Account/userManagement
/h <-- this seems ridiculous...
/admin
/hello

并在您的表单中发布到“测试”:

<form:form action="test" method="POST" modelAttribute = "user-entity">
    <!-- rest of your html code ... -->
</form:form>

这意味着任何帖子都会转到/&lt;whatever_goes_here&gt;/test,即(因为您没有指定哪个是您当前的视图):

/Notification/test
/Account/test
/test <-- this may work as expected
/test <-- this may work as expected
/test <-- this may work as expected

而且您没有前两个映射中的任何一个。

解决方案:修复您的网址或使用HttpServletRequest#getContextPath 转到真实网址。注意:avoid usage of scriptlets,而是在您的 JSP 中使用表达式语言:${request.contextPath}

<form:form action="${request.contextPath}/test" method="POST" modelAttribute = "user-entity">

【讨论】:

  • 我仍然遇到这个问题。我已将代码更新如下并尝试了一些解决方案
  • @aceminer 编辑您当前的问题并提供必要的详细信息以再次复制您的问题。
  • @aceminer 我可以看到您编辑了您的 `@Controller^ 但在您的 HTML 代码中看不到任何针对表单操作的更改。请提供您尝试使用 GET/POST 请求和您的映射访问的确切 URL。或者,您可能需要停止使用 Spring MVC 并从头开始学习并转到 Servlets。
  • 我的错。我没有发布整个动作。这次我说得更清楚了。
  • @aceminer 你提供的&lt;package_name&gt; 是什么?你的确切网址是什么?
猜你喜欢
  • 2011-08-18
  • 1970-01-01
  • 2012-02-27
  • 2011-02-01
  • 1970-01-01
  • 2013-05-26
  • 2014-04-14
  • 2015-08-24
  • 2017-01-05
相关资源
最近更新 更多