我是Spring MVC的新手。单击按钮时,我创建了一个简单的表单,表单值发送到控制器并在第二个视图中设置。但是当我点击按钮时它会给我错误
The request sent by the client was syntactically incorrect.
这是我的代码:
控制器:
@Controller
@RequestMapping(value="/admissionform")
public class StudentAdmissionController {
@RequestMapping(value="/form.html", method = GET)
public ModelAndView getStudentForm() {
ModelAndView model = new ModelAndView("StudentForm");
return model;
}
@RequestMapping(value="/submit.html", method = POST)
public ModelAndView submitStudentForm(@RequestParam("name") String name,
@RequestParam("password") String password) {
ModelAndView model = new ModelAndView("SubmitForm");
model.addObject("msg", "Name is : "+name + " Password is : " + password);
return model;
}
}
StudentForm:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Student Form</title>
</head>
<body>
<div class="modal-body" id="main-body">
<form action="submit.html" method="post">
<div>
<label>Email address:</label>
<input class="form-control" id="email" name="email">
</div>
<div >
<label for="pwd">Password:</label>
<input id="pwd" name="password">
</div>
<input type="submit" value="Submit"/>
</form>
</div>
</body>
</html>
SubmitForm:将
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Submitted</title>
</head>
<body>
<div>${msg}</div>
</body>
</html>
告诉我我在代码中犯的错误是什么。我将感恩:)
答案 0 :(得分:0)
默认情况下,使用@RequestParam
注释的参数是必需的。因此,这些参数应该存在于客户端请求中。在submitStudentForm
方法中,您有两个名为name
和password
的必需参数:
@RequestMapping(...)
public ModelAndView submitStudentForm(@RequestParam("name") String name,
@RequestParam("password") String password) { ... }
然后你应该在你的from中传递具有完全相同名称的那些参数。目前,您正在传递password
参数:
<div>
<label for="pwd">Password:</label>
<input id="pwd" name="password">
</div>
但是对于不幸的name
参数未能这样做。我想你应该将email
参数重命名为name
:
div>
<label>Email address:</label>
<input class="form-control" id="email" name="name">
</div>
有关详细信息,您可以查看Spring documentation。