【问题标题】:Customize respond after h:ajax call在 h:ajax 调用后自定义响应
【发布时间】:2012-03-23 00:57:38
【问题描述】:

我有类似的东西:

<h:commandButton>
     <h:ajax event="click" listener="#{controller.onLog}" onchange="onLogProcess" />
     ....
</h:commandButton>

我向服务器发送一个帖子,在监听器函数中做一些事情。然后我想向客户端返回一个字符串/消息(响应),并在“onLogProcess”中使用 javascript 捕获它,以向用户显示自定义字符串。

我正在研究它,但我还没有找到任何东西......

有什么方法可以用 JSF 做到这一点?

我正在使用 JSF 2.1 / Mojarra 2.1.2

提前致谢!

【问题讨论】:

    标签: java jsf jsf-2 primefaces


    【解决方案1】:

    &lt;f:ajax&gt;(不是&lt;h:ajax&gt;!)有条件地渲染一个&lt;h:outputScript&gt;

    <h:commandButton action="#{controller.onLog}">
        <f:ajax render="script" />
    </h:commandButton>
    <h:panelGroup id="script">
        <h:outputScript rendered="#{not empty controller.log}">
            onLogProcess('#{controller.log}');
        </h:outputScript>
    </h:panelGroup>
    

    #{controller.log} 是您想要传递给 JS 函数的字符串。确保它不包含 JS 特殊字符,如 '、换行符等。如有必要,请使用 Apache Commons Lang StringEscapeUtils#escapeJavaScript()

    【讨论】:

    • 我明天试试。对此,我真的非常感激。谢谢! @BalusC
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多