龙行天下

  政 博
随笔 - 23, 文章 - 0, 评论 - 12, 引用 - 0
数据加载中……

struts错误和信息的处理

1. 错误和信息的处理 .

首先在资源文件中定义错误信息和普通信息 . :MessageResources.properties 中定义如下 :

java 代码



#
# Resources
for testing <html:errors> tag.
#

errors.header=<table>
errors.footer=</table>
errors.prefix=<tr><td>
errors.suffix=</td></tr>

property1error1=Property 1,
Error 1
property2error1=Property 2,
Error 1
property2error2=Property 2,
Error 2
property2error3=Property 2,
Error 3
property3error1=Property 3,
Error 1
property3error2=Property 3,
Error 2
globalError=Global
Error

#
# Resources
for testing <html:messages> tag.
#

messages.header=<table>
messages.footer=</table>

property1message1=Property 1, Message 1
property2message1=Property 2, Message 1
property2message2=Property 2, Message 2
property2message3=Property 2, Message 3
property3message1=Property 3, Message 1
property3message2=Property 3, Message 2
globalMessage=Global Message

 



在程序中定义错误和信息类 , 这个例子写在 JSP

java 代码



<%
      ActionErrors errors =
new ActionErrors();
      errors.add("property1",
new ActionError("property1error1"));
      errors.add("property2",
new ActionError("property2error1"));
      errors.add("property2",
new ActionError("property2error2"));
      errors.add("property2",
new ActionError("property2error3"));
      errors.add("property3",
new ActionError("property3error1"));
      errors.add("property3",
new ActionError("property3error2"));
      errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("globalError"));
      request.setAttribute(Globals.ERROR_KEY, errors);

      ActionMessages messages =
new ActionMessages();
      messages.add("property1",
new ActionMessage("property1message1"));
      messages.add("property2",
new ActionMessage("property2message1"));
      messages.add("property2",
new ActionMessage("property2message2"));
      messages.add("property2",
new ActionMessage("property2message3"));
      messages.add("property3",
new ActionMessage("property3message1"));
      messages.add("property3",
new ActionMessage("property3message2"));
      messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("globalMessage"));
      request.setAttribute(Globals.MESSAGE_KEY, messages);
    %>

 





显示错误 :

java 代码



<html:errors property="property1" />
<html:errors property="property2" />

 


显示信息 :

java 代码



<html:messages property="property1" message="
true " id="msg" header="messages.header" footer="messages.footer">
            <tr>
              <td>
                     <%= pageContext.getAttribute("msg") %>
              </td>
            </tr>
          </html:messages>

<html:messages message="
true " id="msg" header="messages.header" footer="messages.footer">
            <tr>
              <td>
               <%= pageContext.getAttribute("msg") %>
              </td>
            </tr>
</html:messages>

 

 

posted on 2006-05-03 20:02 feingto 阅读(1016) 评论(0)  编辑  收藏 所属分类: Java Web


只有注册用户登录后才能发表评论。


网站导航: