使用struts和hibernate(国家,州,城市)的3依赖性下拉

时间:2011-03-23 13:45:07

标签: ajax hibernate combobox struts2 drop-down-menu

我有组合列表。国家,州和城市。我需要根据之前的组合者更改列表,如果我更改国家/地区我需要获得该国家/地区的相应州名单,以及如果我选择州,那么该州的相应城市。任何人都可以帮我找到答案。感谢

public String getStateNCityList() {
    logger.info("Enter getStateNCityList");
    try {
        AddressUtils addrUtil = new AddressUtils();
        this.stateList = addrUtil.getStateList(this.getCountryId());
        this.cityList = addrUtil.getCityList(this.getStateList().get(0).getAddressComboId());
        this.session.put("stateList", this.stateList);
        this.session.put("cityList", this.cityList);
        System.out.println("state list size "+this.stateList.size());
        System.out.println("city list size "+this.cityList.size());
    } catch(Exception e) {
        logger.error(e,e);
        return ERROR;
    }
    logger.info("Exit getStateNCityList");
    return SUCCESS;
}

// addressUtils.java

public List<AddressComboDetails> getCountryList(){
    List<AddressComboDetails> countryList = new ArrayList<AddressComboDetails>();
    try{
    String query=" addressComboTypeId="+ApplicationConstants.COMBO_COUNTRY;
    countryList=addressDao.getAddressComboDetailsByWhereClause(query);
    System.out.println("countryList size" +countryList.size());
    } catch(Exception e) {
        logger.error("Exception in getCountryList" );
        logger.error(e,e);
    }
    return countryList;
}

// hibernate java class

public List<AddressComboDetails> getAddressComboDetailsByWhereClause(
        String whereClause) {
    logger.info("entered getAddresscomboDetailsByWhereClause ");
    List<AddressComboDetails> addressComboList = new ArrayList<AddressComboDetails>();
    Session session = null;

    try {
        session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        String query = " from AddressComboDetails  where " + whereClause;
        System.out.println("Query is "+query);
        addressComboList = (List<AddressComboDetails>) session.createQuery(query).list();
        tx.commit();

    } catch (HibernateException e) {
        logger.error("error in getAddresscomboDetailsByWhereClause"+ e.getMessage());
        logger.error(e,e);
    } finally {
        session.close();
    }
    return addressComboList;
}

//我的jsp页面代码

<tr>
                <td class="generalText"><s:text name="country" /></td>
                <td><s:select listValue="addressComboValue" listKey="addressComboId" list="#session.countryList" onchange="dojo.event.topic.publish('address_country_details');return false;"
                    name="corpAddress.country.addressComboId" />
                    <s:set name="countryId" value="%{corpAddress.country.addressComboId}" /></td>
                    <td><s:url id="corp_country" action="getStateNCityList.action" >
                    <s:param name="countryId" value="%{11}"></s:param></s:url>
                     <sx:div id="country" cssStyle="display:none;" href="%{corp_country}" listenTopics="address_country_details" formId="supplierDetails" showLoadingText=""></sx:div>
            </tr>

1 个答案:

答案 0 :(得分:0)

向国家/地区组合添加“更改”JavaScript事件侦听器,该组合将使用AJAX加载所选国家/地区的状态列表,并更改状态组合的选项。对状态组合执行相同操作,以便加载城市。

AJAX调用可以调用struts操作,返回HTML选项列表或状态/城市的JSON数组。 struts操作应该使用Hibernate查询来查找数据库中的国家(或州的城市)的状态。