日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
在Struts框架下使用時間類型

使用時間類型?這誰不會,不就是java.util下的幾個類嗎,在不加上java.sql和java.text下的幾個類,這會有什么問題嗎?Struts要是連時間類型都處理不了,那還能干嘛?  在實際應(yīng)用中,我就發(fā)現(xiàn)Struts確實連有些簡單的時間類型都處理不了(不知是我使用的方法不對還是Struts確實沒有考慮到)。順便你也能了解Struts是怎么把form里的請求參數(shù)populate到ActionForm里面的。
  
今天下午同事告訴我把有java.util.Date類型屬性的類存入數(shù)據(jù)庫時出錯,把這個屬性刪除就沒有問題了。當時我就想到是RequestProcessor在processPopulate()時出錯了,因此在它的這個方法設(shè)了斷點并跟蹤了進去。當然,它最先要調(diào)用ActionForm的reset()方法,然后調(diào)用實際處理populate(將請求參數(shù)傳給ActionForm)的RequestUtils.populate()方法。RequestUtils的這個靜態(tài)方法最先是處理Multipart的(即文件上傳等多部分)的方法,然后將所有的請求都放在叫properties的HashMap里并循環(huán)處理它:

 
 
 
  1. names = request.getParameterNames();  
  2.       while (names.hasMoreElements()) {  
  3.         String name = (String) names.nextElement();  
  4.         String stripped = name;  
  5.         if (prefix != null) {  
  6.           if (!stripped.startsWith(prefix)) {  
  7.             continue;  
  8.           }  
  9.           stripped = stripped.substring(prefix.length());  
  10.         }  
  11.         if (suffix != null) {  
  12.           if (!stripped.endsWith(suffix)) {  
  13.             continue;  
  14.           }  
  15.           stripped = stripped.substring(0, stripped.length() - suffix.length());  
  16.         }  
  17.         if (isMultipart) {  
  18.           properties.put(stripped, multipartParameters.get(name));  
  19.         } else {  
  20.           properties.put(stripped, request.getParameterValues(name));  
  21.         }  
  22.       }  

實際處理它們的是下面的:BeanUtils.populate(bean, properties); 其中bean就是接受數(shù)據(jù)的ActionForm,而properties里面則是所有的請求的鍵-值對(鍵和值都是字符串,http協(xié)議的特點)。
  
再看看BeanUtils的靜態(tài)(類)方法populate是怎么處理的:

 
 
 
  1. // Loop through the property name/value pairs to be set  
  2.       Iterator names = properties.keySet().iterator();  
  3.       while (names.hasNext()) {  
  4.     
  5.         // Identify the property name and value(s) to be assigned  
  6.         String name = (String) names.next();  
  7.         if (name == null) {  
  8.           continue;  
  9.         }  
  10.         Object value = properties.get(name);  
  11.     
  12.         // Perform the assignment for this property  
  13.         setProperty(bean, name, value);  
  14.     
  15.       }  

它是循環(huán)所有的請求參數(shù),把實際的工作又交給了setProperty方法。呵呵,弄了半天,這幫人原來都是代理。
  
這個方法還是代理嗎?計算了一下它有180行的代碼。這么長應(yīng)該是個實干家了吧,錯!千萬不要被有些人的外表欺騙了!有些人一天上班16個小時,可夠敬業(yè)的,可有8小時在打CS。這個類就是:一上來20多行都在一個if (log.isTraceEnabled()){}里面。
  
log在這說明一下。Struts中使用的是Jakarta Commons Logging的包,它使用的優(yōu)先級是: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()--當要你把成百上千的System.out.println()去掉的時候你就會覺得Commons Logging是個多好的東東了。它的用法是:

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

private/protected static Log log = LogFactory.getLog(DispatchAction.class);

如果你用的是DispatchAction,那你就不要自己定義Log的實例了,因為它已經(jīng)有一個protected的Log實例,直接使用即可。
  
使用方法是:

    if (log.isInfoEnabled()) {
         log.Info("some information.");
      }

