我已经在jsp和表单中开发了一个表单,如果用户点击提交按钮,那么控制器应该捕获参数,但遗憾的是它不起作用。
/*<form action="http://localhost:8080/school" method="POST">*/
<form action="/school" method="POST">
School name: <input type="text" id="school" name="school" />
<input type="hidden" value="${firstName}" name = "firstName"/>
<input type="hidden" value="${lastName}" name = "lastName"/>
<input type="submit" value="Submit" />
</form>
控制器功能如下:
@RequestMapping(value = "/school", method = RequestMethod.POST)
public void setSchool(HttpServletRequest request,HttpServletResponse response){
String firstName= request.getParameter("firstName");
String lastName= request.getParameter("lastName");
String school= request.getParameter("school");
String status = userController.setSchool(firstName, lastName, school);
try {
if(!status.equals("SUCCESS")) {
response.sendError(HttpServletResponse.SC_FORBIDDEN, status);
}
response.sendRedirect("http://localhost:8080/getInsideSchool?school="+school+"&firstName="+firstName+"&lastName="+lastName);
} catch (IOException e) {
e.printStackTrace();
}
}
当我点击表格中的提交按钮时,我无法达到控制器功能。如何映射网址或修复此问题? 谢谢
答案 0 :(得分:0)
<form action="/school" method="POST">
答案 1 :(得分:0)
首先创建一个POJO
class User {
private String school;
private String firstName;
private String lastName;
// getter setter constructor
}
然后编写控制器方法如下
@RequestMapping(value = "/school", method = RequestMethod.POST)
public String home(@ModelAttribute User user) {
System.out.println(user.toString());
return "redirect:/home";
}