package com.zchen.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private String name;
public String add() {
if(name == null || !name.equals("admin")) {
this.addFieldError("name", "name is error");
this.addFieldError("name", "name is too long");
return ERROR;
}
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
打印出这个错误信息:
1.<s:fielderror fieldName="name" theme="simple"/>
当调用this.addFieldError("name", "name is error");会把错误信息加入到栈中<s:debug></s:debug>可以看到。
2.<s:property value="errors.name[0]"/>也可以打印出来。