<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    176142998

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks

    Struts2中最簡單的驗(yàn)證數(shù)據(jù)的方法是使用validate。我們從ActionSupport類的源代碼中可以看到,ActionSupport類實(shí)現(xiàn)了一個(gè)Validateable接口。這個(gè)接口只有一個(gè)validate方法。如果Action類實(shí)現(xiàn)了這個(gè)接口,Struts2在調(diào)用execute方法之前首先會調(diào)用這個(gè)方法,我們可以在validate方法中驗(yàn)證,如果發(fā)生錯(cuò)誤,可以根據(jù)錯(cuò)誤的level選擇字段級錯(cuò)誤,還是動作級錯(cuò)誤。并且可使用addFieldErroraddActionError加入相應(yīng)的錯(cuò)誤信息,如果存在ActionField錯(cuò)誤,Struts2會返回“input”(這個(gè)并不用開發(fā)人員寫,由Struts2自動返回),如果返回了“input”,Struts2就不會再調(diào)用execute方法了。如果不存在錯(cuò)誤信息,Struts2在最后會調(diào)用execute方法。

    這兩個(gè)add方法和ActionErrors類中的add方法類似,只是add方法的錯(cuò)誤信息需要一個(gè)ActionMessage對象,比較麻煩。除了加入錯(cuò)誤信息外,還可以使用addActionMessage方法加入成功提交后的信息。當(dāng)提交成功后,可以顯示這些信息。

    以上三個(gè)add方法都在ValidationAware接口中定義,并且在ActionSupport類中有一個(gè)默認(rèn)的實(shí)現(xiàn)。其實(shí),在ActionSupport類中的實(shí)現(xiàn)實(shí)際上是調(diào)用了ValidationAwareSupport中的相應(yīng)的方法,也就是這三個(gè)add方法是在ValidationAwareSupport類中實(shí)現(xiàn)的,代碼如下:
    private final ValidationAwareSupport validationAware = new ValidationAwareSupport();

    public void addActionError(String anErrorMessage) 
    {      validationAware.addActionError(anErrorMessage);
    }
    public void addActionMessage(String aMessage) 
    {
        validationAware.addActionMessage(aMessage);
    }
    public void addFieldError(String fieldName, String errorMessage) 
    {
        validationAware.addFieldError(fieldName, errorMessage);
    }

    下面我們來實(shí)現(xiàn)一個(gè)簡單的驗(yàn)證程序,來體驗(yàn)一個(gè)validate方法的使用。

    先來在Web根目錄建立一個(gè)主頁面(validate.jsp),代碼如下:


    <%@ page language="java"  pageEncoding="GBK"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>

    <html>
     <head>
      <title>輸入操作數(shù)</title>

      <style type="text/css">
    .label {
     font-style: italic;
    }

    .errorLabel {
     font-style: italic;
     color: red;
    }

    .errorMessage {
     font-weight: bold;
     color: red;
    }
    </style>

     
    </head>

     <body>
      求代數(shù)和
      <br />
      <s:form action="First">
       <s:textfield name="operand1" label=" 操作數(shù)1" />
       <s:textfield name="operand2" label=" 操作數(shù)2" />
       <s:submit value="代數(shù)和" />
      </s:form>
     </body>
    </html>

    在上面的代碼中,使用了Struts2tag<s:actionerror>、<s:fielderror><s:actionmessage>,分別用來顯示動作錯(cuò)誤信息,字段錯(cuò)誤信息,和動作信息。如果信息為空,則不顯示。

    現(xiàn)在我們來實(shí)現(xiàn)一個(gè)動作類,代碼如下:


    package action;

    import com.opensymphony.xwork2.ActionSupport;

    @SuppressWarnings("serial")
    public class FirstAction extends ActionSupport {
     private int operand1;
     private int operand2;

     public String execute() throws Exception {
      System.out.println(getFirst() + "http://////////");
      if (getFirst() <= 0) // 如果代碼數(shù)和是非負(fù)整數(shù),跳到positive.jsp頁面
      {
       return INPUT;
      }
      return SUCCESS;
     }

     public int getOperand1() {
      return operand1;
     }

     public void setOperand1(int operand1) {
      System.out.println("operand1:" + operand1);
      this.operand1 = operand1;
     }

     public int getOperand2() {
      return operand2;
     }

     public void setOperand2(int operand2) {
      System.out.println("operand2:" + operand2);
      this.operand2 = operand2;
     }

     public int getFirst() {
      return operand1 + operand2; // 計(jì)算兩個(gè)整數(shù)的代碼數(shù)和
     }

    }


    大家從上面的代碼可以看出,Field錯(cuò)誤需要一個(gè)key(一般用來表示是哪一個(gè)屬性出的錯(cuò)誤),而Action錯(cuò)誤和Action消息只要提供一個(gè)信息字符串就可以了。

    最后來配置一下這個(gè)Action,代碼如下:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>

     <package name="struts2" extends="struts-default">
      <action name="First" class="First">
       <result name="input">/index.jsp</result>
       <result name="success">/positive.jsp</result>
     </action>
     </package>
    </struts>


     

    我們還可以使用ValidationAware接口的其他方法(由ValidationAwareSupport類實(shí)現(xiàn))獲得或設(shè)置字段錯(cuò)誤信息、動作錯(cuò)誤信息以及動作消息。如hasActionErrors方法判斷是否存在動作層的錯(cuò)誤,getFieldErrors獲得字段錯(cuò)誤信息(一個(gè)Map對象)。下面是ValidationAware接口提供的所有的方法:


    package com.opensymphony.xwork2;

    import java.util.Collection;
    import java.util.Map;

    public interface ValidationAware
    {
        
    void setActionErrors(Collection errorMessages);
        Collection getActionErrors();

        
    void setActionMessages(Collection messages);
        Collection getActionMessages();
        
    void setFieldErrors(Map errorMap);
        Map getFieldErrors();
        
    void addActionError(String anErrorMessage);
        
    void addActionMessage(String aMessage);
        
    void addFieldError(String fieldName, String errorMessage);
        
    boolean hasActionErrors();
        
    boolean hasActionMessages();
        
    boolean hasErrors();
        
    boolean hasFieldErrors();
    }

    關(guān)于其他更詳細(xì)的驗(yàn)證規(guī)則,請讀者訪問http://struts.apache.org/2.0.11.1/docs/validation.html來查看。

    struts2驗(yàn)證信息重復(fù)出現(xiàn)解決方案

    你的action是不是被spring代管了?


          我一看,是啊,我的action是被spring代管了。

          接著他又寫道:

          你的action class是不是用的bean 的id?

          我一邊看還一邊點(diǎn)頭,是啊,我的真是這樣的,怎么這么了解我?。ㄆ鋵?shí)人人都這樣寫)

          最后他說:

          把你action 的class改成全路徑,問題就會得到解決。


          是嗎?我當(dāng)時(shí)想,那就是說不要spring代管了,那我還要向這個(gè)action 注屬性的,這樣改了肯定注不進(jìn)去了。

          正如我所說的,這樣改了驗(yàn)證的問題是得到解決了,可是屬性注不進(jìn)去了。那不是正了一樣又歪了一樣,問題并沒有得到根本性的解決。

          正在有點(diǎn)想抓狂中,卻無意發(fā)現(xiàn)我的這個(gè)bean怎么沒有寫scope="prototype",這個(gè)屬性是告訴spring,每來一個(gè)action給我產(chǎn)生一個(gè)新的實(shí)例。就這么簡單,問題得以真正的解決。

    posted on 2008-08-01 11:48 飛飛 閱讀(544) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 两个人日本WWW免费版| 暖暖免费中文在线日本| 最近免费字幕中文大全视频| 国外亚洲成AV人片在线观看| a一级爱做片免费| 亚洲人成影院在线观看| 窝窝影视午夜看片免费| 亚洲日韩人妻第一页| 国产精品午夜免费观看网站| 中文字幕亚洲一区| 秋霞人成在线观看免费视频 | 亚洲不卡中文字幕| 在线看片v免费观看视频777| 亚洲AV无码一区二区三区人| 在线观看免费人成视频| 亚洲欧美熟妇综合久久久久| jjzz亚洲亚洲女人| 无码午夜成人1000部免费视频| 亚洲欧洲综合在线| 午夜时刻免费入口| jizz在线免费观看| 亚洲精品自产拍在线观看动漫| 无码国产精品一区二区免费式直播 | 国产精品偷伦视频免费观看了| 亚洲AV午夜福利精品一区二区 | 亚洲av永久无码嘿嘿嘿| 韩国二级毛片免费播放| 精品一区二区三区免费观看| 亚洲丁香色婷婷综合欲色啪| 久久精品免费一区二区喷潮| 免费福利在线观看| 亚洲综合日韩中文字幕v在线| 最近2019中文字幕mv免费看| 成人免费网站久久久| 亚洲日本va午夜中文字幕一区| 好男人视频在线观看免费看片| A毛片毛片看免费| 亚洲永久在线观看| 美腿丝袜亚洲综合| 中文字幕无码视频手机免费看 | 国产精品亚洲综合五月天|