Prashant Sehta
Prashant Sehta

Reputation: 33

How to populate a <s:select> box according to the selected value of the another <s:select> box

I have two struts2 select box in my jsp page. The first select box is populated by a object whose values are set on server side.

This object have two elements:

String softwareName;
Map<String,String> versionList;

The first select box is populated by this object. Listkey property of this select box uses versionList and ListValue of this select box uses softwareName. Like this:

<s:select name="listSoftware" list="listSoftware" listkey="versionList" listvalue="softwareName">

So the select box in html become like this:

<select name="listSoftware">
    <option value="DummySoftware-1.0.0=1.0.0,DummySoftware-2.0.0=2.0.0,DummySoftware-3.0.0=3.0.0">DummySoftware</option>
    <option value="TestSoftware-4.0.0=4.0.0,TestSoftware-5.0.0=5.0.0,TestSoftware-6.0.0=6.0.0">TestSoftware</option>
    <option value="CheckSoftware-7.0.0=7.0.0,CheckSoftware-8.0.0=8.0.0,CheckSoftware-9.0.0=9.0.0">CheckSoftware</option>
</select>

Now i want to populate the second select box according to the software selected from the first select box. For example, if user select DummySoftware from first select box, the corresponding versions of DummySoftware i.e. 1.0.0, 2.0.0 and 3.0.0 should be populated in second select box.

Can anybody help how to do this?

Upvotes: 1

Views: 1260

Answers (2)

coding_idiot
coding_idiot

Reputation: 13734

You can use doubleselect

Reference

Action

private List<BeanFirst> lstFirst = new ArrayList<BeanFirst>();

public String execute() {
    List<BeanSecond> lstSecond = new ArrayList<BeanSecond>();
    lstSecond.add(new BeanSecond(1, "sec1"));
    lstSecond.add(new BeanSecond(2, "sec2"));

    List<BeanSecond> lstSecond2 = new ArrayList<BeanSecond>();
    lstSecond2.add(new BeanSecond(3, "sec3"));
    lstSecond2.add(new BeanSecond(4, "sec4"));


    BeanFirst f1 = new BeanFirst(1, "name1", lstSecond);
    BeanFirst f2 = new BeanFirst(22, "name2", lstSecond2);
    lstFirst.add(f1);
    lstFirst.add(f2);

    return SUCCESS;
}

BeanFirst.java

public class BeanFirst
{
    private Integer id;
    private String name;
    private List<BeanSecond> lst;

//Getters & Setters
}

BeanSecond.java

public class BeanSecond
{
    private Integer id;
    private String name;

   //Getters & Setters
    }

JSP

<s:doubleselect list="lstFirst" listKey="id" listValue="name" name="idfirst" doubleList="lst" doubleName="idsecond" doubleListKey="id" doubleListValue="name" label="Double Select Here"/>

Upvotes: 1

April_Nara
April_Nara

Reputation: 1046

Something like this is what you want but what you want to do is take the validate from one to then generate your result.jsp with both the selects in it again with the now pre-populated versions. So basically add a jsp to this. http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/

Upvotes: 1

Related Questions