我的commandButton
的onclick
属性内部有一些依赖EL的Javascript。更具体地说,这是一段代码:
<p:commandButton
onclick="javascript: if('#{userBean.user.friendList.size() gt 0}' == 'true') deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
value="Delete all friends?" />
CCD_ 3清除好友列表并更新CCD_。好友列表、commandButton和对话框都是这种形式
所以我点击按钮,出现确认对话框(因为朋友列表的长度为gt 0),我确认了,列表被清空,视图被更新。但是,当我再次单击Delete all friends?
按钮时,确认对话框再次出现。由于列表的长度现在为0,因此我希望显示错误对话框。
我想,这是因为onclick
中编写的Javascript没有更新(尽管按钮是表单中的)。
编辑:将#{userBean.user.friendList.size() gt 0}
更改为#{not empty userBean.user.friendList}
也不起作用。
为什么?这里怎么了?
感谢您的帮助。:)
确实如此。PrimeFaces(和标准JSF)不会在每个请求的基础上重新评估on*
属性中的EL。它只发生在每个视图的基础上。然而,RichFaces在<a4j:xxx>
组件中做到了这一点。
你需要以不同的方式解决问题。我建议使用<p:dialog>
的visible
属性。
<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的操作方法中使用PrimeFaces的RequestContext
,这允许您在bean的动作方法中以编程方式执行JavaScript代码(尽管这会使控制器与视图IMO过于紧密地耦合)。
<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:
伪协议是多余的。这已经是默认值了。去掉它。而且针对== 'true'
的测试也是多余的。取下它。只需让EL直接打印true
或false
即可。以下是正确的语法(同样,这并不能解决您的问题,仅供参考)
<p:commandButton
onclick="if (#{not empty userBean.user.friendList}) deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
value="Delete all friends?" />
如果您使用RichFaces的<a4j:commandButton>
,它会起作用。
<a4j:commandButton
oncomplete="if (#{not empty userBean.user.friendList}) deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
value="Delete all friends?" />