Kirti Thorat
Kirti Thorat

Reputation: 53038

populate one dropdown on selection of other using struts2 dojo tags

I have two dropdowns. I want to populate the second dropdown on selection of first dropdown. I am using struts-dojo-tags for accomplishing this.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<html>
<head>
<title>Welcome</title>
<script>
</script>
<sx:head />
</head>
<body>
<h2>Struts 2 + Ajax Example 2!</h2>
<s:form name="f1">
<s:textfield name="proj" label="Project Name" />
<s:url id="url" value="/populateStates.action" />
<s:select id="id1" list="countryList"
    listKey="countryId" listValue="countryName" headerKey="0"
    headerValue="-Select-" label="Country"/>
<s:select id="id2" list="list2" listKey="stateId"
    listValue="stateName" headerKey="0" headerValue="-Select-"
    label="State" />
<sx:bind sources="id1" targets="id2" events="onchange"
    href="%{#url}" />
</s:form>
<br>
</body>
</html>

My action class has two action methods.

        public String populateCountry() throws Exception{

    list1 = new ArrayList<Country>();
    list1.add(new Country(1, "Country1"));
    list1.add(new Country(2, "Country2"));
    list1.add(new Country(3, "Country3"));

    return SUCCESS;
}

public String populateStates() throws Exception{

    System.out.println("populateStates******************"+id1);

    list2 = new ArrayList<State>();

    if (id1 != null && !id1.equals("")) 
    {   
        if(id1.equals("1"))
        {
            list2.add(new State(1, "UMB1"));
            list2.add(new State(2, "UMB2"));
            list2.add(new State(3, "UMB3"));
        }
        else if(id1.equals("2"))
        {
            list2.add(new State(4, "UMB4"));
            list2.add(new State(5, "UMB5"));
            list2.add(new State(6, "UMB6"));
        }
        else if(id1.equals("3"))
        {
            list2.add(new State(7, "UMB7"));
            list2.add(new State(8, "UMB8"));
            list2.add(new State(9, "UMB9"));
        }       
    }
    return SUCCESS;
}

First action method is called when the jsp is loaded the first time (with populated Country drop down) and second action method is called when I select a country from the Country dropdown.

Problem: States drop down is not getting populated. When I debug the code, I see that when control reaches populateStates all the variables of my Action class are reset(id1, id2, list1, list2, proj). Due to which I get an empty States dropdown. Please help me to solve this issue.

Upvotes: 1

Views: 4664

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

I don't think <sx:bind> does what you think it does; the target's HTML will be replaced by what the action returns.

You could try returning just the list of <option>s (and perhaps that's what you're doing), but I don't believe it would work. (If it does, definitely follow up with your own answer.)

I'd actually recommend using the jQuery plugin; the Dojo tags have been deprecated for some time, use a very outdated version of Dojo, and often a pain to work with.

Another option would be to just use plain old jQuery (or library of your choice)--it's almost always easier than implementing custom functionality via a tag library. The Ajax tag libraries are great for really simple usecases, but as soon as there's anything custom, I'm not convinced they save much effort. YMMV.

Upvotes: 1

Related Questions