Set id of a component within JSF dataTable to value from current item in the array

前端 未结 2 2002
萌比男神i
萌比男神i 2020-12-14 10:22

I\'m having a datatable where I would like to set the id of each row to the id of the current item (object that has an id field) in the array that builds the table.

2条回答
  •  自闭症患者
    2020-12-14 11:06

    From the JSF UI components, the id and binding attributes are evaluated during view build time, the moment when the XML tree structure in the XHTML/JSP file is to be parsed and converted to a JSF component tree as available by FacesContext#getViewRoot(). However, the iterates during view render time, the moment when the JSF component tree needs to produce HTML code by UIViewRoot#encodeAll(). So, at that moment the id attribute is evaluated, the #{item} is nowhere available in the EL scope and evaluates to null which ultimately prints an empty string.

    There are basiclly 3 solutions:

    1. Use a view build time tag like JSTL so that the #{item} is available during view build time as well.

      
              

      See further also JSTL in JSF2 Facelets... makes sense?

    2. Don't print it as ID of a JSF component, but of a plain HTML element.

      
      

      Please note that IDs starting with a digit are invalid in HTML as per HTML spec chapter 6.2. You might want to prefix it with some string like so:

      
      
    3. Don't use a dynamic ID. Just use a fixed ID. JSF will autogenerate an unique ID based on row index anyway.

      
      

      This will end up in like provided that it's inside a . The 0 is the 0-based row index which increments every row. This thus guarantees an unique ID in every row without the need to worry about it yourself.

    4. 提交回复
      热议问题