Logging把消息分為6種級別,debug,error,fatal,info,trace,warn。比如,你想記錄一條消息,它只是為了給用戶一個警告,則可以使用warn。為什么在每個log.Info()前做一次判斷呢?難道如果log級別不允許Info,log.Info()仍然能Info嗎?當然不是。在Struts中使用它的作用是提高效率。
  
比如有個消息是計算前一萬個自然數(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是不允許的,那求10000個數(shù)的和就白求的。當然如果你的計算機很快或和高斯一樣聰明,直接log.Info()也每什么問題。
     
閑話少說,回到180多行的BeanUtils.setProperty()方法。這個方法先是處理nested屬性,也就是xxx.xxx的請求參數(shù)。我們只看看處理簡單屬性的必須過程。下面這端代碼有點長,但它只做了一件事:將字符串的請求參數(shù)轉(zhuǎn)成ActionForm的類型。比如:你在ActionForm里有個Integer userAge;然后HTTP請求參數(shù)里可能會有http://localhost:8080/xxx.do?userAge=21。傳人的是字符串,目標是專程Integer。
  
首先它當然會根據(jù)userAge這個字符串查找相應(yīng)的ActionForm,如果這個ActionForm有個屬性也叫userAge,然后就會把這個userAge的時間類型存到type里,type的定義是:Class type = null; 得到type的代碼很長,這是因為要它考慮很多情況,例如

 
 
 
  1. DynaActionForm。  
  2.      // Convert the specified value to the required type  
  3.       Object newValue = null;  
  4.       if (type.isArray() && (index < 0)) { // Scalar value into array  
  5.         if (value == null) {  
  6.           String values[] = new String[1];  
  7.           values[0] = (String) value;  
  8.           newValue = ConvertUtils.convert((String[]) values, type);  
  9.         } else if (value instanceof String) {  
  10.           String values[] = new String[1];  
  11.           values[0] = (String) value;  
  12.           newValue = ConvertUtils.convert((String[]) values, type);  
  13.         } else if (value instanceof String[]) {  
  14.           newValue = ConvertUtils.convert((String[]) value, type);  
  15.         } else {  
  16.           newValue = value;  
  17.         }  
  18.       } else if (type.isArray()) {     // Indexed value into array  
  19.         if (value instanceof String) {  
  20.           newValue = ConvertUtils.convert((String) value,  
  21.                           type.getComponentType());  
  22.         } else if (value instanceof String[]) {  
  23.           newValue = ConvertUtils.convert(((String[]) value)[0],  
  24.                           type.getComponentType());  
  25.         } else {  
  26.           newValue = value;  
  27.         }  
  28.       } else {               // Value into scalar  
  29.         if ((value instanceof String) || (value == null)) {  
  30.           newValue = ConvertUtils.convert((String) value, type);  
  31.         } else if (value instanceof String[]) {  
  32.           newValue = ConvertUtils.convert(((String[]) value)[0],  
  33.                           type);  
  34.         } else if (ConvertUtils.lookup(value.getClass()) != null) {  
  35.           newValue = ConvertUtils.convert(value.toString(), type);// Here is my program's break point  
  36.         } else {  
  37.           newValue = value;  
  38.         }  
  39.       }  

最后是:Struts調(diào)用PropertyUtils的一些方法設(shè)置值。下面代碼的第一種情況是有索引的,即你在請求參數(shù)里傳了field[0]=123之類的參數(shù),第二種是Map時間類型的,傳的是map(key)=value之類的參數(shù),最一般的就是調(diào)用第三個方法。

if (index >= 0) {
   PropertyUtils.setIndexedProperty(target, propName,
   index, newValue);
        } else if (key != null) {
   PropertyUtils.setMappedProperty(target, propName}

【編輯推薦】

  1. Spring AOP的一些概念
  2. acegi到Spring security的轉(zhuǎn)換方式
  3. Spring Framework的理解
  4. 解決Spring2.0向spring2.5遷移的問題
  5. Spring框架人氣暴漲

分享名稱:在Struts框架下使用時間類型
鏈接分享:http://www.dlmjj.cn/article/copjosh.html