Tapestry

          記錄學習Tapestry專用布格格。很多文章都轉(zhuǎn)載網(wǎng)絡(luò)。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            20 隨筆 :: 0 文章 :: 4 評論 :: 0 Trackbacks
          http://tapestry.javaeye.com/blog/36271

          Tapestry中表單的提交有很多問題,刷新導(dǎo)致表單的重復(fù)提交、臭名昭著的dirty form warning
          顯示不友好的URL等,這些都可以使用redirect-after-postpostredirectredirectredirectPagefriendlyUrlurl
          要使用這個模式,不得不提到一個ILinkT4listener:
          java 代碼
          ?
          1. public?ILink?listenerMethod(Parameter?parameter);?

          ILinkT4redirectILinkILinkLinkLinkLinkpage servicelinklinkexternal servicelinklink??

          java 代碼
          ?
          1. @InjectObject("engine-service:page")??
          2. ??
          3. ??public?abstract?IEngineService?getPageService();??
          4. ??
          5. ???????public?ILink?getPageLink(String?pageName)?{??
          6. ??
          7. ???????????return?getPageService().getLink(false,?pageName);??
          8. ??
          9. ??}??
          10. ??
          11. ??
          12. ??
          13. ??????@InjectObject("engine-service:external")??
          14. ??
          15. ??public?abstract?IEngineService?getExternalService();??
          16. ??
          17. ???????public?ILink?getExternalLink(String?pageName,?Object[]?parameters)?{??
          18. ??
          19. ?????ExternalServiceParameter?esp?=?new?ExternalServiceParameter(pageName,??
          20. ??
          21. ????????????parameters);??
          22. ??
          23. ?????return?getExternalService().getLink(false,?esp);??
          24. ??
          25. ??}??

          將上述方法放入到你的項目中自定義的MyBasePage(繼承TapestryBasePage)中,然后讓你的

          頁面類繼承MyBasePage,這樣就可以使用上述方法了。

          這樣在頁面提交時可以如下調(diào)用
          java 代碼
          ?
          1. public?ILink?onDelete(Long?id){??
          2. ??
          3. ???
          4. ??
          5. //your?code?to?delete?object??
          6. ??
          7. ???
          8. ??
          9. return?getPageLink(getPageName());//刪除后重新redirect到本頁面??
          10. ??
          11. }??
          12. ??
          13. 當然如果這個頁面帶參數(shù),例如catgoryId=1,則代碼如下:??
          14. ??
          15. public?ILink?onDelete(Long?id){??
          16. ??
          17. ???
          18. ??
          19. //your?code?to?delete?object??
          20. ??
          21. ???
          22. ??
          23. return?getExternalLink(getPageName(),new?Object[]{categoryId});??
          24. //刪除后重新redirect到本頁面,并帶一個參數(shù)??
          25. ??
          26. }??
          上述方式通過繼承來實現(xiàn)方法調(diào)用,感覺不是很好,我們可以利用Tapestry的易擴展特性來擴展它,
          首先想到的就是注釋方式來注入,例如我想實現(xiàn)如下注入
          java 代碼
          ?
          1. @InjectPageLink(“Home“)??
          2. ??
          3. Public?abstract?ILink?getHomeLink();??
          4. ??
          5. ???
          6. ??
          7. @InjectExternalLink(“Category“)??
          8. ??
          9. Public?abstract?ILink?getCategoryLink(Long?categoryId);??

          這樣即去掉了頁面的繼承,也顯得比較直接。
          首先是定義注釋類
          java 代碼
          ?
          1. import?java.lang.annotation.ElementType;??
          2. ??
          3. import?java.lang.annotation.Retention;??
          4. ??
          5. import?java.lang.annotation.RetentionPolicy;??
          6. ??
          7. import?java.lang.annotation.Target;??
          8. ??
          9. ???
          10. ??
          11. @Target(?{?ElementType.METHOD?})??
          12. ??
          13. @Retention(RetentionPolicy.RUNTIME)??
          14. ??
          15. public?@interface?InjectPageLink?{??
          16. ??
          17. ????String?value();??
          18. ??
          19. }??

          然后寫注釋的worker
          java 代碼
          ?
          1. import?java.lang.reflect.Method;??
          2. ??
          3. import?java.lang.reflect.Modifier;??
          4. ??
          5. ???
          6. ??
          7. import?org.apache.hivemind.ApplicationRuntimeException;??
          8. ??
          9. import?org.apache.hivemind.Location;??
          10. ??
          11. import?org.apache.hivemind.service.BodyBuilder;??
          12. ??
          13. import?org.apache.hivemind.service.MethodSignature;??
          14. ??
          15. import?org.apache.tapestry.annotations.AnnotationUtils;??
          16. ??
          17. import?org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;??
          18. ??
          19. import?org.apache.tapestry.engine.ILink;??
          20. ??
          21. import?org.apache.tapestry.enhance.EnhancementOperation;??
          22. ??
          23. import?org.apache.tapestry.spec.IComponentSpecification;??
          24. ??
          25. ???
          26. ??
          27. public?class?InjectPageLinkAnnotationWorker?implements??
          28. ??
          29. ???????MethodAnnotationEnhancementWorker?{??
          30. ??
          31. ???
          32. ??
          33. ????public?void?performEnhancement(EnhancementOperation?op,??
          34. ??
          35. ???????????IComponentSpecification?spec,?Method?method,?Location?location)?{??
          36. ??
          37. ???????if?(!method.getReturnType().equals(ILink.class))??
          38. ??
          39. ???????????throw?new?ApplicationRuntimeException(??
          40. ??
          41. ??????????????????"InjectPage?annotation?must?return?ILink");??
          42. ??
          43. ???
          44. ??
          45. ???????InjectPageLink?injectPageLink?=?method??
          46. ??
          47. ??????????????.getAnnotation(InjectPageLink.class);??
          48. ??
          49. ???
          50. ??
          51. ???????String?pageName?=?injectPageLink.value();??
          52. ??
          53. ???
          54. ??
          55. ???????BodyBuilder?builder?=?new?BodyBuilder();??
          56. ??
          57. ???
          58. ??
          59. ???????builder.begin();??
          60. ??
          61. ???????builder??
          62. ??
          63. ??????????????.addln(??
          64. ??
          65. ?????????????????????"return?getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(?
          66. ?
          67. org.apache.tapestry.Tapestry.PAGE_SERVICE).getLink(false,\"{0}\");",??
          68. ??
          69. ?????????????????????pageName);??
          70. ??
          71. ???
          72. ??
          73. ???????builder.end();??
          74. ??
          75. ???
          76. ??
          77. ???????op.addMethod(Modifier.PUBLIC,?new?MethodSignature(method),?builder??
          78. ??
          79. ??????????????.toString(),?location);??
          80. ??
          81. ???
          82. ??
          83. ???????if?(isGetter(method))??
          84. ??
          85. ???????????op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));??
          86. ??
          87. ????}??
          88. ??
          89. ???
          90. ??
          91. ????boolean?isGetter(Method?method)?{??
          92. ??
          93. ???????return?method.getName().startsWith("get")??
          94. ??
          95. ??????????????&&?method.getParameterTypes().length?==?0;??
          96. ??
          97. ????}??
          98. ??
          99. }??

          External
          的同樣兩個類
          java 代碼
          ?
          1. import?java.lang.annotation.ElementType;??
          2. ??
          3. import?java.lang.annotation.Retention;??
          4. ??
          5. import?java.lang.annotation.RetentionPolicy;??
          6. ??
          7. import?java.lang.annotation.Target;??
          8. ??
          9. ???
          10. ??
          11. @Target(?{?ElementType.METHOD?})??
          12. ??
          13. @Retention(RetentionPolicy.RUNTIME)??
          14. ??
          15. public?@interface?InjectExternalLink?{??
          16. ??
          17. ????String?value();??
          18. ??
          19. }??

          Work
          java 代碼
          ?
          1. import?java.lang.reflect.Method;??
          2. ??
          3. import?java.lang.reflect.Modifier;??
          4. ??
          5. ???
          6. ??
          7. import?org.apache.hivemind.ApplicationRuntimeException;??
          8. ??
          9. import?org.apache.hivemind.Location;??
          10. ??
          11. import?org.apache.hivemind.service.BodyBuilder;??
          12. ??
          13. import?org.apache.hivemind.service.MethodSignature;??
          14. ??
          15. import?org.apache.tapestry.Tapestry;??
          16. ??
          17. import?org.apache.tapestry.annotations.AnnotationUtils;??
          18. ??
          19. import?org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;??
          20. ??
          21. import?org.apache.tapestry.engine.ILink;??
          22. ??
          23. import?org.apache.tapestry.enhance.EnhancementOperation;??
          24. ??
          25. import?org.apache.tapestry.spec.IComponentSpecification;??
          26. ??
          27. ???
          28. ??
          29. public?class?InjectExternalLinkAnnotationWorker?implements??
          30. ??
          31. ???????MethodAnnotationEnhancementWorker?{??
          32. ??
          33. ???
          34. ??
          35. ????public?void?performEnhancement(EnhancementOperation?op,??
          36. ??
          37. ???????????IComponentSpecification?spec,?Method?method,?Location?location)?{??
          38. ??
          39. ???????if?(!method.getReturnType().equals(ILink.class))??
          40. ??
          41. ???????????throw?new?ApplicationRuntimeException(??
          42. ??
          43. ??????????????????"injectExternalLink?annotation?must?return?ILink");??
          44. ??
          45. ???
          46. ??
          47. ???????InjectExternalLink?injectExternalLink?=?method??
          48. ??
          49. ??????????????.getAnnotation(InjectExternalLink.class);??
          50. ??
          51. ???
          52. ??
          53. ???????String?pageName?=?injectExternalLink.value();??
          54. ??
          55. ???
          56. ??
          57. ???????BodyBuilder?builder?=?new?BodyBuilder();??
          58. ??
          59. ???
          60. ??
          61. ???????builder.begin();??
          62. ??
          63. ???
          64. ??
          65. ???????Class[]?parameterTypes?=?method.getParameterTypes();??
          66. ??
          67. ???????int?paramCount?=?Tapestry.size(parameterTypes);??
          68. ??
          69. ???
          70. ??
          71. ???????if?(paramCount?>?0)?{??
          72. ??
          73. ???????????if?(parameterTypes[0].isArray())?{??
          74. ??
          75. ??????????????builder??
          76. ??
          77. ?????????????????????.addln(??
          78. ??
          79. ????????????????????????????"return?getPage().getRequestCycle().getInfrastructure().getServiceMap().?
          80. ?
          81. getService(org.apache.tapestry.Tapestry.EXTERNAL_SERVICE).getLink(false,?new?org.apache.tapestry.engine.ExternalServiceParameter(\"{0}\",$1));",??
          82. ??
          83. ????????????????????????????pageName);??
          84. ??
          85. }?else?{??
          86. ??
          87. ??????????????builder??
          88. ??
          89. ?????????????????????.addln(??
          90. ??
          91. ????????????????????????????"java.lang.Object[]?params?=?new?java.lang.Object[{0}];",??
          92. ??
          93. ????????????????????????????paramCount);??
          94. ??
          95. ??????????????for?(int?i?=?0;?i?<?paramCount;?i++)?{??
          96. ??
          97. ??????????????????builder.add("params[{0}]?=?",?i);??
          98. ??
          99. ???
          100. ??
          101. ??????????????????if?(parameterTypes[i].isPrimitive())??
          102. ??
          103. ?????????????????????builder.add("($w)?");??
          104. ??
          105. ???
          106. ??
          107. ??????????????????//?Parameter?$0?is?this,?so?$1?is?the?first?real?parameter.??
          108. ??
          109. ???
          110. ??
          111. ??????????????????builder.addln("${0};",?i?+?1);??
          112. ??
          113. ??????????????}??
          114. ??
          115. ??????????????builder??
          116. ??
          117. ?????????????????????.addln(??
          118. ??
          119. ????????????????????????????"return?getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(org.apache.tapestry.Tapestry.EXTERNAL_SERVICE).getLink(false,?new?org.apache.tapestry.engine.ExternalServiceParameter(\"{0}\",params));",??
          120. ??
          121. ????????????????????????????pageName);??
          122. ??
          123. ???????????}??
          124. ??
          125. ???????}??
          126. ??
          127. ???
          128. ??
          129. ???????builder.end();??
          130. ??
          131. ???
          132. ??
          133. ???????op.addMethod(Modifier.PUBLIC,?new?MethodSignature(method),?builder??
          134. ??
          135. ??????????????.toString(),?location);??
          136. ??
          137. ???
          138. ??
          139. ???????if?(isGetter(method))??
          140. ??
          141. ???????????op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));??
          142. ??
          143. ????}??
          144. ??
          145. ???
          146. ??
          147. ????boolean?isGetter(Method?method)?{??
          148. ??
          149. ???????//?We?already?know?the?return?type?is?String??
          150. ??
          151. ???
          152. ??
          153. ???????return?method.getName().startsWith("get")??
          154. ??
          155. ??????????????&&?method.getParameterTypes().length?==?0;??
          156. ??
          157. ????}??
          158. ??
          159. }??
          類寫好了,然后就是貢獻到Tapestry
          xml 代碼
          ?
          1. <module?id="your.model.annotation"?version="1.0.0"?package="your.package.annotations">??
          2. ??
          3. ?????<contribution?configuration-id="tapestry.annotation.MethodWorkers">??
          4. ??
          5. ??????<worker?annotation="InjectPageLink"?object="instance:InjectPageLinkAnnotationWorker"/>??
          6. ??
          7. ??????<worker?annotation="InjectExternalLink"?object="instance:InjectExternalLinkAnnotationWorker"/>??
          8. ??
          9. ????<!--</span-->contribution>??
          10. ??
          11. <!--</span-->module>??

          好了將你的module放入項目hivemodule.Xml作為子module
          Okenjoy it

          ?

          最后更新:2006-12-07 11:32
          22:37??|?? 永久鏈接??|?? 瀏覽?(1703)??|?? 評論?(5)??| ?? 收藏??|?? Tapestry4??|?? 進入論壇??|?? 發(fā)布在 Tapestry 圈子

          評論 ???共 5 條 發(fā)表評論
          zhangqidi ??? 2006-11-29 00:22
          Tapestry中表單的提交有很多問題,刷新導(dǎo)致表單的重復(fù)提交、臭名昭著的dirty form warning
          顯示不友好的URL等,這些都可以使用redirect-after-postpostredirectredirectredirectPagefriendlyUrlurl
          要使用這個模式,不得不提到一個ILinkT4listener:
          java 代碼
          ?
          1. public?ILink?listenerMethod(Parameter?parameter);?

          ILinkT4redirectILinkILinkLinkLinkLinkpage servicelinklinkexternal servicelinklink??

          java 代碼
          ?
          1. @InjectObject("engine-service:page")??
          2. ??
          3. ??public?abstract?IEngineService?getPageService();??
          4. ??
          5. ???????public?ILink?getPageLink(String?pageName)?{??
          6. ??
          7. ???????????return?getPageService().getLink(false,?pageName);??
          8. ??
          9. ??}??
          10. ??
          11. ??
          12. ??
          13. ??????@InjectObject("engine-service:external")??
          14. ??
          15. ??public?abstract?IEngineService?getExternalService();??
          16. ??
          17. ???????public?ILink?getExternalLink(String?pageName,?Object[]?parameters)?{??
          18. ??
          19. ?????ExternalServiceParameter?esp?=?new?ExternalServiceParameter(pageName,??
          20. ??
          21. ????????????parameters);??
          22. ??
          23. ?????return?getExternalService().getLink(false,?esp);??
          24. ??
          25. ??}??

          將上述方法放入到你的項目中自定義的MyBasePage(繼承TapestryBasePage)中,然后讓你的

          頁面類繼承MyBasePage,這樣就可以使用上述方法了。

          這樣在頁面提交時可以如下調(diào)用
          java 代碼
          ?
          1. public?ILink?onDelete(Long?id){??
          2. ??
          3. ???
          4. ??
          5. //your?code?to?delete?object??
          6. ??
          7. ???
          8. ??
          9. return?getPageLink(getPageName());//刪除后重新redirect到本頁面??
          10. ??
          11. }??
          12. ??
          13. 當然如果這個頁面帶參數(shù),例如catgoryId=1,則代碼如下:??
          14. ??
          15. public?ILink?onDelete(Long?id){??
          16. ??
          17. ???
          18. ??
          19. //your?code?to?delete?object??
          20. ??
          21. ???
          22. ??
          23. return?getExternalLink(getPageName(),new?Object[]{categoryId});??
          24. //刪除后重新redirect到本頁面,并帶一個參數(shù)??
          25. ??
          26. }??
          上述方式通過繼承來實現(xiàn)方法調(diào)用,感覺不是很好,我們可以利用Tapestry的易擴展特性來擴展它,
          首先想到的就是注釋方式來注入,例如我想實現(xiàn)如下注入
          java 代碼
          ?
          1. @InjectPageLink(“Home“)??
          2. ??
          3. Public?abstract?ILink?getHomeLink();??
          4. ??
          5. ???
          6. ??
          7. @InjectExternalLink(“Category“)??
          8. ??
          9. Public?abstract?ILink?getCategoryLink(Long?categoryId);??

          這樣即去掉了頁面的繼承,也顯得比較直接。
          首先是定義注釋類
          java 代碼
          ?
          1. import?java.lang.annotation.ElementType;??
          2. ??
          3. import?java.lang.annotation.Retention;??
          4. ??
          5. import?java.lang.annotation.RetentionPolicy;??
          6. ??
          7. import?java.lang.annotation.Target;??
          8. ??
          9. ???
          10. ??
          11. @Target(?{?ElementType.METHOD?})??
          12. ??
          13. @Retention(RetentionPolicy.RUNTIME)??
          14. ??
          15. public?@interface?InjectPageLink?{??
          16. ??
          17. ????String?value();??
          18. ??
          19. }??

          然后寫注釋的worker
          java 代碼
          ?
          1. import?java.lang.reflect.Method;??
          2. ??
          3. import?java.lang.reflect.Modifier;??
          4. ??
          5. ???
          6. ??
          7. import?org.apache.hivemind.ApplicationRuntimeException;??
          8. ??
          9. import?org.apache.hivemind.Location;??
          10. ??
          11. import?org.apache.hivemind.service.BodyBuilder;??
          12. ??
          13. import?org.apache.hivemind.service.MethodSignature;??
          14. ??
          15. import?org.apache.tapestry.annotations.AnnotationUtils;??
          16. ??
          17. import?org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;??
          18. ??
          19. import?org.apache.tapestry.engine.ILink;??
          20. ??
          21. import?org.apache.tapestry.enhance.EnhancementOperation;??
          22. ??
          23. import?org.apache.tapestry.spec.IComponentSpecification;??
          24. ??
          25. ???
          26. ??
          27. public?class?InjectPageLinkAnnotationWorker?implements??
          28. ??
          29. ???????MethodAnnotationEnhancementWorker?{??
          30. ??
          31. ???
          32. ??
          33. ????public?void?performEnhancement(EnhancementOperation?op,??
          34. ??
          35. ???????????IComponentSpecification?spec,?Method?method,?Location?location)?{??
          36. ??
          37. ???????if?(!method.getReturnType().equals(ILink.class))??
          38. ??
          39. ???????????throw?new?ApplicationRuntimeException(??
          40. ??
          41. ??????????????????"InjectPage?annotation?must?return?ILink");??
          42. ??
          43. ???
          44. ??
          45. ???????InjectPageLink?injectPageLink?=?method??
          46. ??
          47. ??????????????.getAnnotation(InjectPageLink.class);??
          48. ??
          49. ???
          50. ??
          51. ???????String?pageName?=?injectPageLink.value();??
          52. ??
          53. ???
          54. ??
          55. ???????BodyBuilder?builder?=?new?BodyBuilder();??
          56. ??
          57. ???
          58. ??
          59. ???????builder.begin();??
          60. ??
          61. ???????builder??
          62. ??
          63. ??????????????.addln(??
          64. ??
          65. ?????????????????????"return?getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(?
          66. ?
          67. org.apache.tapestry.Tapestry.PAGE_SERVICE).getLink(false,\"{0}\");",??
          68. ??
          69. ?????????????????????pageName);??
          70. ??
          71. ???
          72. ??
          73. ???????builder.end();??
          74. ??
          75. ???
          76. ??
          77. ???????op.addMethod(Modifier.PUBLIC,?new?MethodSignature(method),?builder??
          78. ??
          79. ??????????????.toString(),?location);??
          80. ??
          81. ???
          82. ??
          83. ???????if?(isGetter(method))??
          84. ??
          85. ???????????op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));??
          86. ??
          87. ????}??
          88. ??
          89. ???
          90. ??
          91. ????boolean?isGetter(Method?method)?{??
          92. ??
          93. ???????return?method.getName().startsWith("get")??
          94. ??
          95. ??????????????&&?method.getParameterTypes().length?==?0;??
          96. ??
          97. ????}??
          98. ??
          99. }??

          External
          的同樣兩個類
          java 代碼
          ?
          1. import?java.lang.annotation.ElementType;??
          2. ??
          3. import?java.lang.annotation.Retention;??
          4. ??
          5. import?java.lang.annotation.RetentionPolicy;??
          6. ??
          7. import?java.lang.annotation.Target;??
          8. ??
          9. ???
          10. ??
          11. @Target(?{?ElementType.METHOD?})??
          12. ??
          13. @Retention(RetentionPolicy.RUNTIME)??
          14. ??
          15. public?@interface?InjectExternalLink?{??
          16. ??
          17. ????String?value();??
          18. ??
          19. }??

          Work
          java 代碼
          ?
          1. import?java.lang.reflect.Method;??
          2. ??
          3. import?java.lang.reflect.Modifier;??
          4. ??
          5. ???
          6. ??
          7. import?org.apache.hivemind.ApplicationRuntimeException;??
          8. ??
          9. import?org.apache.hivemind.Location;??
          10. ??
          11. import?org.apache.hivemind.service.BodyBuilder;??
          12. ??
          13. import?org.apache.hivemind.service.MethodSignature;??
          14. ??
          15. import?org.apache.tapestry.Tapestry;??
          16. ??
          17. import?org.apache.tapestry.annotations.AnnotationUtils;??
          18. ??
          19. import?org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;??
          20. ??
          21. import?org.apache.tapestry.engine.ILink;??
          22. ??
          23. import?org.apache.tapestry.enhance.EnhancementOperation;??
          24. ??
          25. import?org.apache.tapestry.spec.IComponentSpecification;??
          26. ??
          27. ???
          28. ??
          29. public?class?InjectExternalLinkAnnotationWorker?implements??
          30. ??
          31. ???????MethodAnnotationEnhancementWorker?{??
          32. ??
          33. ???
          34. ??
          35. ????public?void?performEnhancement(EnhancementOperation?op,??
          36. ??
          37. ???????????IComponentSpecification?spec,?Method?method,?Location?location)?{??
          38. ??
          39. ???????if?(!method.getReturnType().equals(ILink.class))??
          40. ??
          41. ???????????throw?new?ApplicationRuntimeException(??
          42. ??
          43. ??????????????????"injectExternalLink?annotation?must?return?ILink");??
          44. ??
          45. ???
          46. ??
          47. ???????InjectExternalLink?injectExternalLink?=?method??
          48. ??
          49. ??????????????.getAnnotation(InjectExternalLink.class);??
          50. ??
          51. ???
          52. ??
          53. ???????String?pageName?=?injectExternalLink.value();??
          54. ??
          55. ???
          56. ??
          57. ???????BodyBuilder?builder?=?new?BodyBuilder();??
          58. ??
          59. ???
          60. ??
          61. ???????builder.begin();??
          62. ??
          63. ???
          64. ??
          65. ???????Class[]?parameterTypes?=?method.getParameterTypes();??
          66. ??
          67. ???????int?paramCount?=?Tapestry.size(parameterTypes);??
          68. ??
          69. ???
          70. ??
          71. ???????if?(paramCount?>?0)?{??
          72. ??
          73. ???????????if?(parameterTypes[0].isArray())?{??
          74. ??
          75. ??????????????builder??
          76. ??
          77. ?????????????????????.addln(??
          78. ??
          79. ????????????????????????????"return?getPage().getRequestCycle().getInfrastructure().getServiceMap().?
          80. ?
          81. getService(org.apache.tapestry.Tapestry.EXTERNAL_SERVICE).getLink(false,?new?org.apache.tapestry.engine.ExternalServiceParameter(\"{0}\",$1));",??
          82. ??
          83. ????????????????????????????pageName);??
          84. ??
          85. }?else?{??
          86. ??
          87. ??????????????builder??
          88. ??
          89. ?????????????????????.addln(??
          90. ??
          91. ????????????????????????????"java.lang.Object[]?params?=?new?java.lang.Object[{0}];",??
          92. ??
          93. ????????????????????????????paramCount);??
          94. ??
          95. ??????????????for?(int?i?=?0;?i?<?paramCount;?i++)?{??
          96. ??
          97. ??????????????????builder.add("params[{0}]?=?",?i);??
          98. ??
          99. ???
          100. ??
          101. ??????????????????if?(parameterTypes[i].isPrimitive())??
          102. ??
          103. ?????????????????????builder.add("($w)?");??
          104. ??
          105. ???
          106. ??
          107. ??????????????????//?Parameter?$0?is?this,?so?$1?is?the?first?real?parameter.??
          108. ??
          109. ???
          110. ??
          111. ??????????????????builder.addln("${0};",?i?+?1);??
          112. ??
          113. ??????????????}??
          114. ??
          115. ??????????????builder??
          116. ??
          117. ?????????????????????.addln(??
          118. ??
          119. ????????????????????????????"return?getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(org.apache.tapestry.Tapestry.EXTERNAL_SERVICE).getLink(false,?new?org.apache.tapestry.engine.ExternalServiceParameter(\"{0}\",params));",??
          120. ??
          121. ????????????????????????????pageName);??
          122. ??
          123. ???????????}??
          124. ??
          125. ???????}??
          126. ??
          127. ???
          128. ??
          129. ???????builder.end();??
          130. ??
          131. ???
          132. ??
          133. ???????op.addMethod(Modifier.PUBLIC,?new?MethodSignature(method),?builder??
          134. ??
          135. ??????????????.toString(),?location);??
          136. ??
          137. ???
          138. ??
          139. ???????if?(isGetter(method))??
          140. ??
          141. ???????????op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));??
          142. ??
          143. ????}??
          144. ??
          145. ???
          146. ??
          147. ????boolean?isGetter(Method?method)?{??
          148. ??
          149. ???????//?We?already?know?the?return?type?is?String??
          150. ??
          151. ???
          152. ??
          153. ???????return?method.getName().startsWith("get")??
          154. ??
          155. ??????????????&&?method.getParameterTypes().length?==?0;??
          156. ??
          157. ????}??
          158. ??
          159. }??
          類寫好了,然后就是貢獻到Tapestry
          xml 代碼
          ?
          1. <module?id="your.model.annotation"?version="1.0.0"?package="your.package.annotations">??
          2. ??
          3. ?????<contribution?configuration-id="tapestry.annotation.MethodWorkers">??
          4. ??
          5. ??????<worker?annotation="InjectPageLink"?object="instance:InjectPageLinkAnnotationWorker"/>??
          6. ??
          7. ??????<worker?annotation="InjectExternalLink"?object="instance:InjectExternalLinkAnnotationWorker"/>??
          8. ??
          9. ????<!--</span-->contribution>??
          10. ??
          11. <!--</span-->module>??

          好了將你的module放入項目hivemodule.Xml作為子module
          Okenjoy it

          ?

          tapestry ??? 2006-11-29 10:59

          是呀,tapestry的經(jīng)典處理流程就是get page->set state->active
          active調(diào)用的是forward方法,就是當前頁面的實際頁面與url上指示的頁面不相符,這也無可厚非。tapestry如果真的要完全的OO的話,屏蔽URL上的處理是必須的。
          取得ILink來redirect的一個好處是可以防止form提交后refresh。

          zhangqidi ??? 2006-11-29 12:56
          Tapestry中表單的提交有很多問題,刷新導(dǎo)致表單的重復(fù)提交、臭名昭著的dirty form warning
          顯示不友好的URL等,這些都可以使用redirect-after-postpostredirectredirectredirectPagefriendlyUrlurl
          要使用這個模式,不得不提到一個ILinkT4listener:
          java 代碼
          ?
          1. public?ILink?listenerMethod(Parameter?parameter);?

          ILinkT4redirectILinkILinkLinkLinkLinkpage servicelinklinkexternal servicelinklink??

          java 代碼
          ?
          1. @InjectObject("engine-service:page")??
          2. ??
          3. ??public?abstract?IEngineService?getPageService();??
          4. ??
          5. ???????public?ILink?getPageLink(String?pageName)?{??
          6. ??
          7. ???????????return?getPageService().getLink(false,?pageName);??
          8. ??
          9. ??}??
          10. ??
          11. ??
          12. ??
          13. ??????@InjectObject("engine-service:external")??
          14. ??
          15. ??public?abstract?IEngineService?getExternalService();??
          16. ??
          17. ???????public?ILink?getExternalLink(String?pageName,?Object[]?parameters)?{??
          18. ??
          19. ?????ExternalServiceParameter?esp?=?new?ExternalServiceParameter(pageName,??
          20. ??
          21. ????????????parameters);??
          22. ??
          23. ?????return?getExternalService().getLink(false,?esp);??
          24. ??
          25. ??}??

          將上述方法放入到你的項目中自定義的MyBasePage(繼承TapestryBasePage)中,然后讓你的

          頁面類繼承MyBasePage,這樣就可以使用上述方法了。

          這樣在頁面提交時可以如下調(diào)用
          java 代碼
          ?
          1. public?ILink?onDelete(Long?id){??
          2. ??
          3. ???
          4. ??
          5. //your?code?to?delete?object??
          6. ??
          7. ???
          8. ??
          9. return?getPageLink(getPageName());//刪除后重新redirect到本頁面??
          10. ??
          11. }??
          12. ??
          13. 當然如果這個頁面帶參數(shù),例如catgoryId=1,則代碼如下:??
          14. ??
          15. public?ILink?onDelete(Long?id){??
          16. ??
          17. ???
          18. ??
          19. //your?code?to?delete?object??
          20. ??
          21. ???
          22. ??
          23. return?getExternalLink(getPageName(),new?Object[]{categoryId});??
          24. //刪除后重新redirect到本頁面,并帶一個參數(shù)??
          25. ??
          26. }??
          上述方式通過繼承來實現(xiàn)方法調(diào)用,感覺不是很好,我們可以利用Tapestry的易擴展特性來擴展它,
          首先想到的就是注釋方式來注入,例如我想實現(xiàn)如下注入
          java 代碼
          ?
          1. @InjectPageLink(“Home“)??
          2. ??
          3. Public?abstract?ILink?getHomeLink();??
          4. ??
          5. ???
          6. ??
          7. @InjectExternalLink(“Category“)??
          8. ??
          9. Public?abstract?ILink?getCategoryLink(Long?categoryId);??

          這樣即去掉了頁面的繼承,也顯得比較直接。
          首先是定義注釋類
          java 代碼
          ?
          1. import?java.lang.annotation.ElementType;??
          2. ??
          3. import?java.lang.annotation.Retention;??
          4. ??
          5. import?java.lang.annotation.RetentionPolicy;??
          6. ??
          7. import?java.lang.annotation.Target;??
          8. ??
          9. ???
          10. ??
          11. @Target(?{?ElementType.METHOD?})??
          12. ??
          13. @Retention(RetentionPolicy.RUNTIME)??
          14. ??
          15. public?@interface?InjectPageLink?{??
          16. ??
          17. ????String?value();??
          18. ??
          19. }??

          然后寫注釋的worker
          java 代碼
          ?
          1. import?java.lang.reflect.Method;??
          2. ??
          3. import?java.lang.reflect.Modifier;??
          4. ??
          5. ???
          6. ??
          7. import?org.apache.hivemind.ApplicationRuntimeException;??
          8. ??
          9. import?org.apache.hivemind.Location;??
          10. ??
          11. import?org.apache.hivemind.service.BodyBuilder;??
          12. ??
          13. import?org.apache.hivemind.service.MethodSignature;??
          14. ??
          15. import?org.apache.tapestry.annotations.AnnotationUtils;??
          16. ??
          17. import?org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;??
          18. ??
          19. import?org.apache.tapestry.engine.ILink;??
          20. ??
          21. import?org.apache.tapestry.enhance.EnhancementOperation;??
          22. ??
          23. import?org.apache.tapestry.spec.IComponentSpecification;??
          24. ??
          25. ???
          26. ??
          27. public?class?InjectPageLinkAnnotationWorker?implements??
          28. ??
          29. ???????MethodAnnotationEnhancementWorker?{??
          30. ??
          31. ???
          32. ??
          33. ????public?void?performEnhancement(EnhancementOperation?op,??
          34. ??
          35. ???????????IComponentSpecification?spec,?Method?method,?Location?location)?{??
          36. ??
          37. ???????if?(!method.getReturnType().equals(ILink.class))??
          38. ??
          39. ???????????throw?new?ApplicationRuntimeException(??
          40. ??
          41. ??????????????????"InjectPage?annotation?must?return?ILink");??
          42. ??
          43. ???
          44. ??
          45. ???????InjectPageLink?injectPageLink?=?method??
          46. ??
          47. ??????????????.getAnnotation(InjectPageLink.class);??
          48. ??
          49. ???
          50. ??
          51. ???????String?pageName?=?injectPageLink.value();??
          52. ??
          53. ???
          54. ??
          55. ???????BodyBuilder?builder?=?new?BodyBuilder();??
          56. ??
          57. ???
          58. ??
          59. ???????builder.begin();??
          60. ??
          61. ???????builder??
          62. ??
          63. ??????????????.addln(??
          64. ??
          65. ?????????????????????"return?getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(?
          66. ?
          67. org.apache.tapestry.Tapestry.PAGE_SERVICE).getLink(false,\"{0}\");",??
          68. ??
          69. ?????????????????????pageName);??
          70. ??
          71. ???
          72. ??
          73. ???????builder.end();??
          74. ??
          75. ???
          76. ??
          77. ???????op.addMethod(Modifier.PUBLIC,?new?MethodSignature(method),?builder??
          78. ??
          79. ??????????????.toString(),?location);??
          80. ??
          81. ???
          82. ??
          83. ???????if?(isGetter(method))??
          84. ??
          85. ???????????op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));??
          86. ??
          87. ????}??
          88. ??
          89. ???
          90. ??
          91. ????boolean?isGetter(Method?method)?{??
          92. ??
          93. ???????return?method.getName().startsWith("get")??
          94. ??
          95. ??????????????&&?method.getParameterTypes().length?==?0;??
          96. ??
          97. ????}??
          98. ??
          99. }??

          External
          的同樣兩個類
          java 代碼
          ?
          1. import?java.lang.annotation.ElementType;??
          2. ??
          3. import?java.lang.annotation.Retention;??
          4. ??
          5. import?java.lang.annotation.RetentionPolicy;??
          6. ??
          7. import?java.lang.annotation.Target;??
          8. ??
          9. ???
          10. ??
          11. @Target(?{?ElementType.METHOD?})??
          12. ??
          13. @Retention(RetentionPolicy.RUNTIME)??
          14. ??
          15. public?@interface?InjectExternalLink?{??
          16. ??
          17. ????String?value();??
          18. ??
          19. }??

          Work
          java 代碼
          ?
          1. import?java.lang.reflect.Method;??
          2. ??
          3. import?java.lang.reflect.Modifier;??
          4. ??
          5. ???
          6. ??
          7. import?org.apache.hivemind.ApplicationRuntimeException;??
          8. ??
          9. import?org.apache.hivemind.Location;??
          10. ??
          11. import?org.apache.hivemind.service.BodyBuilder;??
          12. ??
          13. import?org.apache.hivemind.service.MethodSignature;??
          14. ??
          15. import?org.apache.tapestry.Tapestry;??
          16. ??
          17. import?org.apache.tapestry.annotations.AnnotationUtils;??
          18. ??
          19. import?org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;??
          20. ??
          21. import?org.apache.tapestry.engine.ILink;??
          22. ??
          23. import?org.apache.tapestry.enhance.EnhancementOperation;??
          24. ??
          25. import?org.apache.tapestry.spec.IComponentSpecification;??
          26. ??
          27. ???
          28. ??
          29. public?class?InjectExternalLinkAnnotationWorker?implements??
          30. ??
          31. ???????MethodAnnotationEnhancementWorker?{??
          32. ??
          33. ???
          34. ??
          35. ????public?void?performEnhancement(EnhancementOperation?op,??
          36. ??
          37. ???????????IComponentSpecification?spec,?Method?method,?Location?location)?{??
          38. ??
          39. ???????if?(!method.getReturnType().equals(ILink.class))??
          40. ??
          41. ???????????throw?new?ApplicationRuntimeException(??
          42. ??
          43. ??????????????????"injectExternalLink?annotation?must?return?ILink");??
          44. ??
          45. ???
          46. ??
          47. ???????InjectExternalLink?injectExternalLink?=?method??
          48. ??
          49. ??????????????.getAnnotation(InjectExternalLink.class);??
          50. ??
          51. ???
          52. ??
          53. ???????String?pageName?=?injectExternalLink.value();??
          54. ??
          55. ???
          56. ??
          57. ???????BodyBuilder?builder?=?new?BodyBuilder();??
          58. ??
          59. ???
          60. ??
          61. ???????builder.begin();??
          62. ??
          63. ???
          64. ??
          65. ???????Class[]?parameterTypes?=?method.getParameterTypes();??
          66. ??
          67. ???????int?paramCount?=?Tapestry.size(parameterTypes);??
          68. ??
          69. ???
          70. ??
          71. ???????if?(paramCount?>?0)?{??
          72. ??
          73. ???????????if?(parameterTypes[0].isArray())?{??
          74. ??
          75. ??????????????builder??
          76. ??
          77. ?????????????????????.addln(??
          78. ??
          79. ????????????????????????????"return?getPage().getRequestCycle().getInfrastructure().getServiceMap().?
          80. ?
          81. getService(org.apache.tapestry.Tapestry.EXTERNAL_SERVICE).getLink(false,?new?org.apache.tapestry.engine.ExternalServiceParameter(\"{0}\",$1));",??
          82. ??
          83. ????????????????????????????pageName);??
          84. ??
          85. }?else?{??
          86. ??
          87. ??????????????builder??
          88. ??
          89. ?????????????????????.addln(??
          90. ??
          91. ????????????????????????????"java.lang.Object[]?params?=?new?java.lang.Object[{0}];",??
          92. ??
          93. ????????????????????????????paramCount);??
          94. ??
          95. ??????????????for?(int?i?=?0;?i?<?paramCount;?i++)?{??
          96. ??
          97. ??????????????????builder.add("params[{0}]?=?",?i);??
          98. ??
          99. ???
          100. ??
          101. ??????????????????if?(parameterTypes[i].isPrimitive())??
          102. ??
          103. ?????????????????????builder.add("($w)?");??
          104. ??
          105. ???
          106. ??
          107. ??????????????????//?Parameter?$0?is?this,?so?$1?is?the?first?real?parameter.??
          108. ??
          109. ???
          110. ??
          111. ??????????????????builder.addln("${0};",?i?+?1);??
          112. ??
          113. ??????????????}??
          114. ??
          115. ??????????????builder??
          116. ??
          117. ?????????????????????.addln(??
          118. ??
          119. ????????????????????????????"return?getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(org.apache.tapestry.Tapestry.EXTERNAL_SERVICE).getLink(false,?new?org.apache.tapestry.engine.ExternalServiceParameter(\"{0}\",params));",??
          120. ??
          121. ????????????????????????????pageName);??
          122. ??
          123. ???????????}??
          124. ??
          125. ???????}??
          126. ??
          127. ???
          128. ??
          129. ???????builder.end();??
          130. ??
          131. ???
          132. ??
          133. ???????op.addMethod(Modifier.PUBLIC,?new?MethodSignature(method),?builder??
          134. ??
          135. ??????????????.toString(),?location);??
          136. ??
          137. ???
          138. ??
          139. ???????if?(isGetter(method))??
          140. ??
          141. ???????????op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));??
          142. ??
          143. ????}??
          144. ??
          145. ???
          146. ??
          147. ????boolean?isGetter(Method?method)?{??
          148. ??
          149. ???????//?We?already?know?the?return?type?is?String??
          150. ??
          151. ???
          152. ??
          153. ???????return?method.getName().startsWith("get")??
          154. ??
          155. ??????????????&&?method.getParameterTypes().length?==?0;??
          156. ??
          157. ????}??
          158. ??
          159. }??
          類寫好了,然后就是貢獻到Tapestry
          xml 代碼
          ?
          1. <module?id="your.model.annotation"?version="1.0.0"?package="your.package.annotations">??
          2. ??
          3. ?????<contribution?configuration-id="tapestry.annotation.MethodWorkers">??
          4. ??
          5. ??????<worker?annotation="InjectPageLink"?object="instance:InjectPageLinkAnnotationWorker"/>??
          6. ??
          7. ??????<worker?annotation="InjectExternalLink"?object="instance:InjectExternalLinkAnnotationWorker"/>??
          8. ??
          9. ????<contribution>??
          10. ??
          11. <module>??

          好了將你的module放入項目hivemodule.Xml作為子module
          Okenjoy it
          posted on 2007-01-29 18:56 Tapestry 閱讀(881) 評論(1)  編輯  收藏 所屬分類: Tapestry

          評論

          # re: [轉(zhuǎn)載]在Tapestry中使用redirect-after-post模式控制表單提交[未登錄] 2007-08-29 18:39 過客
          在T4.0.2中如果我使用了
          @Persist("Client")
          public abstract void setOrder(Order order);
          public abstract Order getOrder();

          @InjectObject("engine-service:external")
          public abstract IEngineService getExternalService();

          public ILink getExternalLink(String pageName, Object[] parameters) {
          ExternalServiceParameter esp = new ExternalServiceParameter(getPageName(), parameters);
          return getExternalService().getLink(false, esp);
          }
          系統(tǒng)就會因為URL字符串太長,IE直接拋出
          res://c:\WINDOWS\system32\shdoclc.dll/dnserror.html#http://localhost:8080/test/outPage,form.sdirect
          有沒辦法必須使用@Persist("Client")
          但URL字符串可以變短點的方法  回復(fù)  更多評論
            

          主站蜘蛛池模板: 高台县| 碌曲县| 运城市| 徐州市| 天长市| 仪征市| 长岭县| 遂平县| 申扎县| 南和县| 崇仁县| 山阳县| 班玛县| 南通市| 巴林左旗| 尤溪县| 宁波市| 汾阳市| 通州市| 孟连| 沿河| 醴陵市| 汝州市| 汪清县| 祁连县| 渑池县| 山东省| 甘肃省| 舞钢市| 江孜县| 宁津县| 荃湾区| 江安县| 秦皇岛市| 河南省| 当雄县| 高雄县| 读书| 南昌市| 原阳县| 保靖县|