<form:errors>
标记内时, <form:form>
标记没有显示错误消息。
如果<form:errors>
标记超出<form:form>
标记,则会显示错误消息。我打印了绑定结果的错误,它显示如下错误:
[Field error in object 'student1' on field 'lastname': rejected value []; codes [Size.student1.lastname,Size.lastname,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [student1.lastname,lastname]; arguments []; default message [lastname],16,6]; default message [Size.student1.**lastname**]]
注意:如果我将<form:errors>
置于<form:form>
标记内,则无效。
这是JSP代码:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Contact Manager</title>
<style>
.errStyle{
color:red;}
</style>
</head>
<body>
<h2>${headerName}</h2>
<!-- This line shows the error messages of input binding exception. -->
<!--<form:errors path="student1.*" cssClass="errStyle"/> this line shows the error messages-->
<form:form method="post" action="/SampleTutorials/student/addBean.html">
<label>Last Names</label>
<input type="text" name="lastname"/>
<table>
<tr>
<td><label>First Name</label></td>
<td><input type="text" name="firstname"/></td>
</tr>
<tr>
<td><label>Last Names</label></td>
<td><input type="text" name="lastname"/>
<form:errors path="student1.lastname"> </form:errors>
</td>
</tr><!--**** Here it doesnt shows the error message -->
<tr>
<td><label>DOB</label></td>
<td><input type="text" name="dob"/></td>
</tr>
<tr>
<td><label>Email</label></td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td><label>Telephone</label></td>
<td><input type="text" name="telephone"/></td>
</tr>
<tr>
<td><label>Skillset</label></td>
<td>
<select multiple name="skillSet">
<option value="J2EE">J2EE</option>
<option value="J2SE">J2SE</option>
<option value="Spring">Spring</option>
<option value="Hibernate">Hibernate</option>
</select>
</td>
</tr>
<tr>
<td><label>Flat Number : </label></td>
<td><input type="text" name="address.flatNumber"/></td>
</tr>
<tr>
<td><label>Building Name : </label></td>
<td><input type="text" name="address.buildingName"/></td>
</tr>
<tr>
<td><label>City : </label></td>
<td><input type="text" name="address.city"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Contact"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
控制器方法:
@RequestMapping(value="/addBean.html", method = RequestMethod.POST)
public ModelAndView addContactFromBean(@ModelAttribute("student1") @Valid ContactBean student1, BindingResult result){
System.out.println("Inside addContactFromBean");
if(result.hasErrors()){ // this binding result checks for error
ModelAndView model = new ModelAndView("addStudent");
System.out.println("Some error occured in input.");
System.out.println(result.getAllErrors());
return model;
}
ModelAndView model = new ModelAndView("viewStudent");
System.out.println("Student Bean : "+student1.toString());
model.addObject("student1", student1);
System.out.println("View Name :->> "+model.getViewName());
return model;
}
Spring Bean:
package com.springTut;
import java.util.List;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class ContactBean {
**@NotEmpty
private String firstname;**
**@Size( min=6, max=16, message="Size.student1.lastname")**
**private String lastname;**
private String email;
private String dob;
private Long telephone;
private List<String> skillSet;
private AddressBean address;
@Override
public String toString() {
return "ContactBean [firstname=" + firstname + ", lastname=" + lastname + ", email=" + email + ", telephone="
+ telephone + ", skillSet=" + skillSet + ", address=" + address + "--.]";
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public List<String> getSkillSet() {
return skillSet;
}
public void setSkillSet(List<String> skillSet) {
this.skillSet = skillSet;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getTelephone() {
return telephone;
}
public void setTelephone(Long telephone) {
this.telephone = telephone;
}
public AddressBean getAddress() {
return address;
}
public void setAddress(AddressBean address) {
this.address = address;
}
}
答案 0 :(得分:1)
尝试正确使用弹簧形式作为模型属性对象和我的意思的内部属性。
以您的形式
<form:form method="post" action="/SampleTutorials/student/addBean.html" attribute="student">
...
<td><form:input type="text" path="lastname"/>
<form:errors path="lastname"> </form:errors>
</td>
...
如果在这种情况下也失败了,请告诉我
答案 1 :(得分:1)
绑定和验证错误已在模型中注册。标签从模型中检索这些错误以用于显示目的。如果标记嵌套在form:form标记内,则有效错误键或路径是form:form标签中的modelAttribute属性与您指定的路径的组合。在您的情况下,modelAttribute不是显式的。更改您的表单如下
<form:form method="post" action="/SampleTutorials/student/addBean.html" modelAttribute="student1">
<label>Last Names</label>
<input type="text" name="lastname"/>
<table>
<tr>
<td><label>First Name</label></td>
<td><input type="text" name="firstname"/></td>
</tr>
<tr>
<td><label>Last Names</label></td>
<td><input type="text" name="lastname"/>
<form:errors path="lastname"> </form:errors>
</td>
</tr>
<tr>
<td><label>DOB</label></td>
<td><input type="text" name="dob"/></td>
</tr>
<tr>
<td><label>Email</label></td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td><label>Telephone</label></td>
<td><input type="text" name="telephone"/></td>
</tr>
<tr>
<td><label>Skillset</label></td>
<td>
<select multiple name="skillSet">
<option value="J2EE">J2EE</option>
<option value="J2SE">J2SE</option>
<option value="Spring">Spring</option>
<option value="Hibernate">Hibernate</option>
</select>
</td>
</tr>
<tr>
<td><label>Flat Number : </label></td>
<td><input type="text" name="address.flatNumber"/></td>
</tr>
<tr>
<td><label>Building Name : </label></td>
<td><input type="text" name="address.buildingName"/></td>
</tr>
<tr>
<td><label>City : </label></td>
<td><input type="text" name="address.city"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Contact"/>
</td>
</tr>
</table>
</form:form>