在Struts2中最簡(jiǎn)單的驗(yàn)證數(shù)據(jù)的方法是使用validate。我們從ActionSupport類的源代碼中可以看到,ActionSupport類實(shí)現(xiàn)了一個(gè)Validateable接口。這個(gè)接口只有一個(gè)validate方法。如果Action類實(shí)現(xiàn)了這個(gè)接口,Struts2在調(diào)用execute方法之前首先會(huì)調(diào)用這個(gè)方法,我們可以在validate方法中驗(yàn)證,如果發(fā)生錯(cuò)誤,可以根據(jù)錯(cuò)誤的level選擇字段級(jí)錯(cuò)誤,還是動(dòng)作級(jí)錯(cuò)誤。并且可使用addFieldError或addActionError加入相應(yīng)的錯(cuò)誤信息,如果存在Action或Field錯(cuò)誤,Struts2會(huì)返回“input”(這個(gè)并不用開(kāi)發(fā)人員寫,由Struts2自動(dòng)返回),如果返回了“input”,Struts2就不會(huì)再調(diào)用execute方法了。如果不存在錯(cuò)誤信息,Struts2在最后會(huì)調(diào)用execute方法。
這兩個(gè)add方法和ActionErrors類中的add方法類似,只是add方法的錯(cuò)誤信息需要一個(gè)ActionMessage對(duì)象,比較麻煩。除了加入錯(cuò)誤信息外,還可以使用addActionMessage方法加入成功提交后的信息。當(dāng)提交成功后,可以顯示這些信息。
以上三個(gè)add方法都在ValidationAware接口中定義,并且在ActionSupport類中有一個(gè)默認(rèn)的實(shí)現(xiàn)。其實(shí),在ActionSupport類中的實(shí)現(xiàn)實(shí)際上是調(diào)用了ValidationAwareSupport中的相應(yīng)的方法,也就是這三個(gè)add方法是在ValidationAwareSupport類中實(shí)現(xiàn)的,代碼如下:
private final ValidationAwareSupport validationAware = new ValidationAwareSupport();
public void addActionError(String anErrorMessage)
{ validationAware.addActionError(anErrorMessage);
}
public void addActionMessage(String aMessage)
{
validationAware.addActionMessage(aMessage);
}
public void addFieldError(String fieldName, String errorMessage)
{
validationAware.addFieldError(fieldName, errorMessage);
}
下面我們來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的驗(yàn)證程序,來(lái)體驗(yàn)一個(gè)validate方法的使用。
先來(lái)在Web根目錄建立一個(gè)主頁(yè)面(validate.jsp),代碼如下:
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>輸入操作數(shù)</title>
<style type="text/css">
.label {
font-style: italic;
}
.errorLabel {
font-style: italic;
color: red;
}
.errorMessage {
font-weight: bold;
color: red;
}
</style>
</head>
<body>
求代數(shù)和
<br />
<s:form action="First">
<s:textfield name="operand1" label=" 操作數(shù)1" />
<s:textfield name="operand2" label=" 操作數(shù)2" />
<s:submit value="代數(shù)和" />
</s:form>
</body>
</html>
在上面的代碼中,使用了Struts2的tag:<s:actionerror>、<s:fielderror>和<s:actionmessage>,分別用來(lái)顯示動(dòng)作錯(cuò)誤信息,字段錯(cuò)誤信息,和動(dòng)作信息。如果信息為空,則不顯示。
現(xiàn)在我們來(lái)實(shí)現(xiàn)一個(gè)動(dòng)作類,代碼如下:
package action;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class FirstAction extends ActionSupport {
private int operand1;
private int operand2;
public String execute() throws Exception {
System.out.println(getFirst() + "http://////////");
if (getFirst() <= 0) // 如果代碼數(shù)和是非負(fù)整數(shù),跳到positive.jsp頁(yè)面
{
return INPUT;
}
return SUCCESS;
}
public int getOperand1() {
return operand1;
}
public void setOperand1(int operand1) {
System.out.println("operand1:" + operand1);
this.operand1 = operand1;
}
public int getOperand2() {
return operand2;
}
public void setOperand2(int operand2) {
System.out.println("operand2:" + operand2);
this.operand2 = operand2;
}
public int getFirst() {
return operand1 + operand2; // 計(jì)算兩個(gè)整數(shù)的代碼數(shù)和
}
}
大家從上面的代碼可以看出,Field錯(cuò)誤需要一個(gè)key(一般用來(lái)表示是哪一個(gè)屬性出的錯(cuò)誤),而Action錯(cuò)誤和Action消息只要提供一個(gè)信息字符串就可以了。
最后來(lái)配置一下這個(gè)Action,代碼如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="First" class="First">
<result name="input">/index.jsp</result>
<result name="success">/positive.jsp</result>
</action>
</package>
</struts>
我們還可以使用ValidationAware接口的其他方法(由ValidationAwareSupport類實(shí)現(xiàn))獲得或設(shè)置字段錯(cuò)誤信息、動(dòng)作錯(cuò)誤信息以及動(dòng)作消息。如hasActionErrors方法判斷是否存在動(dòng)作層的錯(cuò)誤,getFieldErrors獲得字段錯(cuò)誤信息(一個(gè)Map對(duì)象)。下面是ValidationAware接口提供的所有的方法:
package com.opensymphony.xwork2;
import java.util.Collection;
import java.util.Map;
public interface ValidationAware
{
void setActionErrors(Collection errorMessages);
Collection getActionErrors();
void setActionMessages(Collection messages);
Collection getActionMessages();
void setFieldErrors(Map errorMap);
Map getFieldErrors();
void addActionError(String anErrorMessage);
void addActionMessage(String aMessage);
void addFieldError(String fieldName, String errorMessage);
boolean hasActionErrors();
boolean hasActionMessages();
boolean hasErrors();
boolean hasFieldErrors();
}
關(guān)于其他更詳細(xì)的驗(yàn)證規(guī)則,請(qǐng)讀者訪問(wèn)
http://struts.apache.org/2.0.11.1/docs/validation.html來(lái)查看。
struts2驗(yàn)證信息重復(fù)出現(xiàn)解決方案
你的action是不是被spring代管了?
我一看,是啊,我的action是被spring代管了。
接著他又寫道:
你的action class是不是用的bean 的id?
我一邊看還一邊點(diǎn)頭,是啊,我的真是這樣的,怎么這么了解我!(其實(shí)人人都這樣寫)
最后他說(shuō):
把你action 的class改成全路徑,問(wèn)題就會(huì)得到解決。
是嗎?我當(dāng)時(shí)想,那就是說(shuō)不要spring代管了,那我還要向這個(gè)action 注屬性的,這樣改了肯定注不進(jìn)去了。
正如我所說(shuō)的,這樣改了驗(yàn)證的問(wèn)題是得到解決了,可是屬性注不進(jìn)去了。那不是正了一樣又歪了一樣,問(wèn)題并沒(méi)有得到根本性的解決。
正在有點(diǎn)想抓狂中,卻無(wú)意發(fā)現(xiàn)我的這個(gè)bean怎么沒(méi)有寫scope="prototype",這個(gè)屬性是告訴spring,每來(lái)一個(gè)action給我產(chǎn)生一個(gè)新的實(shí)例。就這么簡(jiǎn)單,問(wèn)題得以真正的解決。