2017-04-12 24 views
0

我在所有的jsf页面中都有相同的标准工具栏,其中包含保存,新建,搜索等操作,并且我正在寻找一种重构它的方法在一个xhtml页面中并将其包含在每个页面中,问题是每个页面都有它自己的viewscoped managedbean,并且命令按钮操作与每个页面有关。用每个页面的自定义逻辑重构一个常见的JSF工具条

每个managedbean实现下面的界面,表示动作

public interface ActionAbstract { 

public void search(ActionEvent actionEvent); 

public void newRecord(ActionEvent actionEvent); 

public void clear(ActionEvent actionEvent); 

public void remove(ActionEvent actionEvent); 

public void searchAdvanced(ActionEvent actionEvent); 

public void back(ActionEvent actionEvent); 

public void closePage(ActionEvent actionEvent); 

public void validate(ActionEvent actionEvent); 

public void duplicate(ActionEvent actionEvent); 

public void refresh(ActionEvent actionEvent); 

} 

托管Bean:

@ViewScoped 
@ManagedBean 
public class ExampleBean implements ActionAbstract 

工具栏XHTML:

<p:toolbar> 
<f:facet name="left"> 
    <p:commandButton title="New" update="@all" icon="fa fa-plus" actionListener="#{ExampleBean.newRecord}" process="@this" /> 
    <p:commandButton title="Search" update="@all" icon="fa fa-search" actionListener="#{ExampleBean.search}" rendered="#{ExampleBean.showTable}" 
         process="@this" /> 
    <p:commandButton title="Advanced Search" icon="fa fa-search-plus" actionListener="#{ExampleBean.searchAdvanced}" 
         rendered="#{ExampleBean.showTable}" process="@this" /> 
    <p:commandButton title="Clear" icon="fa fa-eraser" actionListener="#{ExampleBean.clear}" rendered="#{ExampleBean.showTable}" process="@this" /> 
    <p:commandButton title="Duplicate" icon="fa fa-copy" actionListener="#{ExampleBean.duplicate}" rendered="#{ExampleBean.showDetail}" 
         process="@this" /> 
    <p:commandButton title="Validate" icon="fa fa-check" actionListener="#{ExampleBean.validate}" rendered="#{ExampleBean.showDetail}" 
         process="@this" /> 
    <p:commandButton title="Remove" icon="fa fa-remove" actionListener="#{ExampleBean.remove}" rendered="#{ExampleBean.showDetail}" process="@this" /> 
    <p:commandButton title="Refresh" icon="fa fa-refresh" actionListener="#{ExampleBean.refresh}" process="@this" /> 
    <p:commandButton title="Back" update=":form" icon="fa fa-arrow-left" actionListener="#{ExampleBean.back}" rendered="#{ExampleBean.showDetail}" 
         process="@this" /> 
    <p:commandButton title="Close" icon="fa fa-sign-out" actionListener="#{ExampleBean.closePage}" process="@this" /> 

</f:facet> 

它possibl e重构它??? !!!非常感谢您的帮助。

回答

1

它绝对可以重构。这是一种情况,我喜欢构图而不是继承。你已经有了一个接口,所以你可以很容易地将它用作匿名类,并将它传递给一个复合组件。通过这种方法,您可以在同一页面上拥有多个工具栏;不是你可能想要,但该技术可以应用于其他组件。那么唯一的考虑是如果你传递你的Ajax属性(更新/进程)和按钮呈现的属性,或者如果你让它们成为你接口的一部分(如果你让它们成为接口的一部分,那么重命名它将是一个好主意)。

例如,工具栏界面

package jp.faces.test; 

public interface Toolbar { 
    public void newRecord(); 
} 

管Bean;我用延迟实例在吸气剂对于本例

@ManagedBean 
@ViewScoped 
public class TestBean { 

private Toolbar toolbar = null; 

public Toolbar getToolbar(){   
    if(toolbar == null){ 
     toolbar = new Toolbar() {   
      @Override 
      public void newRecord() { 
      // custom bean action goes here 
     }; 
    } 
    return toolbar; 
} 

复合

<cc:interface> 
    <cc:attribute name="toolbar" type="jp.faces.test.Toolbar"/> 
</cc:interface> 

<cc:implementation> 
    <div id="#{cc.clientId}"> 
     <h:commandButton value="New" actionListener="#{cc.attrs.toolbar.newRecord}"/> 
    </div> 
</cc:implementation> 

的简洁起见在一个页面的facelet

<comp:toolbar bean="#{testBean.toolbar}"/> 
使用它