Spring MVC控制器不会对表单中的POST做出响应

时间:2013-12-24 19:12:03

标签: java spring jsp spring-mvc post

简而言之,我有一个Spring 3.1 MVC项目,当我点击提交按钮时,我的控制器没有响应POST请求。

没有错误,只是没有回复。未调用控制器方法。我在方法中有一个显示INFO消息的记录器,没有显示任何内容(显示其他INFO消息)。 MVC正在工作(至少是部分),因为我从“主页”JSP页面获得响应,但没有用于POST。

我包括看似重要的事情;告诉我你是否有什么想看的。

控制器类:

@Controller
@RequestMapping("/register")
public class EditorController {
...

    @RequestMapping(method = RequestMethod.POST)
    public String addEditorFromForm(@Valid Editor editor,
            BindingResult bindingResult) throws ClassNotFoundException,
            IOException {

        if (LOG.isInfoEnabled()) {
            LOG.info("In addEditorFromForm()");
        }

        // Form did not validate
        if (bindingResult.hasErrors()) {
            return "register/first_time";
        }

        getEditorDao().saveEditor(editor);

        // New editor still needs to login
        return "home";
    }

}

WEB-INF /视图/注册/ first_time.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="<c:url value='/resources/css/base.css' />">
<title>Register for DIY Doctrine</title>
</head>
<body class="center">

<h2>Sign up for DIY Doctrine</h2>

<sf:form method="POST" modelAttribute="editor">
<fieldset>
    <table cellspacing="0">
        <tr>
            <th><label for="username">Username:</label></th>
            <td><sf:input path="username" size="30" id="username"/></td>
        </tr>

        <tr>
            <th><label for="user_password">Password:</label></th>
            <td><sf:password path="password" size="30" showPassword="false"
                id="user_password"/>
        </td>
        </tr>
    </table>
</fieldset>
<input type="submit" value="Submit Registration" />
</sf:form>
</body>
</html>

您在控制器中从此方法获得register/first_time.jsp

@RequestMapping(value = "first_time")
    public String displayForm(Model model) {
        if (LOG.isInfoEnabled()) {
            LOG.info("In displayForm()");
        }
        model.addAttribute(new Editor());
        return "register/first_time";
    }

这些是您在register/first_time中按下提交按钮时返回的标头:

Request URL:http://localhost:8080/doctrine/register/first_time
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,sl;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:34
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=08209E778F63429CFA031A1DB53491DD
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/doctrine/register/first_time
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.41 Safari/537.36
Form Dataview sourceview URL encoded
username:ngeorgewitz
password:1234
Response Headersview source
Content-Language:en-US
Content-Length:950
Content-Type:text/html;charset=ISO-8859-1
Date:Tue, 24 Dec 2013 20:28:19 GMT
Server:Apache-Coyote/1.1

1 个答案:

答案 0 :(得分:2)

如果你改变了这个

@RequestMapping(value = "first_time")

@RequestMapping(value = "first_time", method = RequestMethod.GET)

提交表单时,您将获得405方法不允许。

如果没有为action元素指定<form>属性,就像在此处一样

<sf:form method="POST" modelAttribute="editor">

浏览器通常会向其先前请求的任何URL发出请求。

您之前的请求是GET,

some-host:[some-port]/your-context/first_time

当您提交表单时,浏览器会再次向

发出POST请求
some-host:[some-port]/your-context/first_time

由于您未在method上指定@RequestMapping,因此它会处理所有请求方法类型,因此会处理该POST。

简单的解决方案是在action上指定<form>属性。