我是春季验证的新手。我有一个带有 4 个文本字段的loginform.html,我试图验证它们,但验证在我的表单中不起作用。但它接受我输入的每一个值。
例如:如果我输入了不正确的电子邮件,它应该会抛出一个验证错误,例如“输入正确的电子邮件 ID ”
登录表单.java
public class loginform {
@NotEmpty
private String user;
@NotEmpty
@Email
private String email;
@NotEmpty(message = "Phone should not be blank.")
@Size(min = 10,max = 10)
private String phone;
@NotEmpty(message = "Enter your blog URL")
@URL
private String blog;
// Get and set methods}
登录.html
<form:form action="login.html" commandName="userDetails">
<table>
<tr>
<td><font face="verdana" size="2px">User</font></td>
<td>:</td>
<td>
<font face="verdana" size="2">
<form:input path="user" /> <form:errors path="user"></form:errors>
</font>
</td>
</tr>
<tr>
<td><font face="verdana" size="2px">Email</font></td>
<td>:</td>
<td>
<font face="verdana" size="2">
<form:input path="email" /> <form:errors path="email"></form:errors>
</font>
</td>
</tr>
<tr>
<td><font face="verdana" size="2px">Phone</font></td>
<td>:</td>
<td>
<font face="verdana" size="2">
<form:input path="phone" /> <form:errors path="phone"></form:errors>
</font>
</td>
</tr>
<tr>
<td><font face="verdana" size="2px">Blog</font></td>
<td>:</td>
<td>
<font face="verdana" size="2">
<form:input path="blog" /> <form:errors path="blog"></form:errors>
</font>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form:form>
联系人控制器.java
@Controller
public class ContactController {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public String get(ModelMap model) {
loginform ud = new loginform();
ud.setUser("prem");
ud.setEmail("@gmail.com");
model.addAttribute("userDetails",ud);
return "login";
}
@RequestMapping("/login")
public String loginCheck(@Valid loginform userDetails, BindingResult result, ModelMap model) {
if (result.hasErrors()) {
return "login";
} else {
model.addAttribute("lfobj", userDetails);
return "success";
}
}
spring-servlet.xml
<context:annotation-config />
<context:component-scan base-package="net.viralpatel.spring3.controller" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="props" />
</bean>
成功.html
<body>
<font face="verdana" size="2">Welcome Mr. <b>${lfobj.user}</b>,<br>
Validations Success..!<br><br>
<u>You Entered</u><br>
</font>
<table>
<tr><td>Email</td><td>${lfobj.email}</td></tr>
<tr><td>Phone</td><td>${lfobj.phone}</td></tr>
<tr><td>Website</td><td>${lfobj.blog}</td></tr>
</table>
</body>
道具.properties
NotEmpty.userDetails.user = User Name is required
NotEmpty.userDetails.email = Email is required
Email.userDetails.email = Enter valid email Id
URL.userDetails.email = Enter valid URL
输出:运行时:(loginform.html - 获取输入)
*User : sriprem
Email : sri
Phone : 1323sri
Blog : nice*
它显示了我的“props.properties”文件中的错误,但它并没有显示直接获取我的所有输入!
Success.html
Welcome Mr. sriprem,
Validations Success..!
You Entered
Email prem
Phone 1234sri
Website nice
在上述情况下,一切都是错误的格式,但它接受数据!谁能告诉我我的验证出了什么问题。