You can create a custom component that extends HtmlOutputText
(<h:outputText>
). Here an example that i have used for something similar, but showing all expressions (beans, resurces and implicit objects), not only resource variable:
The custom component:
@FacesComponent("com.mycompany.component.CustomOutputText")
public class CustomOutputText extends HtmlOutputText {
public static final String ATTRIBUTE_VALUE = "value";
@Override
public String getFamily() {
return "com.mycompany.component.customFamily";
}
public String getValue() {
return (String) getStateHelper().eval(ATTRIBUTE_VALUE);
}
public String getNotEvalValue() {
return (String) getValueExpression(ATTRIBUTE_VALUE).getExpressionString();
}
public void setValue(String value) {
getStateHelper().put(ATTRIBUTE_VALUE, value);
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("span", this);
writer.append(getValue());
writer.endElement("span");
writer.startElement("p", this);
writer.append("[expression: " + getNotEvalValue() + "]");
writer.endElement("p");
}
}
The taglib:
<namespace>http://com.mycompany/custom-components</namespace>
<tag>
<tag-name>customOutputText</tag-name>
<component>
<component-type>com.mycompany.component.CustomOutputText</component-type>
<renderer-type>com.mycompany.component.CustomOutputText</renderer-type>
</component>
<attribute>
<name>value</name>
<required>true</required>
<type>java.lang.String</type>
</attribute>
</tag>
Register in web.xml:
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/custom.taglib.xml</param-value>
</context-param>
And, to test, a simple managed bean (in this case, CDI)
@Named
@ViewScoped
public class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
public String getSample() {
return "sample text from bean";
}
}
In the xhtml include the taglib, and use the component:
<custom:customOutputText value="#{myBean.sample}" />
And the output is:
sample text from bean
[expression: #{myBean.sample}]
Note that the component will generate 2 html tags, 1 <span>
and 1 <p>
, consider this for app css styles.