我有一个请求方法发布不支持我的网站错误,无法弄清楚我哪里出错了,如果它有一个非常简单的解决方案,这是我的第一个Spring项目,请道歉。我已经查看了类似的问题,而且大多数似乎都是由requestMethod中的某种拼写引起的,但我在代码中找不到原因。
注册表格:
<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>
处理表单的方法:
@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";
}
}
我尝试过如此简化控制器以查明问题,但无法让它运行起来。
每当我单击表单上的提交按钮时,都会收到以下错误:
o.s.web.servlet.PageNotFound:请求方法&#39; POST&#39;不支持
关于可能导致这种情况的任何想法?谢谢!
编辑:开发者工具中post方法的Screenshot。 (不完全确定这是否是正确的要求?)
答案 0 :(得分:0)
你的路径不完整:
<form action="/register" method="post" class="form" role="form"
commandName="userForm">
您需要添加以下内容:
<form action="http://localhost:8080/register" method="post" class="form" role="form"
commandName="userForm">