简体   繁体   中英

Spring MVC - Request method post not supported

I have a Request method Post not supported error for my website and cannot figure out where I have gone wrong, apologies if it has a very simple solution this is my first Spring project. I have looked at similar questions to mine, and most seem to be caused by some sort of typo in the requestMethod but I cannot find the reason in my code.

The signup form:

    <form action="/register" method="post" class="form" role="form"
                commandName="userForm">
                <div class="row">
                    <div class="col-xs-12 col-sm-6 col-md-6">
                        <input class="form-control" name="firstname"
                            placeholder="First Name" type="text" required autofocus />
                    </div>
                    <div class="col-xs-12 col-sm-6 col-md-6">
                        <input class="form-control" name="lastname"
                            placeholder="Last Name" type="text" required />
                    </div>
                </div>
                <br />
                <div class="form-group">
                    <input class="form-control" name="email" placeholder="Your Email"
                        type="email" />
                </div>
                <div class="form-group">
                    <input class="form-control" name="reenteremail"
                        placeholder="Re-enter Email" type="email" />
                </div>
                <div class="form-group">
                    <input class="form-control" name="password"
                        placeholder="New Password" type="password" /> <label for="">
                        Birth Date</label>
                    <div class="row">
                        <div class="col-xs-4 col-md-4">
                            <select class="form-control">
                                <option value="Month">Month</option>
                            </select>
                        </div>
                        <div class="col-xs-4 col-md-4">
                            <select class="form-control">
                                <option value="Day">Day</option>
                            </select>
                        </div>
                        <div class="col-xs-4 col-md-4">
                            <select class="form-control">
                                <option value="Year">Year</option>
                            </select>
                        </div>
                    </div>
                    <label class="radio-inline"> <input type="radio"
                        name="sex" id="inlineCheckbox1" value="male" /> Male
                    </label> <label class="radio-inline"> <input type="radio"
                        name="sex" id="inlineCheckbox2" value="female" /> Female
                    </label> <br /> <br />
                    <button class="btn btn-lg btn-primary btn-block" type="submit">
                        Sign up</button>
            </form>

The method to handle the form:

@Controller
public class RegistrationController {

@RequestMapping(value = "/register", method = RequestMethod.GET)
public String viewRegistration(Map<String, Object> model) {
    User userForm = new User();    
    model.put("userForm", userForm);

    return "signup";
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("userForm") User user) {

    user.setId(0);
    user.setEmail(user.getEmail());
    user.setPassword(user.getPassword());

    return "RegistrationSuccess";
}

}

I attempted so simplify the controller to pinpoint the issue but cannot manage to get it to work.

Whenever I click the submit button on the form, I get the following error:

osweb.servlet.PageNotFound : Request method 'POST' not supported

Any ideas on what can be causing this? Thanks!

Edit: The Screenshot of the post method from the developer tool. (Not entirely sure if this is the correct thing that was asked for?)

your path is incomplete:

<form action="/register" method="post" class="form" role="form"
            commandName="userForm">

you'll need to add something like:

<form action="http://localhost:8080/register" method="post" class="form" role="form"
            commandName="userForm">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM