zoukankan      html  css  js  c++  java
  • Struts中的<html:messages>, <html:errors>的用法

    Struts提供的<html:messages>和<html:errors>可以很方便的让我们显示信息。他们两个标签不一样的地方是,<html:messages>有点像<logic:iterate>,有id属性,定义了id属性之后,就可以循环用<bean:write>来显示每个消息资源。<html:errors>虽然也可以显示多个消息,但是他是一股脑的将信息显示出来,没有id属性。我们只能通过footer, header, prefix, suffix这四个属性来控制每个消息之间的HTML格式。 

    在后台方面,对应这两个标签,也牵涉了四个方法:saveMessages, saveErrors。这两个方法都有两种参数,一种参数是将ActionMessage, ActionError对象放到request中,一种是把对象放到session中。 

    saveMessages和saveErrors方法的代码基本是一样的,唯一不同的是saveMessages的时候,Struts最终调用的是request/session.setAttribute(Global.MESSAGE_KEY, <our actionmessages object>),而saveErrors最终调用的是request/session.setAttribute(Global.ERROR_KEY, <our actionmessages object>)。也就是说,在set attribute的时候,key不同。 

    这样一来,在使用<html:messages>和<html:errors>的时候,就需要清除的知道这两个标签是以什么key来寻找对象的了。这里就是关键: 

    1. <html:messages>和<html:errors>默认都以Global.ERROR_KEY为key来找寻对象。 

    2. <html:messages>标签在设置了message=true之后,也就是<html:messages message="true">,会已Global.MESSAGE_KEY为key来寻找对象。 

    所以,在EasyCluster V2.0中,我们是这样综合应用上述知识的: 

    1. 在Action类中,如果是带Form的action,Form中的数据用户填写不合法,那么,用saveErrors,然后返回到input page: 

    CODE: SELECT ALL
    ActionMessages msgs = new ActionMessages();
    msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("submitjobaction.jobnamewrong"));
    msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("submitjobaction.emailaddresswrong"));
    this.saveErrors(request, msgs);
    return mapping.getInputForward();


    在jsp中直接<html:errors/>就完事。用prefix,suffix,footer,header来控制显示格式。注意,在把一个ActionMessage add到ActionMessages中的时候,也可以制定一个key,这里用的是ActionMessages.GLOBAL_MESSAGE,<html:errors>标签默认以这个key在ActionMessages中查找各个ActionMessage对象。不要和Global.ERROR_KEY混淆哦,<html:errors>是通过这个Global.ERROR_KEY来先找到ActionMessages对象,然后再用ActionMessages.GLOBAL_MESSAGE这个为key来找到存放在ActionMessages中的各个ActionMessage。

    2. 当Form中用户填写的数据都合法,但是到了业务逻辑验证的时候出错,这是EC 2.0会导向到errorpage.jsp,由于errorpage.jsp中有多个信息显示的地方,比如要显示一个title,还要显示content,而不是一股脑的把信息全显示出来。于是此时是这样做的:

    CODE: SELECT ALL
            // OK, there are no errors and we can invoke the logical functions
            String return_value = sjb.SubmitJob(username, jobinfo);
            if (return_value != null) {
                log.info("Portal SubmitJob Action's SubmitJob part Failed! Please Check system log");
                ActionMessages errors = new ActionMessages();
                errors.add("errortitle", new ActionMessage("submitjobaction.submit.errortitle"));
                errors.add("errordetail", new ActionMessage(return_value));
                this.saveErrors(request, errors);
                return mapping.findForward("ErrorPage");
            }


    注意,这里我们add ActionMessage的时候,不再用ActionMessage.GLOBAL_MESSAGE为key,而是用了我们自定义的key--errortitle, errordetail。于是,jsp中就要这样写:

    CODE: SELECT ALL
               <%-- Error Information display BEGIN --%>
                <table width="100%" cellpadding=0 cellspacing=0 border=0>
                  <tr>
                    <td background="../image/bg_titlebar_c_l.gif" width=17 height=26 NOWRAP>&nbsp;</td>
                    <Td background="../image/bg_titlebar.gif" width="100%" HEIGHT="26" NOWRAP ALIGN="left">
                      &nbsp;&nbsp;<font color=#F7D12A><b>
                          <!-- html:messages tag use Global.ERROR_KEY as default javabean's name so we shouldn't set 'message=true' -->
                          <html:messages id="errormsg" property="errortitle">
                              <bean:write name="errormsg" filter="false"/>
                          </html:messages>
                      </b></font>
                    </TD>
                    <td background="../image/bg_titlebar_c_r.gif" width=17 height=26 NOWRAP>&nbsp;</td>
                  </tr>
                  <tr>
                    <td class="portaltd" colspan=3>
                      <table width="95%" align="center" cellpadding="5" cellspacing="0" border="0" bgcolor=#d7f2f4>
                        <tr><td height=15 colspan=3></td></tr>
                        
                        <tr>
                          <td align=left nowrap><html:img pageKey="errorpage.errico"/></td>
                          <td align=left nowrap><bean:message key="errorpage.description"/></td>
                          <td width=100%>&nbsp;</td>
                        </tr>
                        
                        <tr><td height=15 colspan=3></td></tr>
                        
                        <html:messages id="errormsgdetail" property="errordetail">
                        <tr>
                          <td align="left" colspan="3">
                            <font color=red style="{font-size:10.5pt}"><bean:write name="errormsgdetail" filter="false"/></font>
                          </td>
                        </tr>
                        </html:messages>
                        
                        <tr><td height=15 colspan=3></td></tr>
                                            
                        <tr><td align=left colspan="3">
                          <font style="{font-size:9pt}">
                            <bean:message key="errorpage.todohint"/>
                          </font>
                          <html:link href="mailto:support@jointforce.com.cn">
                            <font style="{font-size:9pt}" color=red><bean:message key="errorpage.bugreport"/></font>
                          </html:link>
                          <font style="{font-size:9pt}">
                            <bean:message key="errorpage.todohint2"/>
                          </font>
                        </td></tr>
                        
                        <tr><td height=30 colspan=3></td></tr>
                      </table>
                    </td>
                  </tr>
                </table>
                <%-- Error Information display END --%>


    注意上述代码中使用<html:messages>的部分,由于html:messages有了id这个属性,所以我们就可以像使用<logic:iterate>的样子来使用。将errortitle和errordetail对应的不同信息分开显示了。

    3. 当操作成功,返回successpage.jsp的时候,用法和上面一样,只不过不能用saveErrors了,要用saveMessages方法,同时在jsp中使用<html:messages>的时候,加上message="true"的定义即可。

    总结一下:

    1. 首先生成ActionMessage/ActionError对象,将消息文本定义好。而且这些文本都支持参数的哦!比如:
    CODE: SELECT ALL
            // Submit Job Success
            ActionMessages messages = new ActionMessages();
            messages.add("msgtitle", new ActionMessage("submitjobaction.success.title"));
            messages.add("msgdetail", new ActionMessage("submitjobaction.success.detail", sjb.jobID));
            messages.add("msgdetail", new ActionMessage("submitjobaction.success.jobprocdetail", sjb.procNumMax));
            this.saveMessages(request, messages);
            return mapping.findForward("SuccessPage");


    在new ActionMessage的时候,将参数加入,记得在Message Resource定义的文本中,用{0}这样的来表示参数哦。通过参数,我们就可以不光在Message中显示纯文本了,也可以显示一些变量了。 

    2. 然后将ActionMessage/ActionError add到ActionMessages/ActionErrors中,可以用自己定义的key来add哦! 
    3. 调用saveMessages, saveErrors 
    4. 在JSP中使用<html:errors>, <html:messages> 

  • 相关阅读:
    win10和Ubuntu双系统安装过程中遇到的问题
    python中矩阵的用法
    python中yield的用法
    python中的*和**的用途
    python函数后面有多个括号怎么理解?
    keras中的重要函数
    机器学习的常见算法
    agent page
    Spring常用注解介绍【经典总结】
    Spring配置中<context:annotation-config> VS <context:component-scan>
  • 原文地址:https://www.cnblogs.com/super119/p/1934989.html
Copyright © 2011-2022 走看看