java - 未能成功地将参数从 primefaces 页面发送到 java bean 类

标签 java jsf jsf-2 primefaces

我想将一个对象作为参数从 .xhtml 页面发送到 Java 类。

我有以下 primefaces 数据网格:

<p:dataGrid value="#{userRequestBean.userActiveRequests}" var="userActiveRequest">
            <h:panelGrid>

                <h:commandButton value="Cancel" actionListener="#{userRequestBean.cancelRequest()}">
                    <f:setPropertyActionListener target="#{userRequestBean.request}"
                            value="${userActiveRequest}" />
                </h:commandButton>

            </h:panelGrid>
        </p:dataGrid> 

在我的 bean 中,我有:

@ManagedBean(name = "userRequestBean")
@SessionScoped
public class UserRequestBean implements Serializable {
    private TRequest request = new TRequest(); // plus get and set methods

    public void cancelRequest (){
        System.out.println("Author name: " + request.geId());
    }
}

但是当我按下按钮时,它向我抛出 NullPointerException 。知道我如何才能实现这一点吗?提前致谢!

最佳答案

 <h:commandButton value="Cancel" actionListener="#{userRequestBean.cancelRequest()}">
                    <f:setPropertyActionListener target="#{userRequestBean.request}"
                            value="${userActiveRequest}" />
                </h:commandButton>

为什么使用 standard-jsf commandButton 而不是 Primefaces 按钮。

要使这项工作使用:

 <p:commandButton value="Cancel" action="#{userRequestBean.cancelRequest()}">
                    <f:setPropertyActionListener target="#{userRequestBean.request}"
                            value="${userActiveRequest}" />
                </p:commandButton>

我们做了什么?

  • 更改为 Primefaces 命令按钮
  • 使用action而不是actionListener(ActionListener方法应该将ActionEvent作为参数).

希望以上内容对您有所帮助。

关于java - 未能成功地将参数从 primefaces 页面发送到 java bean 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859158/

相关文章:

java - Solr-solrj 4.9.0 在 jdk 1.6 上运行?

java - 如何获取批量操作中插入/删除/更新失败的文档的数组 _ids?

java - 对话范围意外行为

jsf-2 - h :outputText with line break from resource bundle (properties files)

tomcat - 异常找不到工厂 : javax. faces.application.ApplicationFactory

java - Lambda 表达式和匿名类之间的实现差异/优化

java - 为什么我的 java 程序会崩溃?

css - 如何调整 p :commandButton 的大小

java - 如何使冰居中 :confirmationPanel within a modal ice:panelPopup (ICEfaces)?

jsf-2 - session 超时 0 : possible memory leak?