user1428965
user1428965

Reputation: 11

Struts2 get object attribute from select tag

My problem is that I don't succeed in getting the attribute of the object of the list of my select tag.

I have a select tag in my .jsp like this :

<s:select list="listFonction" listKey="code" listValue="Libelle"
                name="fonctionSelectionne" value="defaultFonction" />

and in my action, i declared an arraylist (with getter and setter) :

private ArrayList<Fonction> listFonction = new ArrayList<Fonction>(); 

I also have another class Fonction :

public class Fonction {
    private int code;
    private String libelle;

    public Fonction(int code, String libelle)
    {
        this.code = code;
        this.libelle =libelle;
    }
    public Fonction()
    {

    }
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }
}

To get the selected value in my action i declared (whith getter and setter):

Private String fonctionSelectionne;

but i am just abbled to get the code (listkey) of my object with getFonctionSelectionne. I want to get the code attribute (listkey) and the libelle attribute (listvalue).

Does anyone know how help me ? thanks

Upvotes: 1

Views: 11625

Answers (2)

Hicham
Hicham

Reputation: 1

You should make the setter of the attribute listFonction on the ClassAction

Upvotes: 0

Jaiwo99
Jaiwo99

Reputation: 10017

2 points:

  1. it should be libelle, not Libelle

    <s:select list="listFonction" listKey="code" listValue="libelle"
                name="fonctionSelectionne" value="defaultFonction" />
    
  2. for list="listFunction", you need getter Collection getListFunction(){} in your action class

UPDATE

i'am not sure to this. but you can give a try.

here is the idea, don't deliver a list, but a map to select tag

Map getListFunction(){
Map<Object, String> map;
Function f = new Function(1, "test");
map.put(f, f.libelle);
return map;
}

then in the jsp:

<s:select list="listFonction" listKey="key" listValue="value"
            name="fonctionSelectionne"/>

Upvotes: 1

Related Questions