Tapestry5.3使用總結

來源:互聯網
上載者:User

1.Tapestry架構的載入是通過Filter來完成的,需要在web.xml中加入以下配置:

<filter><filter-name>app</filter-name><filter-class>org.apache.tapestry5.TapestryFilter</filter-class></filter><filter-mapping><filter-name>app</filter-name><url-pattern>/*</url-pattern></filter-mapping>

2.這裡面,過濾器攔截了所有的URL,某些時候可能希望有一些URL不被攔截(比如Servlet的mapping-url)
這時候需要通過構建IgnoredPathsFilter服務,把不需要攔截的url添加到配置中去
在Module類中,添加以下方法:

public static void contributeIgnoredPathsFilter(Configuration<String> configuration){    configuration.add("/topic");//添加後/topic路徑不被Tapestry過濾器攔截}

除了上述方式,還可以為應用程式單獨指定一個context

public void contributeApplicationDefaults(MappedConfiguration<String, String> configuration){configuration.add(SymbolConstants.APPLICATION_FOLDER, "myApp");}

同時修改filter的url

<filter-mapping><filter-name>app</filter-name><url-pattern>/myApp/*</url-pattern></filter-mapping>

這樣,便不會影響其他Filter和Servlet的使用

3.Tapestry遵循"約定大於配置"的開發原則,以contribute為首碼的方法會自動被架構識別(比如上面的contributeIgnoredPathsFilter方法),除此之外還有其他一些約定:
以decorate開頭的方法 使用裝飾器模式對現有Service進行封裝,並且加入新的功能

4.Tapestry和JQuery在功能上存在相容性的問題,如果想要兩個架構協作運行,需要引入tapestry5-jquery組件,
組件在Tapestry上做了一些JQuery的功能擴充,具體可參考:http://tapestry5-jquery.com/
maven依賴如下:

<dependency><groupId>org.got5</groupId><artifactId>tapestry5-jquery</artifactId><version>3.3.1</version></dependency><repository><id>devlab722-repo</id><url>http://nexus.devlab722.net/nexus/content/repositories/releases</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>devlab722-snapshot-repo</id><url>http://nexus.devlab722.net/nexus/content/repositories/snapshots</url><releases><enabled>false</enabled></releases></repository>

引入該架構後,jquery語句便可以被解析,因此無需在手動引入JQuery架構

5.Tapestry架構提供了面向組件的編程方法,其內部封裝了很多組件,參考:http://tapestry.apache.org/component-reference.html
Tree組件的使用
視圖:

<t:tree t:model="model" t:node="treeNode" t:value="topic"><p:label><t:if test="treeNode.leaf"><a id="${topic.href}" style="cursor:pointer">${treeNode.label}</a></t:if><t:if test="!treeNode.leaf">${treeNode.label}</t:if></p:label></t:tree>

控制器:

@Propertyprivate TreeNode<Topic> treeNode;@Propertyprivate Topic topic;public TreeModel<Topic> getModel(){    return new TopicTreeModel();}

6.除了內建的組件之外,Tapestry還有一個比較特殊的組件:Layout布局組件,該組件是由開發人員來編寫的
組件功能:聲明介面的布局架構,供其他介面繼承使用
組件使用情境:介面之間存在共同的元素(菜單導航、著作權資訊等),將這些共同的元素提取出來作為模版使用,功能類似於JSP的include標籤
視圖:

<html xmlns="http://www.w3.org/1999/xhtml"      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"      xmlns:p="tapestry:parameter">    <head>        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>        <title>${title}</title>    </head>    <body><div>header<div>        <t:body/><!--相當於java中的抽象方法,需要子介面去實現--><div>footer<div>    </body></html>

控制器:

public class Layout{    @Property    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)    private String title;}

7.組件的控制器類中經常會看到以@Parameter標註的屬性用來描述組件參數
如同功能函數需要方法參數一樣,組件也需要參數來決定其行為
如上面的Layout組件,其對外聲明了一個'title'參數,使用該組件時可通過t首碼來指定title的值

<html t:type="layout" t:title="index page" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"        xmlns:j="tapestry-library:jquery">    <div>content<div></html>

同時也看到,由於使用了Layout組件,介面無需在處理header和footer資訊,只需覆蓋<t:body/>標籤中內容便可

組件在使用時,組件參數和類屬性是雙向繫結的,修改一方會級聯影響到另一方,比如這裡如果修改title屬性值介面會發生變化,反之亦然

@Parameter標籤對外提供了一些屬性,作用分別如下:
required:參數是否必須指定
autoconnect:將屬性值和Component的Id值綁定,如果類型一致
value:指定預設值(可綁定屬性運算式)
cache:是否緩衝參數值
defaultPrefix:預設綁定約束

8.在介面指定組件參數值時,只能是String形式的運算式,而組件屬性可能是其他類型,因此需要進行語義轉換
Tapestry提供了幾種內建的轉換方法,通過首碼的方式觸發
asset:尋找資源檔轉換成Asset對象
context:尋找webapp根目錄下資源(<img src="${context:images/icon.png}"/>)
literal:綁定字串文本
message:載入properties檔案中對應的key值
prop:綁定屬性運算式(http://tapestry.apache.org/property-expressions.html)
var:綁定介面臨時變數(<li t:type="loop" source="1..10" value="var:index">${var:index}</li>)

9.在使用Layout組件時,或許會有個疑問。由於介面繼承了Layout模版,因此只需要重寫<t:body/>標籤便可,但是如何才能引入額外的指令碼和樣式呢?
平時寫介面,指令碼都是在<head>裡引入的,而通過Tapestry開發介面,指令碼和樣式的引入可在控制器類中進行聲明

@Import(stylesheet="context:styles/layout-default-latest.css", //引入CSS樣式library={"context:javascripts/vendor/jquery.layout-latest.js","Layout.js"}) //引入js指令碼,如不指定context首碼則表示相對路徑public class IndexPage{......}

10.Tapestry在介面端遵循的是面向組件的開發方法,通常將不同的組件按模組進行劃分,打包放入到不同的jar中
主程式如何才能識別這些jar,並調用其封裝的組件?主要是通過manifest.mf設定檔來完成的。
檔案中封裝了這樣一條中繼資料:Tapestry-Module-Classes: com.yourcompany.services.yourModule
Module類是每個模組的入口,主程式通過它來完成模組的載入,為了將Module中的組件暴露出去供其他Module使用,需要在Module類中聲明如下方法:

public static void contributeComponentClassResolver(Configuration<LibraryMapping> configuration){configuration.add(new LibraryMapping("moduleName", "com.yourcompany"));}

自此,模組中的組件便可被其他Module使用,通過如下聲明:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"  xmlns:m="tapestry-library:moduleName">  ...  <m:yourComp/>  ...</html>

詳細參考:http://tapestry.apache.org/component-libraries.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.