在JSF组件属性中转义EL中的JavaScript引号 [英] Escape JavaScript quotes in EL in a JSF component attribute

查看:153
本文介绍了在JSF组件属性中转义EL中的JavaScript引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个取决于变量状态的onclick事件。所以我需要做类似的事情

I have an onclick event that depends on the state of a variable. So I need to do something like

onclick="#{object.isEnabled ? 'ch.my.js_method('#{object.property}'); return false;' : 'return false;'"

问题是,我不能在onclick中使用'。如何逃避EL中的引号?

The problem is, that I can't use ' inside of the onclick. How can I escape the quotes inside of the EL?

推荐答案

您可以使用 \ <转义它们/ code>或 \\ ,但每个EL实现的规则不同,因为EL规范不明确(Oracle的EL impl需要单反斜杠,但Apache需要双反斜杠)。你需要 + = 运算符来串联连接EL表达式。

You can escape them with \ or \\, but rules differ per EL implementation, because it isn't clear cut in EL spec (Oracle's EL impl needs single backslash, but Apache needs double backslash). And you need the += operator to string-concatenate the EL expression.

<x:someComponent onclick="#{object.isEnabled ? 'ch.my.js_method(\'' += object.property += '\'); return false;' : 'return false;'}" />

最安全的做法是在< c:的帮助下创建一个别名:设置>

例如

<c:set var="js_method" value="ch.my.js_method('#{object.property}'); return false;" />
<x:someComponent onclick="#{object.isEnabled ? js_method : 'return false;'}" />

如果#{object.property},你仍然只是一个潜在的问题可以包含JS特殊字符,这些字符又会导致无效的JS输出,例如单引号或换行符。前往第二个链接寻求解决方案。

You've only still a potential problem if #{object.property} can contain JS-special characters which can in turn cause invalid JS output, such as single quotes or newlines. Head to the second link below for the solution to that.

  • How to concatenate Strings in EL expression?
  • How do I pass JSF managed bean properties to a JavaScript function?

这篇关于在JSF组件属性中转义EL中的JavaScript引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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