我有类似的东西:
<h:commandButton>
<h:ajax event="click" listener="#{controller.onLog}" onchange="onLogProcess" />
....
</h:commandButton>
我向服务器发送一个帖子,在监听器函数中执行一些操作。然后我想返回一个字符串/消息(响应)给客户端,并使用“onLogProcess”中的javascript捕获它,以向用户显示自定义字符串。
我正在研究它,但我还没有发现任何东西......
有什么办法可以用 JSF 做到这一点吗?
我正在使用 JSF 2.1/Mojarra 2.1.2
提前致谢!
最佳答案
让<f:ajax>
(不是<h:ajax>
!)有条件地渲染<h:outputScript>
.
<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()
为此。
关于java - 自定义在 h :ajax call 之后响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9832784/