【问题标题】:How to invoke a method with parameter from link如何使用链接中的参数调用方法
【发布时间】:2011-11-26 19:50:28
【问题描述】:

我在支持 bean 中有一个将 Question 对象作为参数的方法。我想将数据表中的问题变量作为参数传递给用户单击按钮/链接时调用的方法。但是,当我尝试 #{bean.deleteQuestion(question)} 时,我只会在 EL 中出现错误,我该怎么做?

        <p:dataTable value="#{bean.questions}" var="question"
            id="questionTable" paginator="true" rows="15"
            paginatorTemplate="{CurrentPageReport} {PreviousPageLink} {PageLinks} {NextPageLink} {RowsPerPageDropdown}"
            rowsPerPageTemplate="15,25,50" paginatorPosition="bottom">
            <p:column sortBy="#{question.id}">
                <f:facet name="header">ID</f:facet>
                <h:outputText value="#{question.id}" />
            </p:column>
            <p:column sortBy="#{question.description}">
                <f:facet name="header">Description</f:facet>
                <h:outputText value="#{question.description}" />
            </p:column>
            <p:column>
                <f:facet name="header">Operations</f:facet>
                <h:link value="Show" outcome="pretty:showQuestion">
                    <f:param name="id" value="#{question.id}" />
                </h:link>
                    &nbsp;|&nbsp; 
                    <h:link value="Edit" outcome="pretty:editQuestion">
                    <f:param name="id" value="#{question.id}" />
                </h:link>
                    &nbsp;|&nbsp;
                    // HERE I WANT A DELETE LINK/BUTTON !
                </p:confirmDialog>
            </p:column>
        </p:dataTable>

【问题讨论】:

  • 堆栈跟踪在哪里?我没有看到您在代码中调用#{bean.deleteQuestion(question)} 的位置。

标签: jsf jsf-2 el


【解决方案1】:

但是,当我尝试#{bean.deleteQuestion(question)}时,我只会在 EL 中遇到错误@

很遗憾,您没有分享有关“EL 错误”的确切细节和您尝试的代码(在以后的问题中,您真的应该这样做)。

我假设你尝试过

<h:commandLink value="Delete" action="#{bean.deleteQuestion(question)}" />

实际上是您的 IDE 错误地显示了警告/错误,并且您无论如何都懒得尝试运行它。根据您的问题历史,您正在使用 Glassfish 3,EL 传递方法参数应该可以正常工作。如果确实是您的 IDE 在该行上猛拉,则无论如何都运行它。如果确实可以正常工作,请重新配置 IDE 的验证设置或安装更好的 JSF/EL 插件。

或者如果你在运行时确实得到了ELException,那么它只能意味着你的web.xml被声明为符合Servlet 2.5而不是Servlet 3.0。 EL 2.2 之后才支持在 EL 中传递方法参数,EL 2.2 是 JSP 2.2 / Servlet 3.0 的一部分。

请注意,命令链接应该有一个&lt;h:form&gt; 父级。确保你有一个;你可以把它放在&lt;h:commandLink&gt;&lt;p:dataTable&gt; 周围。还要确保#{bean} 正确保留questions 以提交表单请求。最简单的就是放在视图范围内。

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    相关资源
    最近更新 更多