p:commandButton onclick 中的 EL 表达式不会在 ajax 请求上更新/重新呈现? [英] EL expression inside p:commandButton onclick does not update/re-render on ajax request?

查看:21
本文介绍了p:commandButton onclick 中的 EL 表达式不会在 ajax 请求上更新/重新呈现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 commandButtononclick 属性里面有一些依赖于 EL 的 Javascript.更具体地说,这是一段代码:

The onclick attribute of my commandButton has some EL dependent Javascript inside. To be more specific, here is that piece of code:

<p:commandButton
    onclick="javascript: if('#{userBean.user.friendList.size() gt 0}' == 'true') deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
    value="Delete all friends?" />

deleteFriendsConfirmDialog 清除好友列表并更新 @form.好友列表、命令按钮和对话框都是这种形式.

deleteFriendsConfirmDialog clears the list of friends and updates the @form. The list of friends, commandButton and the dialogs are all in that form.

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

So I click the button, confirmation dialog comes up (because the length of friends' list is gt 0), I confirm and the list is emptied, the view is updated. However, when I click the Delete all friends? button once more, the confirmation dialog comes up again. Since the length of the list is now 0, I instead expect the error dialog to show.

那,我猜是因为onclick里面写的Javascript没有更新(虽然按钮在表单中).

That, I guess, is because the Javascript written inside onclick is not updated (although the button is in the form).

#{userBean.user.friendList.size() gt 0} 更改为 #{not empty userBean.user.friendList} 也不起作用.

Changing #{userBean.user.friendList.size() gt 0} to #{not empty userBean.user.friendList} doesn't work neither.

怎么会?这里有什么问题吗?

How come? What's wrong here?

任何帮助表示赞赏.:)

Any help appreciated. :)

推荐答案

确实如此.PrimeFaces(和标准 JSF)不会在每个请求的基础上重新评估 on* 属性中的 EL.它只发生在每个视图的基础上.然而,RichFaces 在 组件中做到了这一点.

Indeed. PrimeFaces (and standard JSF) does not re-evaluate the EL in on* attributes on a per-request basis. It only happens on a per-view basis. RichFaces however does that in <a4j:xxx> components.

您需要以不同的方式解决问题.我建议改用 visible 属性.

You need to solve the problem differently. I suggest to use the visible attribute of the <p:dialog> instead.

<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 方法中使用 PrimeFaces 的 RequestContext,它允许您在 bean 的 action 方法中以编程方式执行 JavaScript 代码(尽管这将控制器与视图紧密耦合了一点, 海事组织).

An alternative is to use PrimeFaces' RequestContext in the bean's action method which allows you to execute JavaScript code programmatically in bean's action method (although this tight-couples the controller a bit too much with the view, 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 打印 truefalse 即可.以下是正确的语法(同样,这并不能解决您的问题,仅供参考)


Unrelated to the concrete problem, your original onclick, while it does not work out in your particular case, shows some poor practices. The javascript: pseudoprotocol is superfluous. It's the default already. Remove it. Also the test against == 'true' is superfluous. Remove it. Just let the EL print true or false directly. The following is the proper syntax (again, this does not solve your problem, just for your information)

<p:commandButton
    onclick="if (#{not empty userBean.user.friendList}) deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
    value="Delete all friends?" />

如果您使用 RichFaces 的 <a4j:commandButton>,它会起作用.

It would have worked if you were using RichFaces' <a4j:commandButton>.

<a4j:commandButton
    oncomplete="if (#{not empty userBean.user.friendList}) deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
    value="Delete all friends?" />

这篇关于p:commandButton onclick 中的 EL 表达式不会在 ajax 请求上更新/重新呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