如何在SpringMVC中向控制器获取表单值
我试图在SpringMVC中从jsp获取表单值到控制器,但无法获取表单数据 这是我的DTO(bean) 和我的Jsp如何在SpringMVC中向控制器获取表单值,spring,jsp,spring-mvc,modelattribute,Spring,Jsp,Spring Mvc,Modelattribute,我试图在SpringMVC中从jsp获取表单值到控制器,但无法获取表单数据 这是我的DTO(bean) 和我的Jsp <form class="form-signin" action="test" method="get" modelAttribute="userFormData"> <input type="text" class="form-control" placeholder="Email" required autofocus>
<form class="form-signin" action="test" method="get" modelAttribute="userFormData">
<input type="text" class="form-control"
placeholder="Email" required autofocus>
<input type="password" class="form-control"
placeholder="Password" required>
<input class="btn btn-md btn-success btn-block"
type="submit" value="Signin">
</form>
向JSP页面上的控件添加名称
<input type="text" name="username" ...>
<input type="password" name="password" ...>
让spring了解哪个表单控件值应该转到LoginDTO的哪个属性
我们还可以使用springframework为我们提供的表单标记。因此,我们也可以使用它,但在这种情况下,您必须定义与类中提供的成员变量相同的输入路径
像这样
<form:form method="post" modelAttribute="userFormData">
<form:input path="username" />
<form:input path="password" />
如果您希望在其他jsp页面以及控制台上获得结果,请执行以下操作:
public String checkLogin(@ModelAttribute("userFormData") LoginDTO formData, BindingResult
result , Model model){
System.out.println("=====> " + formData.getUsername()); //this outputs username on console
System.out.println("=====> " + formData.getPassword()); //this outputs password on console
model.addAttribute("LoginDTO ", LoginDTO );
return "success"; //this is the return page where the username and password will be rendered as view
}
<form:form method="post" modelAttribute="userFormData">
<form:input path="username" />
<form:input path="password" />
public String checkLogin(@ModelAttribute("userFormData") LoginDTO formData, BindingResult
result)
public String checkLogin(@ModelAttribute("userFormData") LoginDTO formData, BindingResult
result , Model model){
System.out.println("=====> " + formData.getUsername()); //this outputs username on console
System.out.println("=====> " + formData.getPassword()); //this outputs password on console
model.addAttribute("LoginDTO ", LoginDTO );
return "success"; //this is the return page where the username and password will be rendered as view
}