我是春季验证的新手。我有一个带有4个文本字段的 loginform.html ,我试图验证它们但验证无法正常工作形成。 但它接受我输入的每一个值。
例如:如果我输入了不正确的电子邮件,它应该会给我一个验证错误,例如“输入正确的电子邮件ID ”
loginform.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}
的login.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>
ContactController.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";
}
}
弹簧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>
success.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>
props.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
在上面的场景中,一切都是错误的格式,但它接受了数据!任何人都可以告诉我我的验证有什么问题。
答案 0 :(得分:0)
按照弹簧文档:
http://docs.spring.io/spring/docs/3.0.0.RC3/reference/html/ch05s07.html
您是否配置了Bean验证实施?看一下 5.7.3配置DataBinder