1

I am trying to render richfaces and jsf UI elements dynamically based on the dataType value.

Ex : I have a enum as below

public enum DataType {
    DT_LONGLONG(1), DT_STRING(2), DT_LONG(3), DT_DATE(4), DS_EXTERNALREFERENCE(5), 
    DT_BOOLEAN(6), DT_FLOAT(7), DT_SHORT(8);
}

Then in xhtml page while iterating through the list of my custom objects, I check for the dataType and render the UI elements accordingly as below :

<c:if test="#{meaCompPartAttr.dataType.dataType == 2}">
    <h:inputText />
</c:if>
<c:if test="#{(meaCompPartAttr.dataType.dataType == 1) or
        (meaCompPartAttr.dataType.dataType == 3) or
        (meaCompPartAttr.dataType.dataType == 8)}">
    <h:inputText onkeyup="javascript:validateField(this,             '#{tpMsgs.longRegularExpression}');">
    <f:validateLongRange/>
    </h:inputText>
</c:if>
<c:if test="#{meaCompPartAttr.dataType.dataType == 7}">
<h:inputText onkeyup="javascript:validateField(this, '#{tpMsgs.doubleRegularExpression}');">
    <f:validateDoubleRange/>
    </h:inputText>
</c:if>
<c:if test="#{meaCompPartAttr.dataType.dataType == 6}">
    <h:selectBooleanCheckbox />
</c:if>
<c:if test="#{meaCompPartAttr.dataType.dataType == 4}">
    <rich:calendar />
</c:if>

Because of this I usually get class cast exceptions like String to Boolean or Long to String etc. I assume this is happening coz jstl and jsf code do not run in sync.

Is there any other approach to render UI elements dynamically as proposed in the above sample?

Satya
  • 2,094
  • 6
  • 37
  • 60

1 Answers1

2

So you're iterating using <ui:repeat> or <h:dataTable> or any other JSF iterating component instead of the JSTL <c:forEach>? Either use <c:forEach> instead, or use the rendered attribute instead of <c:if>.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I use c:forEach to iterate through the component. I suppose to give a try with rendered attribute now. – Satya Dec 09 '11 at 15:08
  • But would like to know is that the correct way of checking enum value in my view? Or can't I straight forward do something : – Satya Dec 09 '11 at 15:09
  • Well, then your problem is caused by something else. It's hard to give a targeted answer on such little information about the problem. Edit: well, you can just use `#{meaCompPartAttr.dataType == 'DT_BOOLEAN'}`. See also the 2nd "See also" link. – BalusC Dec 09 '11 at 15:09
  • Finaly got some time to go thru the links and they are really of great help, helped me to solve the problem. Thanks a lo to BalusC – Satya Dec 21 '11 at 12:10