簡體   English   中英

數據表中的JSF(Primefaces)按鈕到包含數據的新頁面

[英]JSF (Primefaces) Button in datatable to new page with data

和素面。

我與用戶有一個簡單的數據表。 我想要每行“添加項目”中的按鈕導航到新頁面,用戶可以在其中輸入項目詳細信息。 創建新商品時必須使用客戶ID。

<p:dataTable id="dataTable" var="customer"
                value="#{customerController.lazyCustomerModel}"
                rowKey="#{customer.id}" styleClass="userDataTableStyle"
                paginator="true" rows="10"
                selection="#{customerController.selectedCustomers}"
                paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                lazy="true" rowsPerPageTemplate="10,15,50">

                <p:column selectionMode="multiple" style="width:18px" />
                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Id" />
                    </f:facet>
                    <h:outputText value="#{customer.id}" />
                </p:column>

                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Phone" />
                    </f:facet>
                    <h:outputText value="#{customer.phone}" />
                </p:column>

                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Name" />
                    </f:facet>
                    <h:outputText value="#{customer.name}" />
                </p:column>

                <p:column>
                     <!-- Here a button to new page for adding item -->
                </p:column>


                <f:facet name="footer">
                                            <p:commandButton value="Delete Users"
                        actionListener="#{customerController.deleteUsers}"
                        update="dataTable" icon="ui-icon-trash" />
                </f:facet>

            </p:dataTable>

嵌套的方法是什么?我可以直接在URL中使用客戶ID導航到新頁面嗎? 還是應該使用ID提交給控制器,然后重定向?

更新:

所以我嘗試使用viewParam:當我單擊New Item按鈕時,URL中沒有傳遞任何參數。

如果我手動輸入URL:例如/newItem.jsf?customerId=2

方法

public void setCustomerId(String customerId) {
    this.customerId = customerId;
}

在NewItemController中以id 2調用,然后按保存按鈕在新項目中鍵入數據,但現在customerId為null

customers.xhtml

<ui:define name="content">
        <f:metadata>
            <f:viewParam name="customerId" value="#{customerController.customerEnt.id}" />
        </f:metadata>
        <h:form id="customers" prependId="false" includeViewParams="true">

            <p:panelGrid styleClass="panelGridCenter">
                <f:facet name="header">
                    <p:row>
                        <p:column style="text-align: center;" colspan="2">Search Customer</p:column>
                    </p:row>
                </f:facet>

                // Rows with search fields

                <f:facet name="footer">
                    <p:row>
                        <p:column style="text-align: center;" colspan="2">
                            <p:commandButton value="Search"
                                action="#{customerController.search}"
                                update=":customers:dataTable" />
                        </p:column>
                    </p:row>
                </f:facet>
            </p:panelGrid>
            <br />

            <h:outputText
                value="Result is more than 100, only the first 100 results are displayed!"
                rendered="#{customerController.searchResultSize > 100}" />
            <h:outputText value="#{customerController.searchResultSize}" />
            <br />
            <p:dataTable id="dataTable" var="customer"
                value="#{customerController.customers}" rowKey="#{customer.id}"
                styleClass="userDataTableStyle" paginator="true" rows="10"
                selection="#{customerController.selectedCustomers}"
                paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                lazy="true" rowsPerPageTemplate="10,15,50">

                // Columns

                <p:column>
                    <p:commandButton value="New Item" action="#{customerController.newItem()}"/>
                </p:column>

            </p:dataTable>

        </h:form>

    </ui:define>

CustomerController僅在此處顯示重要部分...

@SuppressWarnings("serial")
@SessionScoped
@Named
public class CustomerController implements Serializable {

    private Long customerId;

    public String newItem() {
        return "newItem?faces-redirect=true&includeViewParams=true";
    }

    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }
}

newItem.xhtml

    <?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    template="/WEB-INF/templates/default.xhtml">

    <div class="center">
        <ui:define name="content">
            <f:metadata>
                <f:viewParam name="customerId" value="#{newItemController.customerId}" />
            </f:metadata>

            <h:form id="item">
                <p:messages id="messages" />
                <p:panelGrid styleClass="panelGridCenter">
                        ...
                </p:panelGrid>
            </h:form>
        </ui:define>
    </div>
</ui:composition>

NewItemController

    @SuppressWarnings("serial")
@ViewScoped
@Named
public class NewItemController implements Serializable {

    private String customerId;

public void save() throws Exception {
        try {
            CustomerEnt customerEnt = null;
            if (StringUtils.isNotBlank(customerId)) {
                customerEnt = customerDas.find(Long.parseLong(customerId));
            } else {
                throw new Exception("Customer id not set when creating a new item");
            }
            itemEnt.setCustomerEnt(customerEnt);
            serviceSLSB.save(itemEnt);
        } catch (Exception e) {
            String errorMessage = getRootErrorMessage(e);
            FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Saving unsuccessful");
            facesContext.addMessage(null, m);
        }
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }
}

您可以同時執行以下操作,將參數傳遞給控制器​​中的Bean,您可以像這樣進行操作:

<h:commandButton value="Link" action="#{formBean.someAction}">
    <f:param name="customerId" value="123456" />
</h:commandButton>

然后在表單bean上使用@ManagedProperty

@ManagedProperty(value = "#{param.customerId}")
private String customerId;

... needs getters and setters ...

或者,您可以使用帶有直接GET和參數的鏈接來完成此操作,然后在目標頁面上讀取GET參數並將其保存在Bean中,如下所示(請參見此文章 ):

<f:metadata>
    <f:viewParam name="customerId" value="#{formBeanbean.customerId}" />
</f:metadata>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM