01_11_Strtus2简单数据验证
1. 引入struts标签
<%@taglib uri="/struts-tags" prefix="s" %>
2. 配置struts.xml文件
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.sumapay.user.UserAction">
<result>
/user_add_success.jsp
</result>
<result name="error">
/user_add_error.jsp
</result>
</action>
</package>
3. 实现Action类
package com.sumapay.user;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sumapay.domain.User;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String name;
public String add() {
if(name == null || !name.equals("admin")) {
this.addFieldError("name", "name is error");
this.addFieldError("name", "name is not null");
return "error";
}
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
return "success";
}
}
4. struts标签获取后端值
<s:fielderror fieldName="name" theme="simple" />
<!--获取后端报错的信息-->
<br />
<s:property value="errors.name[0]"/>
<!--获取属性值-->
<s:debug />