我想将一个对象作为参数从 .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/