p:commandButton没有执行动作方法

时间:2014-02-06 20:02:04

标签: jsf primefaces

我已经挖了SO试图解决这个问题并继续寻找回到帖子(我似乎无法再找到)的方式,其中BalusC提供了可能存在问题的列表,但我已经实现了解决他们适用于我的问题。情况是,在我的.xhtml文件中,我有一个Primefaces commandButton尝试调用支持Bean方法,同时使用f:setPropertyActionListener设置一些bean属性。但是bean方法永远不会被调用。

令人不安的是,我正在处理的页面上的按钮使用了几乎相同的代码,这些按钮工作得很好。

点击此按钮可刷新页面,但没有其他内容。一些相关信息:按钮位于ui:repeat内部,它位于另一个ui:repeat内部,位于p:dataGrid内部。整个事物包含在h:形式中。页面上有两个h:form标签,但它们不是嵌套的。此按钮位于第二个表单内。第一种形式的按钮工作正常。支持bean是ViewScoped。

我正在使用JSF 2.X和Facelets,以及使用Primefaces。我的服务器是Apache Tomcat v7.0.47

以下是行动方法:

public final String selectAnswerForRequest() {
    FacesContext fCtx = FacesContext.getCurrentInstance();
    String navString = "";
    try {
        if (chosenAnswer != null && unansweredRequest != null && unansweredRequest.getResponse() != null) {
            brain.handleResponse(unansweredRequest.getResponse(), chosenAnswer, unansweredRequest);
            navString = "refresh";
        }
    } catch (BrainException e) {
        LOGGER.error("Error adding answer", e);
        fCtx.addMessage(null, buildErrorMessage("hdrFailed", "txtErrorAddingAnswer"));
    }
    return navString;
}

一切都有吸气剂和二传手。我已经尝试将action属性更改为actionListener并将我的操作方法更改为接受事件,但它仍然无效。我试过把它从p:改为h:。我试过把它变成一个commandLink和一个h:commandButton。

编辑:这是一个更清晰的整个.xhtml文档:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
xmlns:encode="http://envisage.com/jsf/encode"
template="/templates/twocolumn.xhtml">

<ui:param name="pageTitle" value="#{msgHome['pageTitle']}" />
<ui:param name="pageId" value="pg-home" />
<ui:param name="pageDesc" value="#{msgHome['pageDesc']}" />

<ui:define name="columnOne">

    <h2 class="section-title">
        <span class="label label-warning label-icon"><i
            class="eis-icon-recent"></i></span> #{msgHome['answeredTitle']}
    </h2>
    <div id="answered-questions" data-resize="trending-sidebar"
        data-min-resize="200">
        <h:form id="firstform" prependId="false">
            <p:dataGrid paginatorPosition="bottom" var="req"
                value="#{homeBean.answeredQuestions}" rows="5" columns="1"
                paginator="true"
                paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                id="renderTarget1">
                <p:column>
                    <section>
                        <div>
                            <dl>
                                <ui:repeat var="answer" value="#{req.response.answers}">
                                    <div>
                                        <p:commandButton update="@form, :growlForm:growl"
                                            ajax="false" value="#{msgHome['highlyBtn']}"
                                            action="#{homeBean.markAnswerAsHighlyUseful}">
                                            <f:setPropertyActionListener
                                                target="#{homeBean.selectedAnswer}" value="#{answer}" />
                                            <f:setPropertyActionListener
                                                target="#{homeBean.selectedRequest}" value="#{req}" />
                                        </p:commandButton>
                                        <p:commandButton update="@form, :growlForm:growl"
                                            ajax="false" value="#{msgHome['somewhatBtn']}"
                                            action="#{homeBean.markAnswerAsSomewhatUseful}">
                                            <f:setPropertyActionListener
                                                target="#{homeBean.selectedAnswer}" value="#{answer}" />
                                            <f:setPropertyActionListener
                                                target="#{homeBean.selectedRequest}" value="#{req}" />
                                        </p:commandButton>
                                        <p:commandButton update="@form, :growlForm:growl"
                                            ajax="false" value="#{msgHome['notBtn']}"
                                            action="#{homeBean.markAnswerAsNotUseful}">
                                            <f:setPropertyActionListener
                                                target="#{homeBean.selectedAnswer}" value="#{answer}" />
                                            <f:setPropertyActionListener
                                                target="#{homeBean.selectedRequest}" value="#{req}" />
                                        </p:commandButton>
                                    </div>
                                    <p:separator />
                                </ui:repeat>
                            </dl>
                        </div>
                    </section>
                </p:column>
            </p:dataGrid>
        </h:form>
    </div>


    <h2>
        <span class="label label-danger label-icon"><i
            class="eis-icon-question"></i></span> #{msgHome['unansweredTitle']}
    </h2>
    <div id="unanswered-questions" data-resize="moderators-sidebar"
        data-min-resize="140">
        <h:form id="secondForm" prependId="false">
            <p:dataGrid paginatorPosition="bottom" var="unansweredReq"
                value="#{homeBean.unansweredRequests}" rows="5" columns="1"
                paginator="true"
                paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                id="renderTarget2">
                <p:column>
                    <section>
                        <div>
                            <dl>
                                <p:separator />
                                <ui:repeat var="match"
                                    value="#{unansweredReq.matchingQuestions}">
                                    <ui:repeat var="answers" value="#{match.answers}">
                                        <div>
                                            <p:commandButton update="@form, :growlForm:growl"
                                                ajax="false" value="#{msgHome['acceptAnsBtn']}"
                                                action="#{homeBean.selectAnswerForRequest}" execute="@this">
                                                <f:setPropertyActionListener
                                                    target="#{homeBean.chosenAnswer}" value="#{answers}" />
                                                <f:setPropertyActionListener
                                                    target="#{homeBean.unansweredRequest}"
                                                    value="#{unansweredReq}" />
                                            </p:commandButton>
                                            <p:commandButton update="@form, :growlForm:growl"
                                                ajax="false" value="#{msgHome['removeAnsBtn']}"
                                                action="#{homeBean.removeAnswerFromRequest}"
                                                execute="@this">
                                                <f:setPropertyActionListener
                                                    target="#{homeBean.chosenAnswer}" value="#{answers}" />
                                                <f:setPropertyActionListener
                                                    target="#{homeBean.unansweredRequest}"
                                                    value="#{unansweredReq}" />
                                            </p:commandButton>
                                        </div>
                                        <p:separator />
                                    </ui:repeat>
                                </ui:repeat>
                            </dl>
                        </div>
                    </section>
                </p:column>
            </p:dataGrid>
        </h:form>
    </div>

    <h:form id="growlForm">
        <p:growl id="growl" showDetail="true" life="5000"
            severity="info,warn,error" />
    </h:form>

    <h:messages />

</ui:define>

编辑2:最新发现是在删除.xhtml文档的上半部分(整个第一个h:表单)后,下半部分的按钮开始正常工作。我目前正试图利用这些知识来了解我可以获得的地方。

1 个答案:

答案 0 :(得分:0)

您使用的dataTable中的

<p:dataGrid paginatorPosition="bottom" var="unansweredReq"

var =“unansweredReq”

但在你的:

<f:setPropertyActionListener target="#{homeBean.unansweredRequest}"
  value="#{unansweredreq}" />

<强>值= “#{unansweredreq}”

你看到了差异( unansweredreq!= unansweredReq )?我猜jsf不接受这个。 (我不确定)

unansweredreq 在您的代码中的许多地方使用)