struts2.16 数据查验

struts2.16 数据检验
public class UserAction extends ActionSupport {
	private String name;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
	public String add2(){
		if(name==null || !name.equals("admin")){
			this.addFieldError("name", "name is error");
			this.addFieldError("name", "name is error2");
			this.addFieldError("pwd", "pwd is error");
			return ERROR;
			
		}
		
		return SUCCESS;
	}
	
	
	
}

 页面:

<body>
	User add2 Error!
	<s:fielderror fieldName="name" theme="simple"/>
	<br/>
	<s:property value="errors.name[0]"/>
	<s:debug></s:debug>
</body>

 从Struts ValueStack Debug分析数据: (打开<s:debug></s:debug>标签)

 

Object Property Name Property Value 
com.bjsxt.struts2.user.action.UserAction texts null 

actionErrors [] 

errors {name=[name is error, name is error2], pwd=[pwd is error]} 

fieldErrors {name=[name is error, name is error2], pwd=[pwd is error]} 

errorMessages [] 

name a 

locale zh_CN 

actionMessages [] 

com.opensymphony.xwork2.DefaultTextProvider texts null 

 

 其实在开发时只要打开<s:debug></s:debug>标签就很清楚ValueStack里的内容,

从上面数据分析可以看出ValueStack存放数据的方式,ValueStack里存放不同的map,map的value存放集合,上面的eror.name里就有两个元素.

 

<s:property value="errors.name[0]"/>这个标签很好用,可能取到ValueStack里的所有值,例如<s:property value="fieldErrors.name"/>这样也可以