簡體   English   中英

JSF commandButton無法調用操作

[英]JSF commandButton not call action

如果viewParam模式不為null,我有一個jsf form呈現不同的inputText和commandButton 如果沒有模式參數,則單擊時第一個按鈕將調用功能,而如果使用模式參數,則第一個按鈕不會調用功能。 它總是提交並發回。 我想打電話給功能,驗證並重定向回。

<h:form style="border-top: solid 1px #DBDBDB">
    <div class="form-group" style="width: 112px; float: left">
         Quantity:&nbsp;<h:messages class="messages"/>
            <h:inputText value="#{orderCreateBean.stringQuantity}" styleClass="form-control" style="margin-bottom: 10px" rendered="#{productBean.mode == null}" />
            <h:commandButton value="Add to Order" action="#{orderCreateBean.addProduct(productBean.pid)}" styleClass="btn btn-primary" rendered="#{productBean.mode == null}"/>

            <h:inputText value="#{orderEditBean.stringQuantity}" styleClass="form-control" style="margin-bottom: 10px" rendered="#{productBean.mode != null}" />
            <h:commandButton value="Add to Edit" action="#{orderEditBean.addSelectedProduct(productBean.pid)}" styleClass="btn btn-primary" rendered="#{productBean.mode != null}" />


     </div>

</h:form>

OrderEditBean.java

public void addSelectedProduct(int pid) {
    boolean valid = false;
    if (session.get("edit_products") != null && session.get("edit_number") != null) {
        String edit_number = session.get("edit_number").toString();
        List<OrderProductDetails> opds = (List<OrderProductDetails>) session.get("edit_products");
        if (ApplicationHelper.isInteger(stringQuantity)) {
            quantity = Integer.parseInt(stringQuantity);
            if (0 < quantity && quantity <= 10) {
                valid = true;
            }
        }

        if (!valid) {
            ApplicationHelper.addMessage("Quantity between 1 and 10");
        } else {
            OrderProductDetails opd = new OrderProductDetails();
            opd.setProductId(new Products(pid));
            opd.setQuantity(quantity);
            opds.add(opd);
            session.put("edit_products", opds);
            ApplicationHelper.addMessage("Product added!");
        }

        ApplicationHelper.redirect("client/order/edit_products.xhtml?number=" + edit_number, true);

    } else {
        ApplicationHelper.addMessage("You are not in update mode!");
        ApplicationHelper.redirect("/client/product/show.xhtml?pid=" + pid, true);
    }
}

OrderCreateBean.java

public void addProduct(int pid) {
    boolean valid = false;

    if (ApplicationHelper.isInteger(stringQuantity)) {
        quantity = Integer.parseInt(stringQuantity);
        if (0 < quantity && quantity <= 10) {
            valid = true;
        }
    }

    if (!valid) {
        ApplicationHelper.addMessage("Quantity between 1 and 10");
        ApplicationHelper.redirect("/client/product/show.xhtml?pid=" + pid, true);
        return;
    }

    session = SessionHelper.getSessionMap();
    if (session.get("order_product_details") == null) {
        List<OrderProductDetails> opds = new ArrayList<>();
        OrderProductDetails opd = new OrderProductDetails();
        opd.setProductId(new Products(pid));
        opd.setQuantity(quantity);
        opds.add(opd);
        session.put("order_product_details", opds);
    } else {
        boolean exists = false;
        List<OrderProductDetails> opds = (List<OrderProductDetails>) session.get("order_product_details");
        for (OrderProductDetails opd : opds) {
            if (opd.getProductId().getPid() == pid) {
                opd.setQuantity(opd.getQuantity() + quantity);
                exists = true;
                break;
            }
        }

        if (!exists) {
            OrderProductDetails opd = new OrderProductDetails();
            opd.setProductId(new Products(pid));
            opd.setQuantity(quantity);
            opds.add(opd);
        }

        session.put("order_product_details", opds);
    }

    ApplicationHelper.addMessage("Product added!");
    ApplicationHelper.redirect("/client/order/selected_products.xhtml", true);
}

您的方法“ addSelectedProduct”和“ addProduct”的簽名錯誤。 這是Oracle文檔所說的關於h:commandButton標記的value屬性中允許的表達式的內容:

“表達式必須對不帶參數的公共方法求值,並返回一個對象(調用此對象的toString()以得出邏輯結果),該對象將傳遞給此應用程序的NavigationHandler。”

所以這就是我要做的方式:

XHTML代碼:

<h:commandButton value="Add to Edit" action="{orderEditBean.addSelectedProduct}" styleClass="btn btn-primary" rendered="#{productBean.mode != null}">
    <f:param name="pid" value="#{productBean.pid}" />
</h:commandButton>

管理代碼:

    public String addProduct() {

      // retrieve the pid value
      String pid = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pid");

      // convert pid to integer value and do all your stuff here

      return null;
    }

第一種方法也是如此。

暫無
暫無

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

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