Apache Commons Lang 3.xStringEscapeUtils#escapeEcmaScript()
をELで使用することができます。
まず次のようになり/WEB-INF/functions.taglib.xml
を作成します。
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://example.com/functions</namespace>
<function>
<name>escapeJS</name>
<function-class>org.apache.commons.lang3.StringEscapeUtils</function-class>
<function-signature>java.lang.String escapeEcmaScript(java.lang.String)</function-signature>
</function>
</taglib>
そして、次のように/WEB-INF/web.xml
に登録:
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/functions.taglib.xml</param-value>
</context-param>
を次のように次に、あなたがそれを使用することができます。
<html ... xmlns:func="http://example.com/functions">
...
<script>var foo = '#{func:escapeJS(bean.foo)}';</script>
...
<h:xxx ... onclick="foo('#{func:escapeJS(bean.foo)}')" />
また、既にJSFユーティリティー・ライブラリーを使用している場合、あなたはまた、ちょうどその組み込みof:escapeJS()
機能を使用することができます。
<html ... xmlns:of="http://omnifaces.org/functions">
...
<script>var foo = '#{of:escapeJS(bean.foo)}';</script>
...
<h:xxx ... onclick="foo('#{of:escapeJS(bean.foo)}')" />
以下の答えが正しい軌道に乗って私を置きます。 – Ontonomo
が追加されました:xmlns:fn = "http://java.sun.com/jsp/jstl/functions"およびコード '#{fn:replace(_selectedItem.item.webName、 "'、" "}} ' 、 – Ontonomo