gpt4 book ai didi

java - 为什么这个参数在我的 Bean 中作为 Null 发送?

转载 作者:行者123 更新时间:2023-12-02 08:08:48 25 4
gpt4 key购买 nike

我正在使用 JSF 2 和 EJB 3.1 创建表单。

我正在使用页面的这一部分来获取一些数据,因此我可以使用下面的确认对话框将其传递给我的 bean

<p:column headerText="#{bundle.edit}" style="width:10px; overflow:visible;">  
<p:rowEditor/>
</p:column>
<p:column headerText="#{bundle.delete}" style="width:10px; overflow:visible;">
<p:commandButton update=":form" oncomplete="confirmation.show()"
image="ui-icon ui-icon-close" title="Delete">
<f:param value="#{user}" name="userAction" />
</p:commandButton>
</p:column>
</p:dataTable>

<p:confirmDialog message="Are you sure? user:#{param['userAction']} " width="500"
header="Confirm" severity="alert" widgetVar="confirmation">
<p:commandButton value="Yes sure" update=":form"
actionListener="#{userController.deleteAction(param['userAction'])}"
oncomplete="confirmation.hide()" />
<p:commandButton value="Not yet" onclick="confirmation.hide()" type="button" />

</p:confirmDialog>

</h:form>

这就是应该得到它的 Bean

@Named(value = "userController")
@Stateful
@RequestScoped
@TransactionManagement(TransactionManagementType.CONTAINER)
public class UserController implements Serializable {

private User current;

@Inject
private br.com.cflex.itm.dataaccess.UserFacade userFacade;

public UserController() {
}

public void deleteAction(User user) {
userFacade.remove(user);
}

但是我的 bean 仅作为用户接收 null,并且在对话框中我正在打印数据,以便我可以看到那里选择了一个用户对象。

这样传递参数有什么问题吗?
为什么我的 Bean 中的值为 null?因为它们在客户端和服务器端之间的通信中迷失了......

最佳答案

<p:commandButton action="#{userController.deleteAction(param['userAction'])}" />

action 的 EL (和 actionListener )在提交表单时评估,而不是在显示表单时评估。请求参数是请求范围的,并且不会出现在表单提交的后续请求中。您需要传递它:

<p:commandButton action="#{userController.deleteAction(param['userAction'])}">  
<f:param name="userAction" value="#{param['userAction']}" />
</p:commandButton>

<f:param> 的 EL显示表单时进行评估。因此它将出现在生成的 HTML 中,并且 JavaScript 将负责传递它。

注意请求参数为String类型。期望它们是User根本行不通。基本上,它包含值 User#toString() 。您需要采取 String作为操作参数并将其转换为 User你自己。或者更好,使用<f:viewParam>其中您可以显式指定转换器。

关于java - 为什么这个参数在我的 Bean 中作为 Null 发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7770666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com