p:commandButton onclick中的EL表达式不会在ajax请求上更新/重新呈现?

时间:2012-01-11 16:05:11

标签: javascript jsf primefaces el

我的onclick的{​​{1}}属性里面有一些EL依赖的Javascript。更具体地说,这是一段代码:

commandButton

<p:commandButton onclick="javascript: if('#{userBean.user.friendList.size() gt 0}' == 'true') deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();" value="Delete all friends?" /> 清除朋友列表并更新deleteFriendsConfirmDialog。朋友列表,commandButton和对话都是这种形式。

所以我点击按钮,确认对话框出现(因为朋友列表的长度是gt 0),我确认并清空列表,视图更新。但是,当我再次单击@form按钮时,会再次出现确认对话框。由于列表的长度现在为0,因此我希望显示错误对话框。

我想,这是因为Delete all friends?中写的Javascript没有更新(虽然按钮在表单中)。

修改:将onclick更改为#{userBean.user.friendList.size() gt 0}也不起作用。

为什么?这有什么不对?

任何帮助表示赞赏。 :)

1 个答案:

答案 0 :(得分:5)

事实上。 PrimeFaces(和标准JSF)不会在每个请求的基础上重新评估on*属性中的EL。它只发生在每个视图的基础上。然而,RichFaces在<a4j:xxx>组件中执行此操作。

您需要以不同方式解决问题。我建议改为使用visible的{​​{1}}属性。

<p:dialog>

另一种方法是在bean的action方法中使用PrimeFaces'<h:form> ... <p:commandButton value="Delete all friends?" update=":deleteDialogs" /> </h:form> ... <h:panelGroup id="deleteDialogs"> <p:dialog id="deleteFriendsConfirmDialog" visible="#{facesContext.postback and not empty userBean.user.friendList}"> ... </p:dialog> <p:dialog id="friendsAlreadyDeletedErrorDialog" visible="#{facesContext.postback and empty userBean.user.friendList}"> ... </p:dialog> </h:panelGroup> ,它允许你以编程方式在bean的action方法中执行JavaScript代码(尽管这与控制器紧密耦合,视图,IMO)

RequestContext

<p:commandButton value="Delete all friends?" action="#{userBean.deleteAllFriends}" />

对于具体问题

无关,您的原始RequestContext context = RequestContext.getCurrentInstance(); if (!user.getFriendList().isEmpty()) { context.execute("deleteFriendsConfirmDialog.show()"); } else { context.execute("friendsAlreadyDeletedErrorDialog.show()"); } 虽然在您的特定情况下无效,却显示了一些不良做法。 onclick伪协议是多余的。这已经是默认值了。去掉它。对javascript:的测试也是多余的。去掉它。让EL直接打印== 'true'true。以下是正确的语法(同样,这不能解决您的问题,只是为了您的信息)

false

如果您使用RichFaces'<p:commandButton onclick="if (#{not empty userBean.user.friendList}) deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();" value="Delete all friends?" /> ,那将会奏效。

<a4j:commandButton>