日期:2014-05-17  浏览次数:20663 次

html_errors标签单独显示各个错误消息
public class UserForm extends ActionForm {

private static final long serialVersionUID = 1L;

private User user = new User();
private String password2;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

// ALT + SHIFT + S + R
public String getPassword2() {
return password2;
}

public void setPassword2(String password2) {
this.password2 = password2;
}

@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
String name1 = user.getUsername();
if(name1 != null)
name1 = name1.trim();
if("".equals(name1))
errors.add("username", new ActionMessage("error.username"));
if(!user.getPassword().equals(password2)) {
errors.add("password2", new ActionMessage("error.password2"));
}
return errors;
}
}


<form action="<html:rewrite action="/RegUser"/>">
 
user:<input type="text" name="user.username" value="${userForm.user.username}"/> <html:errors property="username" /><br>
 
password:<input type="password" name="user.password" value="${userForm.user.password}" /><br>
 
password1:<input type="password" name="password2" value="${userForm.password2}"/><html:errors property="password2" /><br>

以上蓝色部分必须对应上*****

<input type="submit" title="info.submit"  value="<bean:message key='info.submit'/>" />
<html:submit titleKey="info.submit"><bean:message key="info.submit"/> </html:submit>
</form>