How to output EL as example

随声附和 提交于 2019-12-24 20:48:13

问题


I am creating Facelet components for JSF to be used by other developers in my company. To help those, we have example pages set up, where you could see working examples and the source code to copy, paste and modify in other pages.

I need to show an examples of how to use EL in attributes, but I cannot get the EL to be printed out as String without an ugly workaround like

attribute="<h:outputText value="#"/>
<h:outputText value="{"/>
<h:outputText value="myExpression"/>
<h:outputText value="}"/>"

It works this way, but looks pretty dull to me. How to achieve my goal leaner and/or cleaner?


回答1:


From the Expression Language Specification Version 2.2 :

To generate literal values that include the character sequence "${" or "#{", the developer can choose to use a composite expression as shown here:

${'${'}exprA}
#{'#{'}exprB}

The resulting values would then be the strings ${exprA} and #{exprB}.

Alternatively, the escape characters \$ and \# can be used to escape what would otherwise be treated as an eval-expression. Given the literal-expressions:

\${exprA}
\#{exprB}



回答2:


You can't directly input JSF in attributes without escaping it of course, it is not XML compliant to use < and > inside them. Also as you already seen #{} are rendered.

The easy way :

import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;

public class Bean
{
    public String getValue()
    {
        return escapeHtml("<h:outputText value=\"#{myExpression}\" />");
    }
}

<yourComponent attribute="#{bean.value}" />

The hard way :

<yourComponent attribute="&lt;h:outputText value=&quot;{myExpression}&quot; /&gt;" />


来源:https://stackoverflow.com/questions/16833379/how-to-output-el-as-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!