struts 處理流程是 jsp --> ActionForm 中的ActionErrors validate-->驗證通過後執行 action ,否則傳回頁面,顯示錯誤資訊!
例子:
package org.myrose.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class LoginForm extends ActionForm {
private String password;
private String id;
private String validatecode;
public String getValidatecode() {
return validatecode;
}
public void setValidatecode(String validatecode) {
this.validatecode = validatecode;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if(id == null || id.trim().equals("")){
errors.add("login_id_null",new ActionMessage("login.id.null"));
return errors;
}else if(id.length() != 20){
errors.add("login_id_length",new ActionMessage("login.id.length"));
return errors;
}else if(password == null || password.trim().equals("")){
errors.add("login_password_null",new ActionMessage("login.password.null"));
return errors;
}else if(password.length() != 6){
errors.add("login_password_length",new ActionMessage("login.password.length"));
return errors;
}else if(!validatecode.equals(request.getSession().getAttribute("rand").toString())){
errors.add("login_validatecode_error", new ActionMessage("login.validatecode.error"));
return errors;
}
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
轉自:http://hi.baidu.com/630270730/item/e946aa5f8af0bf434eff2083
一、簡介ActionErrors與ActionMessages
ActionErrors是ActionMessages的子類,包括ActionMessage,他們都實作了接口java.io.serializable。原來ActionError是ActionMessage的子類,後來在struts1.3中已被棄用,估計原因是ActionMessage能滿足要求,并且 ActionErrors亦能用作ActionMessage的容器,是以ActionError顯得多餘。
ActionErrors和ActionMessages都可作為ActionMessage的容器,ActionMessages執行個體也可以互為容器,甚至ActionErrors可以做父類執行個體ActionMessages的容器(這裡展現了“裡氏代換”原則——父類出現的地方,子類是可以安全替代的),既可以有errors.add(msgs)。
二、例子
先來看一個簡單例子
1、資源檔案錯誤資訊來源(其格式為 key = value )
error.test = this is a test error.
2、JSP頁面中用于顯示錯誤資訊标簽
<html:errors property="testerror"/>
3、ActionFormBean的validate()方法中産生錯誤資訊
ActionErrors error = new ActionErrors();
error.add("testerror",new ActionMessage("error.test"))
return error;
這個例子的功能就是在ActionForm Bean的validate()方法中産生一條名為:testerror的錯誤資訊,
錯誤資訊息是資源檔案中key為error.test的值。然後在頁面上用html:errors标簽輸出testerror這條錯誤資訊。
這是最常用的一種功能,所有的錯誤資訊都在資源檔案裡面。
有人會問,錯誤資訊隻能存放在資源檔案中嗎,其實不是這樣。不需要資源檔案也可以産生錯誤資訊。
1)ActionMessage(String key,boolean isresource)
如果isresource值為true,則表示key是資源檔案中的key,産生的消息就是與key相對應的消息
如果isresource值為false,則表示key為一條普通的消息。
如果上面的error.add改為error.add("testerror",new ActonMessage("這是一條自定義消息",false",));
那麼頁面上顯示的将是:這是一條自定義消息.
2)另外還可以用ActionMessage産生複合消息,比如我們要輸出:xxx不能用作使用者名,其中xxx是一個變量。
首先我們在資源檔案中加一個條複合消息
testmsg = {0}不能用作使用者名。這裡{0}是要被替換的參數。
我們再來看一下ActionMessage的另一中構造方法
ActionMessage(String key,Object value0);
也就是說用value0的值來替換{0}
我們修改error.add為error.add("testerror",new ActonMessage("testmsg","毛澤東"))
那麼JSP頁面上将顯示:毛澤東不能用作使用者名。
當然在一條複合消息中也可帶多個參數,參數依次為{0},{1},{2}或更多
例如:loginUser = 使用者名:{0} 姓名:{1} 登入次數:{2}.....
那麼在産生錯誤消息時就用new ActionMessage(String key,Object value0,Object value1,Object value2.....)
或者使用對象數組new ActionMessage(String key,Object[] values)
String[] detail = {"Admin","王晶","12"};
error.add("testerror",new ActionMessage("loginUser",detail))
________________________________________
Note:
Cannot find message resources under key org.apache.struts.action.MESSAGE 錯誤的原因是沒有配置資源檔案
解決辦法: 在struts-config.xml 中加入如下的一段
<message-resources parameter="application" null="false"></message-resources>
-----------------------------------------------------------------------------------------------------------------------
在Action或Form裡生成errors:
在Action中使用:
ActionErrors errors = new ActionErrors();
//建立一個error,topic.show.error要在application.properties檔案中聲明
ActionError error1 = new ActionError("topic.show.error");
//加入到errors ,content為鍵
errors.add("content", error1);
//建立多個error
ActionError error2 = new ActionError("topic.show.error");
ActionError error3 = new ActionError("topic.show.error");
errors.add("error2", error2);
errors.add("error2", error3);
//Action傳回值是Forward,是以要儲存此errors
this.saveErrors(request, errors);
在JSP端就可以用:
<html:errors property="content" />
來顯示error1這個錯誤。
若用<html:erros />則顯示出所有的錯誤
三、詳細講解
ActionMessages以一個HashMap存儲ActionMessage.Map中的key是一個辨別,其對應的value是一個List對象,所有的ActionMessage存儲在List中.也就是說key辨別了一組ActionMessage.
1、 ActionMessage
1)ActionMessage(String key)該方法接受一個字元串,字元串是在資源檔案種配置的key值,必須在配置檔案中進行相關配置.
2)ActionMessage(String key,Object value)
3)ActionMessage(String key,Object value0,Object value1)
4)ActionMessage(String key,Object value0,Object value1,Object value2)
5)ActionMessage(String key,Object value0,Object value1,Object value2,Object value3)
上面4個方法第一個參數同樣是資源檔案中配置的key值,同樣必須在配置檔案中進行相關配置.
後面的參數為資源檔案中key所對應的信 息中需要的參數 ActionMessage(String key,Object[] values)
這種方法第一個參數同上,第二個參數接受一個Object數組,其中儲存key在資源檔案中對應資訊需要的參數.
6)ActionMessage(String msg,false )這個方法顯示自定義消息,即消息輸出内容為msg。
7)getKey()傳回ActionMessage的key,傳回類型:String。
8)getValues()傳回ActionMessage中設定的資源檔案參數,傳回類型:Object[]。
2、ActionMessages
ActionMessages.add(String property,ActionMessage message)
第一個參數property對應于<html:messages>标簽中的property屬性, property的值一般采用ActionMessages類中的靜态常量ActionMessages.GLOBAL_MESSAGE,也可以自己定義一個key.該方法執行時,先判斷ActionMessages中有沒有該key,如果沒有就新添加對key-List鍵值對;如果有同樣的key,就先擷取該key對應的Value并轉換為List對象,然後将(ActionMessage)message添加進List對象中.
ActionMessages.add(ActionMessages ams)該方法将參數中儲存的ActionMessage合并到調用ActionMessages中.
3、Action及其所有子類
1)addMessages(HttpServletRequest request,ActionMessages messages)
該方法首先檢查request對象中是否有ActionMessages對象,如果有對象,則将接收的ActionMessages合并到request中,如果沒有,用setAttribute(Globals.MESSAGE_KEY,messages)方法将messages添加進去.如果一個Action中需要顯示多條錯誤資訊,推薦使用該方法.
(Globals.MESSAGE_KEY ="org.apache.struts.action.ACTION_MESSAGE")
2)saveMessages(HttpServletRequest request,ActionMessages messages)
該方法儲存messages時,如果request中已經有一個ActionMessages對象,則用新的覆寫原有的.不推薦使用該方法,該方法容易發生丢失資訊的情況.
3)addErrors()和saveErrors()
這兩個方法與addMessages()和saveMessages()相似,不過在HttpServletRequest.setAttribute()時的參數不一樣,這兩個方法是:setAttribute(Globals.ERROR_KEY,messages)添加的.(Globals.ERROR_KEY = "org.apache.struts.action.ERROR")
4)clear()
5)isEmpty()
6)isAccessed()是否已将ActionMessages存入request中。
7)size()存于ActionMessages中的ActionMessage的數量。
8)size(String property)ActionMessages中property鍵對應的List中的ActionMessage的數量。
9)properties()傳回ActionMessages的所有key,并存于Iterator中。
10)get()傳回存于ActionMessages中的所有ActionMessage,并存于Iterator中。
11)get(String property)傳回ActionMessages中property鍵對應的List中的所有ActionMessage,并存于Iterator中。
4、 AtionErrors和ActionMessages的差別
1) ActionErrors是ActionMessages的一個子類,功能幾乎相同,不同點在于标簽<html:errors/>和<html:messages>的使用上的差別。
html:errors指定了footer和header屬性。預設值為errors.header和errors.footer,需要時可以自己指定。如果資源屬性檔案配置了 errors.header和errors.footer,則任何時候使用html:errors時開頭和結尾都是這兩個屬性對應的資源資訊。
而html:message預設情況下沒有errors.header和errors.footer值,當然可以自己指定。
2)html:errors可以根據property屬性指定顯示一個錯誤資訊。html:messages有一個必添項id。html:messages不能直接顯示資訊,它将選出的資訊放入一個用id辨別的Iterator對象裡,然後在用ben:write或JSTL c:out标簽顯示每個資訊.例如:
<html:messages message="true" id="msg">
<c:out value="${msg}"/><br />
</html:messages>