Struts中的error handling,html:messages 與html:errors的顯示錯(cuò)誤消息的區(qū)別
在struts中,對(duì)Form和Action等錯(cuò)誤信息在頁面中的顯示非常方便,通過<html:messages/>與<html:errors/>
標(biāo)簽都能完成。而他們?cè)谡Z法上的區(qū)別也很小,舉例說明:<html:messages/>
其中id屬性可以隨便取,但bean:wirte的name要與id一樣,顯示全部錯(cuò)誤信息就不要Property屬性,顯示特定的出錯(cuò)信息就要指定property屬性
<html:errors/>
頁面顯示:
最后:html:messages是1.1以后出現(xiàn)的,也是推薦使用的;
標(biāo)簽都能完成。而他們?cè)谡Z法上的區(qū)別也很小,舉例說明:<html:messages/>
ActionMessages message = new ActionMessages();
//從Request范圍獲得出錯(cuò)頁面的資源文件屬性
MessageResources messageResources = getResources(request);
//判斷是否出錯(cuò),出錯(cuò)就添加到message對(duì)象中,其中add的第一個(gè)屬性用于標(biāo)識(shí)不同的錯(cuò)誤信息,第二個(gè)屬性是得到錯(cuò)誤提 示字符串,第一個(gè)屬性與顯示頁面的Property對(duì)應(yīng),如果設(shè)為一樣就把錯(cuò)一起顯示出來
if(bookEditForm.getAuthor().equals(""))
message.add("author", new ActionMessage(
"error.field", messageResources.getMessage("label.author", request)));
if(bookEditForm.getTitle().equals(""))
message.add("author2", new ActionMessage(
"error.field", messageResources.getMessage("label.title", request)));
//判斷是否有錯(cuò)誤,并跳轉(zhuǎn)
if(!message.isEmpty()){
//在Request范圍保存錯(cuò)誤消息
saveMessages(request, message);
return mapping.findForward("showEdit");
}
頁面顯示://從Request范圍獲得出錯(cuò)頁面的資源文件屬性
MessageResources messageResources = getResources(request);
//判斷是否出錯(cuò),出錯(cuò)就添加到message對(duì)象中,其中add的第一個(gè)屬性用于標(biāo)識(shí)不同的錯(cuò)誤信息,第二個(gè)屬性是得到錯(cuò)誤提 示字符串,第一個(gè)屬性與顯示頁面的Property對(duì)應(yīng),如果設(shè)為一樣就把錯(cuò)一起顯示出來
if(bookEditForm.getAuthor().equals(""))
message.add("author", new ActionMessage(
"error.field", messageResources.getMessage("label.author", request)));
if(bookEditForm.getTitle().equals(""))
message.add("author2", new ActionMessage(
"error.field", messageResources.getMessage("label.title", request)));
//判斷是否有錯(cuò)誤,并跳轉(zhuǎn)
if(!message.isEmpty()){
//在Request范圍保存錯(cuò)誤消息
saveMessages(request, message);
return mapping.findForward("showEdit");
}
其中id屬性可以隨便取,但bean:wirte的name要與id一樣,顯示全部錯(cuò)誤信息就不要Property屬性,顯示特定的出錯(cuò)信息就要指定property屬性
<html:messages id="haha" property="author" message="true">
<font style="font-weight:bold; color=#FF0000">
<bean:write name="haha" />
</font><br>
</html:messages>
<font style="font-weight:bold; color=#FF0000">
<bean:write name="haha" />
</font><br>
</html:messages>
<html:errors/>
ActionErrors actionErrors = new ActionErrors();
MessageResources messageResources = getResources(request);
if(bookEditForm.getAuthor().equals(""))
actionErrors.add("author", new ActionError(
"error.field", messageResources.getMessage("label.author", request)));
if(bookEditForm.getTitle().equals(""))
actionErrors.add("author2", new ActionError(
"error.field", messageResources.getMessage("label.title", request)));
if(!actionErrors.isEmpty()){
saveErrors(request, actionErrors);
return mapping.findForward("showEdit");
}
MessageResources messageResources = getResources(request);
if(bookEditForm.getAuthor().equals(""))
actionErrors.add("author", new ActionError(
"error.field", messageResources.getMessage("label.author", request)));
if(bookEditForm.getTitle().equals(""))
actionErrors.add("author2", new ActionError(
"error.field", messageResources.getMessage("label.title", request)));
if(!actionErrors.isEmpty()){
saveErrors(request, actionErrors);
return mapping.findForward("showEdit");
}
頁面顯示:
<html:errors property="author"/>
//或者
<html:errors />
//或者
<html:errors />
最后:html:messages是1.1以后出現(xiàn)的,也是推薦使用的;
posted on 2007-09-13 14:48 月芽?jī)?/a> 閱讀(702) 評(píng)論(0) 編輯 收藏 所屬分類: J2EE學(xué)習(xí)心得