【发布时间】:2011-10-25 05:20:41
【问题描述】:
我有两个下拉菜单。我想在选择第一个下拉列表时填充第二个下拉列表。 我正在使用 struts-dojo-tags 来完成此操作。
<%@ 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>
我的动作类有两个动作方法。
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;
}
第一次加载 jsp 时调用第一个操作方法(填充 Country 下拉列表),当我从Country 下拉列表中选择一个国家时调用第二个操作方法。
问题:States 下拉菜单未填充。当我调试代码时,我看到当控制到达populateStates 时,我的Action 类的所有变量都被重置(id1,id2,list1,list2,proj)。因此,我得到一个空的州下拉菜单。请帮我解决这个问题。
【问题讨论】: