WebObjects/Web 应用程序/开发/技巧和窍门
外观
- wocontext.request().uri() = 当前正在请求的 URL
您的应用程序与多个不同的 URL 相关联,所有这些 URL 都可以从 WOApplication 上的各种方法中检索。以下是一个快速备忘单:
- WOApplication.application().baseURL() = /WebObjects
- WOApplication.application().applicationBaseURL() = /WebObjects
- WOApplication.application().cgiAdaptorURL() = http://hostname/cgi-bin/WebObjects
- WOApplication.application().directConnectURL() = http://hostname:port/cgi-bin/WebObjects/MyApplication.woa
- WOApplication.application().frameworksBaseURL() = /WebObjects/Frameworks
- WOApplication.application().servletConnectURL() = http://hostname/cgi-bin/WebObjects/MyApplication.woa
- WOApplication.application().webserverConnectURL() = http://hostname/cgi-bin/WebObjects/MyApplication.woa/-port
/** 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 处理数组关键路径的方式来看),这有点显而易见。但我认为这里真正的教训是,在字母页的末尾很容易忽略文档……
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), " ")) + "..."; }