ksnortum
ksnortum

Reputation: 3057

Spring MVC controller won't respond to POST from a form

In short, I have a Spring 3.1 MVC project and my controller doesn't respond to the POST request when I hit the submit button.

There is no error, just no response. The controller method is not being called. I have a logger in the method that displays an INFO message and nothing is displayed (other INFO messages do display). MVC is working (at least partial) because I get a response from a "home" JSP page, but nothing for a POST.

I'm including things that seem important; tell me if there's something you'd like to see.

Controller class:

@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/views/register/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>

You get the register/first_time.jsp from this method in the controller:

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

These are the headers returned when you press the submit button in 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

Upvotes: 0

Views: 4836

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280168

If you change this

@RequestMapping(value = "first_time")

to

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

you will get a 405 Method Not Allowed when submitting your form.

When you don't specify a action attribute to a <form> element, as you do here

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

a browser will typically make a request to whatever URL it did its previous request.

You did your previous request, a GET, to

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

When you submit your form, the browser makes a POST request again to

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

Since you haven't specified a method on the @RequestMapping, it handles all request method types and therefore handles that POST.

The simple solution is to specify an action attribute on the <form>.

Upvotes: 2

Related Questions