如何自訂Struts2表單驗證後的錯誤資訊顯示格式/樣式

來源:互聯網
上載者:User
Struts 2.1.8.1 + Myeclipse 8.6 + Tomcat 7.0.2 前提

 請大家搭建好Struts2的Web項目,這個應該很簡單,就不在敘述,本篇文章主要做的是如何顯示Struts的錯誤資訊,那麼這裡就建立一個Action類,來類比產生的錯誤。請看下面的Action類。(源碼會在後面提供)

public class DisplayErrorInfoAction extends ActionSupport

{

    private static final long serialVersionUID = -2690064846056775963L;

 

    @Override

    public String execute() throws Exception

    {

       /**

        * 添加一些FieldError供測試,如果需要請自寫validater方法

        */

       this.addFieldError("displayErrorInfo", "error!Please check it.");

       this.addFieldError("user.username", "名字有錯誤啦");

       return INPUT;

    }

}

方案 第一種方案:使用OGNL拿值棧(ValueStack)的內容(重點推薦使用)

主要是通過OGNL來取得Value Stack 中 errors 和fieldErrors的值。這裡先來介紹下s:debug 的標籤。

debug標籤主要用於輔助測試,它在頁面上產生一個超連結,通過該連結可以查看ValueStack和Stack Context 中的所有值資訊。可以通過OGNL 中 s:property來取得相應的值(順利提醒: s:debug 使用請放在 s:form外面,放在裡面有什麼效果,請大家試試就知道了)。附上本案例使用的代碼。

註:請大家關注下error code 包含‘.’(類似於user.username)的取值方法:

<s:property value="errors['user.username'][0]" /> 和 <s:property value="fieldErrors['user.username'][0]" />

(網上“struts2 property標籤的 value屬性” 的解決方案)


    <body>

       <s:form action="displayErrorInfo" method="post" theme="simple">

           <h1 align="center" style="color: blue">

              DisplayErrorInfoForStruts2Demo1(使用OGNL拿值棧的內容,推薦使用)

           </h1>

           <h2 dir="rtl">

              順利整理

           </h2><hr><hr>

           1.1.All ErrorInfos(Map):&nbsp;<s:property value="errors" /><br>

           1.2.All fieldErrorsInfos(Map):&nbsp;<s:property value="fieldErrors" /><br>

           <hr>

           2.1.DisplayErrorInfo in errors(errors.displayErrorInfo[0]):<s:textfield/>

           <font color="red">

              <s:property value="errors.displayErrorInfo[0]" />

           </font><br>

           2.2.DisplayErrorInfo in errors(errors['displayErrorInfo'][0]):<s:textfield/>

           <font color="red">

              <s:property value="errors['displayErrorInfo'][0]" />

           </font>

           <br>

              &nbsp;&nbsp;<font color="green">註:拿到值後,就可以按照自己的格式進行自訂顯示了</font>

           <br>

           2.3.DisplayErrorInfo in errors(一般不會使用到[1],這裡僅是測試):<s:textfield/>

           <font color="red">

              <s:property value="errors.displayErrorInfo[1]" />

           </font>

           <br>

           <hr>

           3.1.DisplayErrorInfo in fieldErrors(fieldErrors.displayErrorInfo[0]):<s:textfield/>

           <font color="red">

              <s:property value="fieldErrors.displayErrorInfo[0]" />

           </font>

           <br>

           3.2.DisplayErrorInfo in fieldErrors(fieldErrors['displayErrorInfo'][0]):<s:textfield/>

           <font color="red">

              <s:property value="fieldErrors['displayErrorInfo'][0]" />

           </font>

           <br>

              &nbsp;&nbsp;<font color="green">註:建議使用fieldErrors取值,在Action中使用的是this.addFieldError</font>

           <br>

           <hr>

           4.0.DisplayErrorInfo - user.username(正確的運算式.errors['user.username'][0]):<s:textfield/>

           <font color="red">

              <s:property value="errors['user.username'][0]" />

           </font>

           <br>

           4.1.DisplayErrorInfo - user.username(正確的運算式.fieldErrors['user.username'][0]):<s:textfield/>

           <font color="red">

              <s:property value="fieldErrors['user.username'][0]" />

           </font>

           <br>

           <font color="blue">

              4.2.DisplayErrorInfo - user.username(錯誤的運算式1.errors.%{user.username}[0]):<s:textfield/>

              <font color="red">

                  <s:property value="errors.%{user.username}[0]" />

              </font>

              <br> 4.3.DisplayErrorInfo - user.username(錯誤的運算式2.errors.# {user.username}[0]):<s:textfield/>

               這種方法本身語言就有問題,詳情請看JSP規範對# 的規範

              <br> 4.4.DisplayErrorInfo - user.username(錯誤的運算式3.%{errors.user.username}[0]):<s:textfield/>

              <font color="red">

                  <s:property value="%{errors.user.username}[0]" />

              </font>

              <br> 4.5.DisplayErrorInfo - user.username(錯誤的運算式4.errors.user.username):<s:textfield/>

              <font color="red">

                  <s:property value="errors.user.username" />

              </font>

           <br>

           </font>

       </s:form>

       <hr>

       <s:debug/>

    </body>

效果如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.