10 Replies Latest reply on Aug 22, 2007 7:52 AM by Thiagu Mariappan

    How to refer object fields dynamically by using EL

    Thiagu Mariappan Novice

      Hai everyone i need to display the Object field value by using Expression Language.
      But i need to refer this Object field name dynamically by using Expression Language.
      Here i don't how to use the nested Expression Language

      Here my sample code is

      #{fields} gives the field name of object ?sample?

      then how to i display ?sample? field value

      i try following ways

      1)#{sample}.#{fields}
      2) #{sample[fields]}

      Please any one help me

      By
      Thiagu.m

        • 1. Re: How to refer object fields dynamically by using EL
          henrik lindberg Apprentice

          Can you elaborate on your question - it is difficult to help without more information.

          Is "fields" a string with the value "sample"?
          What it meaning of the questionsmarks in ?sample? - did you mean "sample".

          What is sample refering to? What type of object?

          1. That is just wrong - does not work
          2. that is the same as sample.y if fields value is "y"

          Don't know if this helps you at all.

          • 2. Re: How to refer object fields dynamically by using EL
            Thiagu Mariappan Novice

            i need to display the object fields value dynamically in my part

            assume "sample" as a object
            this object have multiple fields
            i have the object fields name in "#{row.field}" expression language

            i need to display field value meant how to i combine this expression with "sample" object.

            i try different ways like

            1)<h:outputText value="#{sample}.#{row.field}"/>

            but it simply concatenated the object with field name like "SampleClass@323.variable_name"

            but i need the value of field "variable_name"

            By
            Thiagu.m



            • 3. Re: How to refer object fields dynamically by using EL
              David Spark Newbie

              In general you would use something of the form

              <h:outputText value="#{sample.row.field}"/>

              I'm not entirely clear on your data structure though. Probably the easiest thing to help diagnose your problem would be if you posted your code for the sample class and the code for the page you're trying to display the data on.

              • 4. Re: How to refer object fields dynamically by using EL
                Thiagu Mariappan Novice

                sorry i miss one point

                Assume "sample" is a Object of class "Sample" bean

                i don't know the field names of "Sample" bean

                but the "Sample" bean class field names are in some other bean class "Row"

                then "#{row.variable_name}" gives the field name of "Sample" bean class meant
                How to i display the value of field name "#{row.variable_name}" from "Sample" bean class

                How to i make it dynamic



                this is my Question

                • 5. Re: How to refer object fields dynamically by using EL
                  David Spark Newbie

                  OK, I see what you're trying to do now.

                  I'm not sure if it's possible to it in that way. The approach that I'd probably take would be use code in the backing bean to get the value of a field name that's passed as a variable. So the bean has a function like returnFieldValue(String fieldName) then all you need to do is have something like:

                  <h:outputText value="#{sample.returnFieldValue(fieldName)}"/>

                  Here I'm assuming that you're populating fieldName with the appropriate value (e.g. as the 'var=' of a h:dataTable).
                  Then all you need to do is use Java code to return the value you need :-)

                  I hope this points you in the direction of a workable solution.

                  • 6. Re: How to refer object fields dynamically by using EL
                    Thiagu Mariappan Novice

                    thank for your replay david.spark

                    absolutely the fieldName form dataTable var parameters

                    so i need #{} ,or ${} syntax to get the field name from that parameter

                    if i use

                    <h:outputText value="#{sample.returnFieldValue(#{fieldName})}"/>
                    


                    it gives me syntax error ,
                    i need the fieldName should be dynamic

                    i try this wat too


                    <h:outputText value="#{sample['#{fieldName}']}"/>
                    

                    it also give me the syntax error
                    if i hot code the field name of the "sample" object it gives the correct value

                    By
                    Thiagu.M

                    • 7. Re: How to refer object fields dynamically by using EL
                      David Spark Newbie

                      So if I think your code should look something like this:

                      <h:dataTable value="#{sample.fieldNameList}" var="fieldName">
                       <h:column>
                       <f:facet name="header">Field Name</f:facet>
                       <h:outputText value="#{sample.returnFieldValue(fieldName)}"/>
                       </h:column>
                      </h:dataTable>

                      The important thing is to make sure that the list that makes up the datatable is made up of elements of the right class for passing to your function (I'm presuming String).
                      If it still doesn't work you can try adding some debugging code:
                      System.out.println(fieldName)

                      to the returnFieldValue fucntion in the Java code to see if what's being passed is correct.

                      • 8. Re: How to refer object fields dynamically by using EL
                        Thiagu Mariappan Novice

                        hai mr david.spark this is my table structure

                        Table_Products
                        product property
                        
                        cell phone messaging
                        cell phone camera
                        cell phone Entertainment
                        Car fuel
                        Car price
                        
                        Table_car
                        
                        id fuel price
                        m55 petrol 35000
                        c56 diesel 50000
                        
                        Table_Cellphone
                        
                        id messaging camera Entertainment
                        2288 sms video capture FM
                        4466 email zoom camera games
                        

                        i need to display the all products information under one category at a time.
                        The datatable should be column wise and completely dynamic
                        if i am going to display the ?car? category i have two list for display the dataTable
                        one from ?Table_products? and another one from ?Table_Car?

                        so this is my view part

                        
                        <rich:dataTable var="product" value="#{tbl_products_list_for_car}">
                         <f:facet name="header">
                         <rich:columnGroup>
                         <rich:column>
                         <h:outputText value="property" />
                         </rich:column>
                         <c:forEach items="#{car_list}" var="car">
                         <rich:column>
                         <h:outputText value="#{car .id}"/>
                         </rich:column>
                         </c:forEach>
                         </rich:columnGroup>
                         </f:facet>
                         <rich:column>
                         <h:outputText value="#{product.property}" />
                         </rich:column>
                        
                         <c:forEach items="#{car_list}" var="car">
                         <rich:column>
                         <h:outputText value="#{car}.#{product.property}"/>
                         </rich:column>
                         </c:forEach>
                        </rich:dataTable>
                        

                        this datTable fully dynamic so it is woke for any product category.
                        in the bold font line i have the problem
                        here i don't know about the any products property name, so i incorporate property name form another list

                        but the bold font line simply concatenated the object with field name like "CarClass@323.fuel" but i need to display the value in a dataTable

                        so how to i make EL syntax dynamically

                        By
                        Thiagu.m

                        • 9. Re: How to refer object fields dynamically by using EL
                          David Spark Newbie

                          As far as I am aware it's not possible to do things of the form:

                          <h:outputText value="#{car}.#{product.property}"/>

                          You need to rewrite this as something like:
                          <h:outputText value="#{car.returnPropertyValue(product.property)}"/>

                          and write your own returnPropertyValue method in the car bean that returns the correct value.

                          As an aside I think your problem mostly comes from your database design. If it was me I think I would have one table holding the products (i.e. replace Table_car and Table_Cellphone with a single table) another with product types (containing Car and Cellphone rows) and another with product property types (camera, fuel etc.). It's not a very good idea to hard code the properties as field names as it becomes problematic if you want to add another property.
                          It then becomes far easier to iterate over different things, such as product types for example.

                          • 10. Re: How to refer object fields dynamically by using EL
                            Thiagu Mariappan Novice

                            thank you mr david.spark

                            your replay is very useful for me

                            i create new nethod as you suggest ,and i get the value of that perticular field

                            thank you once again

                            By
                            thiagu.m