yes Koskela ,
If you have time go through this code and please give me your valuable suggestions..
if the code is confusing sorry.....
on change function of select tag i am calling one method redirect(x).
here x is selected index of country.
function redirect(x){
document.forms[0].action="index.do?countryId=" + x;
document.forms[0].submit();
}
Long country = new Long(0);
String cCountry = request.getParameter("countryId");
if ((cCountry == null) || ("".equals(cCountry))) {
cCountry = "0";
}
else {
country = new Long(cCountry);
}
%>
index.do gives the same page that is on which page we are selecting country
but populated states according to that...
this is the code for state select...
<html:select property="stateId">
<state:statelist name="states" countryId="<%=country%>"/>
</html:select>
I created one tag library
like this..
-------------------------------------code----------------------------------
public int doStartTag() {
availOptions = new StringBuffer();
/** Queries to be executed */
String queryState =
"SELECT STATE_ID, STATE_NAME FROM STATE WHERE COUNTRY_ID = ?";
try {
/** Open the connection, perform the query and store the results */
Class.forName(driver);
conn = DriverManager.getConnection(hostName, user, password);
conn.setAutoCommit(false);
if (conn != null) {
/** Get the list of states */
prepStmt = conn.prepareStatement(queryState);
prepStmt.setLong(1, this.countryId.longValue());
rsState = prepStmt.executeQuery();
availOptions.append(
"<option value=\"\"<span style=\"font-size: 10.0pt; font-family: Helvetica; color: black\">");
availOptions.append("Select State </span></option>\r\n");
/** Populate with the values of states */
while (rsState.next()) {
String cState = rsState.getString("STATE_NAME");
String cStateId = rsState.getString("STATE_ID");
addOption(availOptions, cState, cStateId,cStateId.equalsIgnoreCase(this.getStateId()));
}
ResponseUtils.write(pageContext, availOptions.toString());
return 0;
}
return SKIP_BODY;
}
catch (Exception ex) {
/** If the database can't be opened, skip everything */
return SKIP_BODY;
}
}
protected void addOption(StringBuffer availOptions, String label, String value, boolean matched) {
availOptions.append("<option value=\"" + value + "\"");
if (matched) {
availOptions.append(" selected=\"selected\"");
}
availOptions.append(
"<span style=\"font-size: 10.0pt; font-family: Helvetica; color: black\">");
availOptions.append(label);
availOptions.append("</span></option>\r\n");
}