java – 正确使用Facelet模板和复合组件

前端之家收集整理的这篇文章主要介绍了java – 正确使用Facelet模板和复合组件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我仍然不确定正确使用JSF模板&复合部件.我需要创建一个企业Web应用程序,它将拥有大量页面.每个页面都有相同的标题,菜单,页脚,当然还有不同的内容(= JSF模板).每个页面上的内容将包含可重复使用的“框”(= JSF复合组件).这些盒子包括一些文件,按钮等.我的解决方案是否合适?或者我应该使用其他技术,如自定义组件,装饰……?

layout.xhtml

  1. <h:body>
  2. <ui:insert name="main_menu">
  3. <ui:include src="/xhtml/template/main_menu.xhtml"/>
  4. </ui:insert>
  5. <ui:insert name="header">
  6. <ui:include src="/xhtml/template/header.xhtml"/>
  7. </ui:insert>
  8. <ui:insert name="content"/>
  9. <ui:insert name="footer">
  10. <ui:include src="/xhtml/template/footer.xhtml"/>
  11. </ui:insert>
  12. </h:body>

customer_overview.xhtml:

  1. <html xmlns:cc="http://java.sun.com/jsf/composite/composite_component">
  2. <h:body>
  3. <!-- Facelet template -->
  4. <ui:composition template="/xhtml/template/layout.xhtml">
  5. <ui:define name="content">
  6. <!-- Composite Components -->
  7. <cc:component_case_history
  8. caseList="#{customerOverviewController.cases}"
  9. />
  10. <cc:component_customer
  11. ....
  12. />
  13. ...
  14. </ui:define>
  15. </ui:composition>
  16. </h:body>

component_case_history.xhtml

  1. <html xmlns:composite="http://java.sun.com/jsf/composite">
  2. <composite:interface>
  3. <composite:attribute name="cases" type="java.util.List"/>
  4. </composite:interface>
  5.  
  6. <composite:implementation>
  7. <!-- using of "cases" -->
  8. ...
  9. </composite:implementation>

CustomerOverviewController.java

  1. @ManagedBean
  2. @ViewScoped
  3. public class CustomerOverviewController {
  4. public List<Case> getCases() {
  5. ...
  6. }
  7. }

编辑2012-04-27

基于:
When to use <ui:include>,tag files,composite components and/or custom components?

我认为我应该使用Facelet模板Facelet标签文件而不是Facelet模板复合组件.

解决方法

布局,模板

layout.xhtml:

Every page will have the same header,menu,footer …

在这种情况下,您可以省略标题,页脚的ui:insert标记.

  1. <h:body>
  2. <ui:include src="/xhtml/template/main_menu.xhtml"/>
  3. <ui:include src="/xhtml/template/header.xhtml"/>
  4. <ui:insert name="content"/>
  5. <ui:include src="/xhtml/template/footer.xhtml"/>
  6. </h:body>

您可能还有一个ui:insert没有名称,所以如果您想进一步简化:

  1. <h:body>
  2. <ui:include src="/xhtml/template/main_menu.xhtml"/>
  3. <ui:include src="/xhtml/template/header.xhtml"/>
  4. <ui:insert/>
  5. <ui:include src="/xhtml/template/footer.xhtml"/>
  6. </h:body>

customer_overview.xhtml:

如果你有ui:在layout.xhtml中插入没有名字,你不需要ui:define here:

  1. <ui:composition template="/xhtml/template/layout.xhtml">
  2. <!-- Composite Components -->
  3. <cc:component_customer/>
  4. <cc:component_case_history
  5. caseList="#{customerOverviewController.cases}"
  6. />
  7. ...
  8. </ui:composition>

您还应将模板放在用户无法直接访问的文件夹中(WEB-INF).

可重复使用的“盒子”

您的复合组件之一如下所示:

  1. <cc:component_customer/>

没有任何属性的组件非常可疑.

>它做什么?
>显示用户名
>如果你没有传递任何属性,它如何获得用户名

组件应该是独立的,对于其他可重用的部件,请使用ui:insert.

猜你在找的Java相关文章