Struts1.2.4学习心得!(五)

本次博客记录第五次学习内容,主要围绕Java程序中错误和信息的处理。先在资源文件如MessageResources.properties中定义错误和普通信息,还在程序(以JSP为例)中定义错误和信息类,并展示了显示错误和信息的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第五次学习

1.错误和信息的处理.

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

  
  #
  # Resources for testing <html:errors> tag.
  #
  
  errors.header=<table>
  errors.footer=</table>
  errors.prefix=<tr><td>
  errors.suffix=</td></tr>
 10 
 11 property1error1=Property 1, Error 1
 12 property2error1=Property 2, Error 1
 13 property2error2=Property 2, Error 2
 14 property2error3=Property 2, Error 3
 15 property3error1=Property 3, Error 1
 16 property3error2=Property 3, Error 2
 17 globalError=Global Error
 18 
 19 #
 20 # Resources for testing <html:messages> tag.
 21 #
 22 
 23 messages.header=<table>
 24 messages.footer=</table>
 25 
 26 property1message1=Property 1, Message 1
 27 property2message1=Property 2, Message 1
 28 property2message2=Property 2, Message 2
 29 property2message3=Property 2, Message 3
 30 property3message1=Property 3, Message 1
 31 property3message2=Property 3, Message 2
 32 globalMessage=Global Message
 33 



在程序中定义错误和信息类,这个例子写在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"));
 10       errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("globalError"));
 11       request.setAttribute(Globals.ERROR_KEY, errors);
 12 
 13       ActionMessages messages = new ActionMessages();
 14       messages.add("property1", new ActionMessage("property1message1"));
 15       messages.add("property2", new ActionMessage("property2message1"));
 16       messages.add("property2", new ActionMessage("property2message2"));
 17       messages.add("property2", new ActionMessage("property2message3"));
 18       messages.add("property3", new ActionMessage("property3message1"));
 19       messages.add("property3", new ActionMessage("property3message2"));
 20       messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("globalMessage"));
 21       request.setAttribute(Globals.MESSAGE_KEY, messages);
 22     %>
 23 





显示错误:
java代码: 

 1 
 2  <html:errors property="property1" />
 3  <html:errors property="property2" />
 4 


显示信息:
java代码: 

  
  <html:messages property="property1" message="true" id="msg" header="messages.header" footer="messages.footer">
              <tr>
                <td>
                       <%= pageContext.getAttribute("msg") %>
                </td>
              </tr>
            </html:messages>
  
 10 <html:messages message="true" id="msg" header="messages.header" footer="messages.footer">
 11             <tr>
 12               <td>
 13                <%= pageContext.getAttribute("msg") %>
 14               </td>
 15             </tr>
 16 </html:messages>
 17 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值