覆盖servlet()中的doPost方法

时间:2014-08-04 14:51:10

标签: java spring servlets post model-view-controller

我对spring mvc相对较新,一直在探索一些表单提交。但是,目前,我有一个错误的HTTP 405,这意味着我无法发布。

错误是不支持HTTP帖子。我用google搜索并检查我需要在我的代码中覆盖和实现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   警告:请求方法&#39; POST&#39;不支持

这是我之前提问的链接

POST not working in spring mvc 4

我将表单操作修改为

 <form:form action="/<packagename>/Account/test" method="POST" modelAttribute = "user-entity">

注册页面的默认网址是

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

1 个答案:

答案 0 :(得分:1)

问题是您的网址不支持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>

这意味着任何帖子都会转到/<whatever_goes_here>/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">
相关问题