spring jsp error
I am trying to make a simple login form with Spring MVC. I should say that I am new to this.The idea is that I am trying to validate the form (using a class that implements Validator interface) and if something goes wrong tu print the errors.
The validate method is:
public void validate(Object target, Errors errors)
{
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "required.username","Va rugam introduceti un nume de utilizator!");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "parola", "required.parola","Va rugam introduceti o parola!");
User user= (User) target;
System.out.println(" a:"+user.getUsername()+" b:"+user.getParola());
User u=userDAO.findByUsername(user.getUsername());
if(u == null)
{
errors.reject("invalid.username","Nume utilizator incorect!");
//System.out.println("NULL");
}
else if(!u.getParola().equals(user.开发者_如何转开发getParola()))
{
errors.reject("invalid.parola","Parola incorecta!");
}
}
The problem is with my jsp page :
<form:form method="POST" commandName="user" >
<form:errors path="*" cssClass="error" />
<table>
<tr>
<td>Username :</td>
<td><form:input path="username" /></td>
<td><form:errors path="username" cssClass="error" /></td>
<td><form:errors path="invalid.username" cssClass="error" /></td>
</tr>
<tr>
<td>Password :</td>
<td><form:password path="parola" /></td>
<td><form:errors path="parola" cssClass="error" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Login"></td>
</tr>
</table>
</form:form>
If I try to run this I get the error:
org.springframework.beans.NotReadablePropertyException: Invalid property 'invalid' of bean class [pachet.dbo.User]: Bean property 'invalid' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
If I delete the line :
<td><form:errors path="invalid.username" cssClass="error" /></td>
the line
<form:errors path="*" cssClass="error" />
shows me the "invalid.username" error,so the error exists..maybe the path isn't right?
There are two options for rejection:
- reject a particular field (field error)
- reject the whole submission (global error)
You are messing the two:
reject(..)
registers a global error,rejectValue(..)
registers a field error.- the
path
attribute of<form:error>
should be a path through the fields of the model attribute. You obviously don't have aninvalid
model attribute. Yours isuser
The way to fix this is to use:
errors.rejectValue("username", "invalid.username", "....");
and have:
<form:error path="username" .. />
精彩评论