Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. An h:dataTable can be configured to use a !DataModel explicitly provided by your code, ie the "value" attribute of the h:dataTable can point to a backing-bean method that returns a !DataModel. If you are doing this, and the !DataModel object is cached in a member of your backing bean, then the "action method" that is triggered can simply access that member and call its getRowData() method to get the object for the "selected row".
  2. If the "value" attribute of the h:dataTable instead points to a backing-bean method that returns a List or similar collection, then the dataTable will internally create a !DataModel to wrap that list. You can access this DataModel instance by using the "actionListener" attribute on the command component, rather than the "action" one. The backing-bean method is then passed an ActionEvent object which contain a reference to the command-component that was clicked on; by walking up the component tree the enclosing UIData component can be found, and its DataModel then retrieved. Actually, it's even simpler, as the "getRowData" method on the UIData component can be used directly (it just delegates to the DataModel method).
    The JSF page looks like this:
    Code Block
    xml
    xml
    <h:commandLink actionListener="#{employeeAction.prepareForEdit}">
        <h:outputText value="#{msgs.edit}" />
    </h:commandLink>
    
    And the backing bean looks like this:
    Code Block
    java
    java
      public void prepareForEdit(ActionEvent anEvent) {
    
        YourBeanClass tmpBean = null;
    
        UIComponent tmpComponent = anEvent.getComponent();
    
        while (null != tmpComponent && !(tmpComponent instanceof UIData)) {
          tmpComponent = tmpComponent.getParent();
        }
    
        if (tmpComponent != null && (tmpComponent instanceof UIData)) {
          Object tmpRowData = ((UIData) tmpComponent).getRowData();
          if (tmpRowData instanceof YourBeanClass) {
            tmpBean = (YourBeanClass) tmpRowData;
    
            //TODO Implementation of your method 
    
          }
        }
    
        //TODO Exception Handling if UIData not found or tmpRowBean of wrong type
    
      }
    {code:java}
    # You could also use the 
  3. You could also use the "binding" attribute on the h:dataTable to make it accessable from the backing bean. However component bindings have their own problems and should be avoided where possible. h3.

Passing

...

params

...

with

...

f:param

...

If

...

you

...

are

...

coming

...

from

...

Struts

...

or

...

some

...

other

...

servlet

...

MVC

...

framework

...

you

...

may

...

have

...

previously

...

solved

...

this

...

problem

...

by

...

passing

...

some

...

kind

...

of

...

primary

...

key

...

as

...

request

...

parameters

...

as

...

part

...

of

...

a

...

link,

...

perhaps

...

via

...

something

...

like

...

this:

...

Code Block
xml
xml
<a href="/appContext/someAction.do?id=1234&userAction=prepareEdit">Edit</a>

Using JSTL to create the above:

...