跳轉到內容

WebObjects/Web 應用程式/開發/自定義模板

來自華夏公益教科書

Petite Abeille

[編輯 | 編輯原始碼]

以下解決方案比嚴格需要的更復雜。它可以避免除錯訊息,他使用了一些準私有內容。

這可能相當容易

[編輯 | 編輯原始碼]
 protected WOElement myTemplate = null;
 public WOElement template() {
   if ( myTemplate == null ) {
       myTemplate = WOComponent.templateWithHTMLString( html, wod, null );        
   }
   return myTemplate;
 }

更復雜的方法

這裡是一個關於如何覆蓋 WOComponent.template() 的小例子,這樣你就可以從你想要的地方(例如 jar 檔案)載入 html 模板和 wod。

  • 首先你需要覆蓋 WOComponent.template()
 public WOElement template() {
   return Component.templateForComponent( this );
 }
  • 現在,templateForComponent 可以這樣寫
 public static WOElement templateForComponent(WOComponent aComponent) {
 {
   String aName = aComponent.name();
   WOElement anElement = (WOElement) _elementCache.get( aName );
   if ( anElement == null ) {
     String anHTMLString = Component.componentTemplateWithExtension( aComponent, ".html" );
     String aDescriptionString = Component.componentTemplateWithExtension( aComponent, ".wod" );
     anElement = WOComponent.templateWithHTMLString ( anHTMLString, aDescriptionString, null );
     if ( aComponent.isCachingEnabled() == true ) {
       _elementCache.put( aName, anElement );
     }
   }
   return anElement;
 }
 throw new IllegalArgumentException ( "Component.templateForComponent: null component." );

}

  • componentTemplateWithExtension 方法可以寫成如下
 private static String componentTemplateWithExtension(WOComponent aComponent, String anExtension) {
 if ( anExtension != null ) {
   String aResource = aComponent.name() + anExtension;
   InputStream anInputStream = Component.componentStreamForResource( aComponent, aResource );
   if ( anInputStream != null ) {
     StringBuffer aBuffer = new StringBuffer();
     try {
       BufferedInputStream  aBufferedStream = new BufferedInputStream( anInputStream );
       InputStreamReader  aStreamReader = new InputStreamReader( aBufferedStream );
       int aChar = -1;
       while ( ( aChar = aStreamReader.read() ) != -1 ) {
         aBuffer.append( (char) aChar );
       }
         anInputStream.close();
       } catch(Exception anException) {
         Log.warning( anException );
       }
       return aBuffer.toString();
     }
     throw new RuntimeException ( "Component.componentTemplateWithExtension: resource not found: '" +  aResource + "'." );
   }
   throw new IllegalArgumentException ( "Component.componentTemplateWithExtension: null extension." );
 }
 }
  • 最後,componentStreamForResource 可以寫成如下

 private static InputStream componentStreamForResource(WOComponent aComponent, String aResource) {
 if ( aResource != null ) {
   File aDirectory = new File( System.getProperty( "user.dir" ) );
   File aFile = new File( aDirectory, aResource );    
   if ( aFile.exists() == true ) {
     try {
       return new FileInputStream( aFile );
     } catch(Exception anException) {
       Log.warning( anException);
     }
   }
   return aComponent.getClass().getResourceAsStream( aResource );
 }
 throw new IllegalArgumentException( "Component.componentStreamForResource: null resource." );
 }
華夏公益教科書