国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

在Struts框架下使用時(shí)間類型

2019-11-18 15:55:53
字體:
供稿:網(wǎng)友

  使用時(shí)間類型?這誰不會(huì),不就是java.util下的幾個(gè)類嗎,在不加上java.sql和java.text下的幾個(gè)類,這會(huì)有什么問題嗎?Struts要是連時(shí)間都處理不了,那還能干嘛?  在實(shí)際應(yīng)用中,我就發(fā)現(xiàn)Struts確實(shí)連有些簡單的時(shí)間都處理不了(不知是我使用的方法不對(duì)還是Struts確實(shí)沒有考慮到)。順便你也能了解Struts是怎么把form里的請(qǐng)求參數(shù)populate到ActionForm里面的。
  
  今天下午同事告訴我把有java.util.Date類型屬性的類存入數(shù)據(jù)庫時(shí)出錯(cuò),把這個(gè)屬性刪除就沒有問題了。當(dāng)時(shí)我就想到是RequestPRocessor在processPopulate()時(shí)出錯(cuò)了,因此在它的這個(gè)方法設(shè)了斷點(diǎn)并跟蹤了進(jìn)去。當(dāng)然,它最先要調(diào)用ActionForm的reset()方法,然后調(diào)用實(shí)際處理populate(將請(qǐng)求參數(shù)傳給ActionForm)的RequestUtils.populate()方法。RequestUtils的這個(gè)靜態(tài)方法最先是處理Multipart的(即文件上傳等多部分)的方法,然后將所有的請(qǐng)求都放在叫properties的HashMap里并循環(huán)處理它:
      names = request.getParameterNames();
      while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        String stripped = name;
        if (prefix != null) {
          if (!stripped.startsWith(prefix)) {
            continue;
          }
          stripped = stripped.substring(prefix.length());
        }
        if (suffix != null) {
          if (!stripped.endsWith(suffix)) {
            continue;
          }
          stripped = stripped.substring(0, stripped.length() - suffix.length());
        }
        if (isMultipart) {
          properties.put(stripped, multipartParameters.get(name));
        } else {
          properties.put(stripped, request.getParameterValues(name));
        }
      }
  
  實(shí)際處理它們的是下面的:BeanUtils.populate(bean, properties); 其中bean就是接受數(shù)據(jù)的ActionForm,而properties里面則是所有的請(qǐng)求的鍵-值對(duì)(鍵和值都是字符串,http協(xié)議的特點(diǎn))。
  
  再看看BeanUtils的靜態(tài)(類)方法populate是怎么處理的:
      // Loop through the property name/value pairs to be set
      Iterator names = properties.keySet().iterator();
      while (names.hasNext()) {
  
        // Identify the property name and value(s) to be assigned
        String name = (String) names.next();
        if (name == null) {
          continue;
        }
        Object value = properties.get(name);
  
        // Perform the assignment for this property
        setProperty(bean, name, value);
  
      }
  它是循環(huán)所有的請(qǐng)求參數(shù),把實(shí)際的工作又交給了setProperty方法。呵呵,弄了半天,這幫人原來都是代理。
  
  這個(gè)方法還是代理嗎?計(jì)算了一下它有180行的代碼。這么長應(yīng)該是個(gè)實(shí)干家了吧,錯(cuò)!千萬不要被有些人的外表欺騙了!有些人一天上班16個(gè)小時(shí),可夠敬業(yè)的,可有8小時(shí)在打CS。這個(gè)類就是:一上來20多行都在一個(gè)if (log.isTraceEnabled()){}里面。
  
  log在這說明一下。Struts中使用的是Jakarta Commons Logging的包,它使用的優(yōu)先級(jí)是:Log4j(4念four似乎比較有意義,大概是Logger For Java的意思,我聽有的人年Log si J,感覺很別扭,呵呵),Java 1.4 Logging API,Simple Logging。功能是依次減弱。
  
  建議在寫Action 的execute()或被execute()調(diào)用的業(yè)務(wù)方法中使用Commons Logging 來代替System.out.println()--當(dāng)要你把成百上千的System.out.println()去掉的時(shí)候你就會(huì)覺得Commons Logging是個(gè)多好的東東了。它的用法是:
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
      private/protected static Log log = LogFactory.getLog(DispatchAction.class);
  假如你用的是DispatchAction,那你就不要自己定義Log的實(shí)例了,因?yàn)樗呀?jīng)有一個(gè)protected的Log實(shí)例,直接使用即可。
  
  使用方法是:
      if (log.isInfoEnabled()) {
         log.Info("some information.");
      }
  Logging把消息分為6種級(jí)別,debug,error,fatal,info,trace,warn。比如,你想記錄一條消息,它只是為了給用戶一個(gè)警告,則可以使用warn。為什么在每個(gè)log.Info()前做一次判定呢?難道假如log級(jí)別不答應(yīng)Info,log.Info()仍然能Info嗎?當(dāng)然不是。它的作用是提高效率。
  
  比如有個(gè)消息是計(jì)算前一萬個(gè)自然數(shù)的和(這種消息可能少見)。用直接log.Info()
      int sum=0;
      for(int i=0;i<10000;i++){
       sum+=i;
      }
      log.Info("the sum of form 1 to 10000 is : "_sum);
  假如log.Info是不答應(yīng)的,那求10000個(gè)數(shù)的和就白求的。當(dāng)然假如你的計(jì)算機(jī)很快或和高斯一樣聰明,直接log.Info()也每什么問題。
     
  閑話少說,回到180多行的BeanUtils.setProperty()方法。這個(gè)方法先是處理nested屬性,也就是xxx.xxx的請(qǐng)求參數(shù)。我們只看看處理簡單屬性的必須過程。下面這端代碼有點(diǎn)長,但它只做了一件事:將字符串的請(qǐng)求參數(shù)轉(zhuǎn)成ActionForm的類型。比如:你在ActionForm里有個(gè)Integer userAge;然后HTTP請(qǐng)求參數(shù)里可能會(huì)有http://localhost:8080/xxx.do?userAge=21。傳人的是字符串,目標(biāo)是專程Integer。
  
  首先它當(dāng)然會(huì)根據(jù)userAge這個(gè)字符串查找相應(yīng)的ActionForm,假如這個(gè)ActionForm有個(gè)屬性也叫userAge,然后就會(huì)把這個(gè)userAge的類型存到type里,type的定義是:Class type = null; 得到type的代碼很長,這是因?yàn)橐紤]很多情況,例如DynaActionForm。
     // Convert the specified value to the required type
      Object newValue = null;
      if (type.isArray() && (index < 0)) { // Scalar value into array
        if (value == null) {
          String values[] = new String[1];
          values[0] = (String) value;
          newValue = ConvertUtils.convert((String[]) values, type);
        } else if (value instanceof String) {
          String values[] = new String[1];
          values[0] = (String) value;
          newValue = ConvertUtils.convert((String[]) values, type);
        } else if (value instanceof String[]) {
          newValue = ConvertUtils.convert((String[]) value, type);
        } else {
          newValue = value;
        }
      } else if (type.isArray()) {     // Indexed value into array
        if (value instanceof String) {
          newValue = ConvertUtils.convert((String) value,
                          type.getComponentType());
        } else if (value instanceof String[]) {
          newValue = ConvertUtils.convert(((String[]) value)[0],
                          type.getComponentType());
        } else {
          newValue = value;
        }
      } else {               // Value into scalar
        if ((value instanceof String) (value == null)) {
          newValue = ConvertUtils.convert((String) value, type);
        } else if (value instanceof String[]) {
          newValue = ConvertUtils.convert(((String[]) value)[0],
                          type);
        } else if (ConvertUtils.lookup(value.getClass()) != null) {
          newValue = ConvertUtils.convert(value.toString(), type);// Here is my program's break point
        } else {
          newValue = value;
        }
      }
  
  最后是:調(diào)用PropertyUtils的一些方法設(shè)置值。下面代碼的第一種情況是有索引的,即你在請(qǐng)求參數(shù)里傳了field[0]=123之類的參數(shù),第二種是Map類型的,傳的是map(key)=value之類的參數(shù),最一般的就是調(diào)用第三個(gè)方法。
        if (index >= 0) {
          PropertyUtils.setIndexedProperty(target, propName,
                           index, newValue);
        } else if (key != null) {
          PropertyUtils.setMappedProperty(target, propName,

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 修文县| 甘洛县| 乌兰浩特市| 东港市| 延吉市| 博湖县| 玉溪市| 斗六市| 健康| 林西县| 阿荣旗| 延津县| 海伦市| 东方市| 琼中| 新晃| 阿瓦提县| 夏津县| 凤冈县| 会泽县| 鸡泽县| 永和县| 天长市| 商丘市| 清新县| 名山县| 微博| 溆浦县| 沙雅县| 商洛市| 通城县| 邵阳县| 德清县| 安岳县| 五常市| 浙江省| 商洛市| 长宁县| 蕲春县| 驻马店市| 永安市|