跳轉到內容

WebObjects/Web 應用程式/開發/技巧和竅門

來自華夏公益教科書
  • wocontext.request().uri() = 當前正在請求的 URL

您的應用程式與多個不同的 URL 相關聯,所有這些 URL 都可以從 WOApplication 上的各種方法中檢索。以下是一個快速備忘單:

瀏覽器 IP

[編輯 | 編輯原始碼]
 /** Returns the IP address of the client.
    * This should return accurate information whether in direct connect or webserver deployment mode.
    * If performance caching is turned on on OS X server, this method will correctly use pc-remote-addr
    * @return The IP address as a string.
    */
   public static String clientIP(WORequest request) {
       Object ipAddress = request.headerForKey("pc-remote-addr");
       if (ipAddress == null) {
               ipAddress = request.headerForKey("remote_addr");
               if( ipAddress == null ) {
                   ipAddress = request.headerForKey("remote_host");
                   if( ipAddress == null ) {
                       ipAddress = request._remoteAddress();
                       if( ipAddress == null ) {
                           ipAddress = request._originatingAddress();
                           if( ipAddress != null ) ipAddress = ((InetAddress)ipAddress).getHostAddress();
                       }
                   }
               }
       }
       return ipAddress == null ? "<address unknown>" : ipAddress.toString();
   }

在文件中,NSArray 的 KeyValueCoding 實現並非我預期的。要獲取 NSArray 中特定數字索引處的物件,您將使用

objectAtIndex() 

方法。那麼

NSArray.valueForKey(String key) 

返回什麼?

首先,閱讀文件:file:///OSX/Developer/Documentation/WebObjects/Reference/com/webobjects/foundation/NSArray.html#valueForKey(java.lang.String)

事實證明,對陣列呼叫 valueForKey 等同於對該陣列的每個元素呼叫 valueForKey。因此,如果您有一個 Users 的 NSArray,呼叫 valueForKey("email"); 將返回一個電子郵件地址的 NSArray。呼叫 valueForKey("documents"); 將返回一個包含文件物件的 NSArray 的 NSArray。事後諸葛亮(從 WOBuilder 處理陣列關鍵路徑的方式來看),這有點顯而易見。但我認為這裡真正的教訓是,在字母頁的末尾很容易忽略文件……

HTML 友好字串截斷

[編輯 | 編輯原始碼]
 import org.apache.commons.lang.*; //From Apache
 import org.clapper.util.text.*; // From http://www.clapper.org/
 
 public static String stripHTMLTagsAndConcatenate(String htmlString, int numberOfChar) {
   return (StringUtils.substringBeforeLast(StringUtils.abbreviate((HTMLUtil.stripHTMLTags(htmlString)), numberOfChar), " ")) + "...";
 }
華夏公益教科書