<html:messages>的message屬性如果設(shè)定為true,會輸出ActionMessages中所儲存的訊息,Message表示一個提示訊息,也許使用者輸入了不正確的資訊,例如在輸入名稱與密碼時打錯了字,程式要提示使用者他們輸入了不正確的訊息。
<html:messages>的message屬性如果不設(shè)定為true,會輸出ActionErrors中所儲存的訊息,Error代表的是一個操作方面的錯誤,例如錯誤操作導(dǎo)致使用者名稱或密碼為空(當(dāng)然也許也是故意的)。
示例:(struts 1.3)
ActionForm中:
ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("messages.username.required"));
addMessages(request,messages);
ActionErrors errors = new ActionErrors();
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.username.required"));
addErrors(request,errors);
return mapping.getInputForward();
===========================================================================
JSP頁面顯示:
<html:messages id="outMessage" message="true" header="messages.header" footer="messages.footer">
<bean:write name="outMessage"/>
</html:messages>
<html:messages id="outError" message="false" header="errors.header" footer="errors.footer">
<bean:write name="outError"/>
</html:messages>
============================================================================
ApplicationResources.properties配置文件:
messages.header=<h2><font color="red">
messages.footer=</font></h2>
messages.username.required=ActionMessage:the name is null
errors.header=<h2><font color="blue">
errors.footer=</font></h2>
errors.username.required=ActionError:the name is null