"Simple Software"

OcpSoft

JSF’s <h:dataTable> vs <ui:repeat> – How to get the selected row.

September 14th, 2009 by Lincoln

So, a little while ago I was attempting to use JSF’s Facelets <ui:repeat> tag, as a replacement for <h:dataTable>, but difficulty came when I needed to process actions on individual records of each row.

<ui:repeat> allows iteration over a List of Array[] of items, but it does not provide a method of discovering the “selected” or “actioned” row; there’s no way to discover the row the user is interacting with. Or is there?

I’ve since learned of two ways to deal with this situation:

1. Use ListDataModel

I asked another JSF Expert, Dan Allen (author of Seam in Action) and here’s what he told me:

From Dan’s email, I learned that you could place a List of objects in a ListDataModel wrapper, effectively binding the state of the list to the UI. With that done, you can manipulate the data in an action-method, being able to retrieve the selected row index, object itself, and continue as you would expect with your programming.

2. Upgrade to an advanced EL (EL2) Jar

Probably my favorite solution – you can use EL2 method invocation, provided either by Seam, or Sun’s reference implementation, and pass the desired objects directly to methods in your JSP/Facelet code. It’s coming standard in J2EE 6, but why wait?

This method is simple, intuitive, and enables much more powerful and reusable Beans to handle page code, reducing redundancy in Model classes and business logic.

Note how I pass the current var object directly to the method via EL:

<h:commandButton id="add" value="Add Task" 
    action="#{itemController.addItemNote(currentItemBean.item, addNoteBean.newNote)}">

And here’s the Backing Bean code:

@ManagedBean
@RequestScoped
public class ItemController
{
    public String addTask(final Item item, final Note note)
    {
        ItemService.addNote(item, note);
        return "pretty:viewItem";
        // this return statement is a PrettyFaces JSF bookmarking navigation id
    }

For instructions on how to: include EL2 in a JSF project go here.

For more information on JSF SEO and Bookmarking, take a look at PrettyFaces: URL rewriting extension for JSF.

If you are at all confused by these examples, feel free to leave a comment, and I’ll do my best to help.

Post to Twitter Post to Delicious Post to Digg Post to StumbleUpon

Posted in JSF

9 Comments

  1. Geert says:

    I’ve been craving to be able to just pass a parameter into action methods!
    Since I can’t use Seam I’ve been using
    It leads to a setProp(someval) prior to invocation of the action method.

    Anyway I know passing parameters is possible in Seam, but can’t find any references to it in J2EE6, or to “advanced EL2″ outside of Seam. Can you provide any URLs for more info on the standard (non-seam) implementations of this?
    thanks!

  2. Geert says:

    oops some markup was removed from previous post. That should have been:

    <f:setPropertyActionListener target="#{bean.prop}" value="someval" />

  3. Lincoln says:

    I’ve added the Maven repository and artifactId for each implementation, hope this helps.

  4. Pls help on getting dynamic ids says:

    I am trying to implement suggestion box in ui repeat. First suggestionBox works fine as expected:
    a) somehow when I use the same in ui repeat suggestionAction event is not fired..
    b) Sugeestion box uses id to display its content, problem is when user clicks “Add More regs” new row is added and id needs to be changed, please suggest how can handle dynamic ids to render suggestion box value as in code below.

    <rich:suggestionbox id="suggestionBoxId" for="text" tokens=",[]"

    We are using jboss4.2.3 and richfaces 3.3.1, jdk5

    Please give your valuable inputs it will be very helpful.

    XHTML

    //THIS WORKS FINE

    //NOT WORKING, SUGGESTIONACTION EVENT IS NOT FIRED

    ….

    Save…

  5. Scott Langley says:

    Do you know if the syntax for the advanced features in JBoss EL and the new Sun EL 2.1.2 in J2EE 6 / Glassfish 3 are completely compatible? That is, is either jar a drop-in replacement for the other?

    Thanks.

    1. Lincoln says:

      They are mostly the same, if I am not mistaken! Though Seam EL may provide some additional functions off of the List type.

      1. ved says:

        Hi Lincoln,
        I have problem in datatable of jsf. i want to create dynamic data. my complete data in a double array you can suppose.
        like

        private Set bundleColumns;

        :-complete column so i can figure out how many rows will need to populated.

        Map<rowId here,Map> bundleMap;

        so how i can draw on server side please let me know. i also try few examples but did not get any solution. its urgent please reply me. my id is ved.java@gmail.com

  6. Lincoln says:

    Ved,

    I’m not sure what you are asking.

    Could you please give an example of what you’re trying to do?

  7. krishna says:

    Hi Lincoln, can we have something like highlite aparticular row in a datatable?

Leave a Comment




Please note: In order to submit code or special characters, wrap it in <pre lang="java"> </pre> - or your tags will be eaten.

Please note: Comment moderation is enabled and may delay your comment from appearing. There is no need to resubmit your comment.

Search

Recent Posts

Recent Comments

OcpSoft Agile Management

Recent Tweets:

Posting tweet...

Sponsored By:

Resources

Meta