Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用每个页面的自定义逻辑重构公共JSF工具栏_Jsf_Jsf 2.2 - Fatal编程技术网

使用每个页面的自定义逻辑重构公共JSF工具栏

使用每个页面的自定义逻辑重构公共JSF工具栏,jsf,jsf-2.2,Jsf,Jsf 2.2,我在所有jsf页面中都有相同的标准工具栏和相同的commandbutton,并执行保存、新建、搜索等操作。。。我正在寻找一种在xhtml页面中重构它并将其包含在每个页面中的方法,问题是每个页面都有自己的ViewScope managedbean,并且命令按钮操作与每个页面相关 每个managedbean实现下面表示操作的接口 public interface ActionAbstract { public void search(ActionEvent actionEvent); publi

我在所有jsf页面中都有相同的标准工具栏和相同的commandbutton,并执行保存、新建、搜索等操作。。。我正在寻找一种在xhtml页面中重构它并将其包含在每个页面中的方法,问题是每个页面都有自己的ViewScope managedbean,并且命令按钮操作与每个页面相关

每个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>

有可能重构它???!!!非常感谢您的帮助。

它完全可以重构。在这种情况下,我更喜欢组合而不是继承。您已经有了一个接口,因此可以轻松地将其用作匿名类并将其传递给复合组件。使用这种方法,您可以在同一页面上有多个工具栏;您可能并不希望这样,但该技术可以应用于其他组件。然后,唯一需要考虑的是,如果您传入Ajax属性更新/处理和按钮渲染属性,或者如果您将它们作为ActionAbstract接口的一部分,如果您将它们作为接口的一部分,那么最好将其重命名

例如,工具栏界面

package jp.faces.test;

public interface Toolbar {
    public void newRecord();
}
菜豆;为了本例的简洁性,我在getter中使用了惰性实例化

@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}"/>