由于Struts2框架总是把用户的输入信息封装到Action的属性中,
所以若果要进行输入校验,Action类必须继承ActionSupport类,使用ActionSupport中的方法进行输入校验。
1、public void validate()
该方法往往被Action类覆盖,实现对输入参数的校验。
2、public void addActionError(String anErrorMessage)
该方法将Action级别的错误信息添加到Action中。
3、public void addActionMessage(String aMessage)
该方法将Action级别的消息添加到Action中。
4、public void addFieldError(String fieldName,String errorMessage)
该方法将域级错误消息添加到特定的域中。
5、public String getText(String key,String[] args)
从国际化资源文件中获取属性值,其中参数是属性文件的key值
6、public String getText(String key,String[] args)
从国际化资源文件中获取属性值,其中key是属性文件的key值,args对应国际化资源文件中的参数。
如果Action类中通过addActionError或者addFieldError方法添加了错误信息,那么就称为Action类校验失败。
校验失败后Struts2框架将自动跳转到Action类的名字为input的result视图上。
<action name="Register" class="Action.RegisterAction" >
<result name="regsuccess">/index.jsp</result>
<result name="regfail" >/register.jsp</result>
<result name="input" >/register.jsp</result>
</action>
使RegisterAction类继承ActionSupport类
package Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import dao.Impl.CustomerDAOImpl;
import Service.CustomerServiceImpl;
import VO.Customer;
public class RegisterAction extends ActionSupport implements ModelDriven<Customer>{
private String custname;
private String pwd;
private Integer age;
private String address;
private Customer cust = new Customer(custname, pwd);
public String getCustname() {
return custname;
}
public void setCustname(String custname) {
this.custname = custname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String register(){
CustomerServiceImpl cs = new CustomerServiceImpl();
cs.setDao(new CustomerDAOImpl());
try{
cs.register(new Customer(custname,pwd,age,address));
return"regsuccess";
}catch(Exception e) {
e.printStackTrace();
return "regfail";
}
}
@Override
public void validate(){
String custname=cust.getCustname();
String pwd = cust.getPwd();
if(custname==null||custname.equals("")){
this.addFieldError("custname", "Pls input your name.");
}
if(pwd==null||pwd.equals("")){
this.addFieldError("pwd", "Pls input your password.");
}
}
@Override
public Customer getModel() {
return cust;
}
}
配置struts.xml
<action name="Register" class="Action.RegisterAction" >
<result name="regsuccess">/index.jsp</result>
<result name="regfail" >/register.jsp</result>
<result name="input" >/register.jsp</result>
</action>
输入对应信息,执行注册按钮
显示结果如下:
如果需要再JSP页面某处集中显示所有的域级错误信息,而不是分别在对应域的位置显示错误信息,那么可以使用<s:fielderror>标签是实现。
代码如下:
<body>
<s:fielderror></s:fielderror>
<s:text name="register.info"></s:text><br>
<s:form action="Register">
<s:textfield name="custname" key="custname.label"></s:textfield>
<s:password name="pwd" key="pwd.label"></s:password>
<s:textfield name="age" key="age.label"></s:textfield>
<s:textfield name="address" key="address.label"></s:textfield>
<s:submit name="button" key="register.button"></s:submit>
</s:form>
</body>
执行显示结果:
使用Action级别错误
修改RegisterAction
public String register() {
CustomerServiceImpl cs = new CustomerServiceImpl();
cs.setDao(new CustomerDAOImpl());
try{
cs.register(cust);
return "regsuccess";
}catch (RegisterException e) {
this.addActionError("Sorry,custname already existed.");
e.printStackTrace();
return "regfail";
}
}
在JSP中使用actionerror标签即可方便显示所有的Action级别信息,如修改register.jsp文件
<body>
<s:actionerror/>
<s:text name="register.info"></s:text><br>
<s:form action="Register">
<s:textfield name="custname" key="custname.label"></s:textfield>
<s:password name="pwd" key="pwd.label"></s:password>
<s:textfield name="age" key="age.label"></s:textfield>
<s:textfield name="address" key="address.label"></s:textfield>
<s:submit name="button" key="register.button" method="register"></s:submit>
</s:form>
</body>
使用Action消息
很多时候,在Web应用中需要一些友好的提示信息,而不是错误信息。例如,如注册成功后跳转到登陆页面,提示“注册成功,请登录”的信息
修改RegisterAction
public String register() {
CustomerServiceImpl cs = new CustomerServiceImpl();
cs.setDao(new CustomerDAOImpl());
try{
cs.register(cust);
this.addActionMessage("Register successfully,login pls.");
return "regsuccess";
}catch (RegisterException e) {
this.addActionError("Sorry,custname already existed.");
e.printStackTrace();
return "regfail";
}
}
修改登陆成功后的index.jsp页面
<body>
<s:actionmessage/>
<s:form action="Customer!login">
<s:textfield name="custname" label="Input your custname"></s:textfield><br>
<s:password name="pwd" label="Input your password"></s:password><br>
<s:submit value="Login"></s:submit>
</s:form>
</body>