Search code examples
javaapacheloopstapestry

Tapestry, Get selected item from loop


I'm currently working on some project, using Apache Tapestry 5.3.6. I have issue using t:loop component. Is there any way I can get selected item after loop finishes, and page is rendered? What I need to achieve is: Let's say I have loop like this:

<t:loop t:source="itemList" t:value="item">
    <t:actionlink id="something" context="item.ID"></t:actionlink>
</t:loop>

This will work fine. But if I move this actionlink into my own component, and pass this ID through my parameter, if I click, I always get the last item from list, and not the one which is clicked.

<t:loop t:source="itemList" t:value="item">
    <t:mycomponent myparameter="item.ID"></t:mycomponent>
</t:loop>

I tried putting formState="iteration", and puting ValueEncoder, but nothing helps. Please, can anyone help me, and show me how to solve this issue, and get the selected item from the list. Thanks in advance

Edit: Here is code of my component

public class Ocenjivanje 
{   
    @Parameter(required=true) 
    @Property 
    private int materijalID; 

    private Materijal materijal; 

    @Inject 
    private Session session; 

    @SessionState 
    private User user; 

    @CommitAfter 
    public Object unesiOcenu(int ocena) 
    { 
            Materijal m = (Materijal)session.createCriteria(Materijal.class).add(Restrictions.eq("materijalID", this.materijalID)).list().get(0); 
            Date d = new Date(); 
            Ocena o = new Ocena(); 
            o.setMaterijal(m); 
            o.setKorisnikID(this.user.getID()); 
            o.setDatumOcene(d); 
            o.setOcena(ocena); 
            session.save(o); 
            return this; 
    } 

    public void onActionFromJedan() 
    { 
            unesiOcenu(1); 
    } 

    public void onActionFromDva() 
    { 
            unesiOcenu(2); 
    } 

    public void onActionFromTri() 
    { 
            unesiOcenu(3); 
    } 

    public void onActionFromCetiri() 
    { 
            unesiOcenu(4); 
    } 

    public void onActionFromPet() 
    { 
            unesiOcenu(5); 
    }   
} 

<t:container 
      xmlns="http://www.w3.org/1999/xhtml"
  xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
  xmlns:p="tapestry:parameter">

  Oceni sadržaj:                 
            <t:actionlink t:id="jedan">1</t:actionlink>
            <t:actionlink t:id="dva">2</t:actionlink>
            <t:actionlink t:id="tri">3</t:actionlink>
            <t:actionlink t:id="cetiri">4</t:actionlink>
            <t:actionlink t:id="pet">5</t:actionlink>        


Solution

  • I'm not quite sure what you are trying to achieve but in any case you do not use the context you pass in in your actionlinks and use hardcoded int's in stead. Change your action links to <t:actionlink t:id="tri" context="materijalID">3</t:actionlink> and change your event handlers to

    public void onActionFromJedan(int context) 
        { 
                unesiOcenu(context); 
        }