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



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

每个托管 Bean 实现以下表示操作的接口

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);
}

托管豆 :

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

最新更新