Escape JavaScript quotes in EL in a JSF component attribute

后端 未结 2 1514
忘掉有多难
忘掉有多难 2020-12-04 04:15

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.p         


        
2条回答
  •  孤城傲影
    2020-12-04 04:17

    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.

    
    

    Safest bet is to just create an alias with help of .

    E.g.

    
    
    

    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.

    See also:

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

提交回复
热议问题