1

What is the best way of using special characters and formatting in a dynamically generated h:commandLink?

I am rendering an equation (e.g. A=π*r2) as a list of h:commandLink items, so that each symbol in the equation can be separately clicked:

JSF:

<ui:repeat value="#{eqBean.eqSymbolDisplays}" var="eqSym">
  <h:commandLink value="#{eqSym.text}" styleClass="#{eqSym.styleClass}" action="#{eqBean.eqSymbolClick(eqSym)}" />
</ui:repeat>                                

Bean:

public String getText(){
    // Return the text for a given symbol
}

The question is what the getText method should do when the symbol needs a special character, special mathematical symbol, and/or needs to be a super-script or sub-script?

Here are some specific problems/questions:

(1) How do I use the greek letter π symbol in a CommandLink? If I return &pi; then that is what gets displayed, not the greek symbol

(2) What is the best way to do a superscript in a CommandLink? I could use a CSS style but some people say that is a bad idea, especially when the superscript implies meaning, rather than just presentation, as it does for a number raised to a power. See :

Beware CSS for Superscript/Subcript

Ant Waters
  • 510
  • 1
  • 5
  • 22
  • Perhaps, but I'm open to suggestions on the "right" way to do it if that is not accepted practice. I guess I could use a `cc:if` to make `sub` elements conditionally included? – Ant Waters Jun 22 '15 at 14:52

1 Answers1

2

The answer was obvious in the end: Just replace the value attribute of the h:commandLink with a child h:outputText element that has escape="false" :

JSF:

<ui:repeat value="#{eqBean.eqSymbolDisplays}" var="eqSym">
  <h:commandLink styleClass="#{eqSym.styleClass}" action="#{eqBean.eqSymbolClick(eqSym)}" >
    <h:outputText value="#{eqSym.htmlText}" escape="false"/>
  </h:commandLink>
</ui:repeat>                                
Ant Waters
  • 510
  • 1
  • 5
  • 22
  • If that's the only you need to know then this Q duplicates http://stackoverflow.com/questions/12662851/jsf-2-0-component-to-interpret-string-with-html-code/ – BalusC Jun 24 '15 at 06:49
  • Perhaps, but I needed to use h:commandLink, which doesn't have the attribute "escape", and it took me a while to figure out the solution. – Ant Waters Jun 25 '15 at 08:03