Struts2 学习笔记5--Validate
数据校验
1、简单校验
使用addFieldError方法和s:fieldError标签简单处理数据校验。
将错误信息作为field加入
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;
??? }
}
jsp中标签设置:
<body>
??? User Add Error!
??? <s:fielderror fieldName="name" theme="simple"/>
??? <br />
??? <s:property value="errors.name[0]"/>
??? <s:debug></s:debug>
</body>
错误信息放入到值栈中,显示调试值栈状态用标签<s:debug></s:debug>。如下图:
?
?
查找struts标签库
?
?
?