1

我目前正在处理一个项目,我的应用程序中有多个选择框,每个值都应根据第一个列表中选择的先前值更改,这是我的代码,我没有得到第二个选择列表。

这是我的jsp。

<s:form id="onSelectList" action="#"> 
  <s:hidden id="c_country" name="buildingName" value="%{bean.building}"></s:hidden>
  <s:hidden id="s_state" name="blockName" value="%{bean.block}"></s:hidden>  
</s:form> 

<s:hidden name="hospitalFloor.buildingName" id="c_country" ></s:hidden>             

<s:url id="loadbuildingurl" action="loadbuildingforFloor"/>
<s:url id="loadstateurl" action="loadblockForFloor"/> 

<s:url id="loadbuildingurl" action="loadbuildingforFloor"/>
<s:url id="loadstateurl" action="loadblockForFloor"/>

<textarea maxlength="200" name="hospitalBlock.blockDescription" id="blockDescription"></textarea> 
<label class="tasks" style="vertical-align:top;"> Select Building :</label>

<sj:select tabindex="10" 
           id="docform_country"
           cssClass="form-control input-sm"
           href="%{loadbuildingurl}"
           name="bean.country"
           list="building"
           onchange="loadValue();"
           onSuccessTopics="s_countryList,s_countryList1"
           onChangeTopics="c_countryList,c_countryList1"
           deferredLoading="false"
           headerKey="-1"
           headerValue="select"
           value="%{bean.country}">
</sj:select>

<label class="tasks" style="vertical-align:top;"> Block :</label>

<sj:select tabindex="10"
           id="docform_state"
           cssClass="form-control input-sm"
           href="%{loadstateurl}"
           name="bean.state"
           list="block"
           formIds="onSelectList"
           onchange="loadValue();"
           onSuccessTopics="s_stateList,s_stateList1"
           onChangeTopics="c_stateList,c_stateList1"
           reloadTopics="c_countryList"
           deferredLoading="false"
           headerKey="-1"
           headerValue="select"
           value="%{bean.state}">
 </sj:select>

这是我的 struts.xml。

<action name="loadbuildingforFloor" class="hart.hospitalManagement.HospitalBuildingListForFloorAction">
    <result name="success" type="json"></result>                                    
</action>

<action name="loadblockForFloor" class="hart.hospitalManagement.HospitalBlockListForFloorAction" >
    <result name="success" type="json"></result>
</action>

这是我的第二个选定列表的 Java 操作类。

package hart.hospitalManagement;

import hart.bean.HospitalFloor;
import hart.profilemanagement.DoctorRegistrationHelper;

import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

public class HospitalBlockListForFloorAction extends ActionSupport {

    private Map<String, String> block;
    private String buildingName;
    private HospitalFloor hospitalfloor;

    public Map<String, String> getBlock() {
        return block;
    }
    public void setBlock(Map<String, String> block) {
        this.block = block;
    }
    public String getBuildingName() {
        return buildingName;
    }
    public void setBuildingName(String buildingName) {
        this.buildingName = buildingName;
    }       

    public String setBlockListForFloor()
    {    
        HospitalFloorManagementHelper hospitalFloorManagementHelper=new HospitalFloorManagementHelper();
        block=hospitalFloorManagementHelper.getBlockListForFloor(buildingName);
        System.out.println("building Name are"+buildingName);
        System.out.println("block list are"+block);
        return SUCCESS;
    }
    public String getJSON()
    {
        return setBlockListForFloor();
    }    
}

这是助手类。

public Map<String, String> getBlockListForFloor(String buildingName)
{
    int buildingId=0;
    Map<String, String> block=new HashMap<>();
    DbConnect db=new DbConnect();
    Connection con=db.returnConnection();

    try
    {
        Statement stmt1=con.createStatement();
        Statement stmt2=con.createStatement();
        ResultSet rs1=stmt1.executeQuery("select buildingId from hospital_building where buildingName='"+buildingName+"'");
        while(rs1.next())
        {
            buildingId=rs1.getInt("buildingId");

        }
        ResultSet rs2=stmt2.executeQuery("select blockName from hospital_block where buildingId='"+buildingId+"'");
        while(rs2.next())
        {
            block.put(rs1.getString("blockName"), rs1.getString("blockName"));
        }               
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        db.closeConnection(con);
    }

    return block;
}
}

这是豆类。

private String buildingName;
private String block;

public String getBuildingName() {
    return buildingName;
}
public void setBuildingName(String buildingName) {
    this.buildingName = buildingName;
}

但是我无法获得第二个选定列表,请有人可以帮助我吗?我是struts2的新手。

4

1 回答 1

1

您的代码中有一个巨大的混乱。请记住:

  1. 每个私有属性都需要一个 Getter 和一个 Setter(在某些情况下可以避免使用它们,但除非您确切知道自己在做什么,否则请始终生成它们);具体来说,block在您的 bean 和hospitalfloor您的操作中都缺少它们。
  2. 我没有在bean任何地方看到您的属性定义,而在 JSP 页面中您多次引用它(bean.statebean.country)。
  3. 如果您需要发布某些内容(并且由于您要发布来自 select1 的值以填充 select2,因此您需要它),您必须将您的元素放入表单中。在特定情况下<sj:select/>,不要action在表单本身中添加属性,因为您已经从 s 的href属性中指定了操作 url <sj:select/>

    <s:form>
         <!-- stuff -->
         <sj:select ... />
         <!-- stuff -->
         <sj:select ... />
         <!-- stuff -->
    </s:form> 
    
  4. 您的主题未在任何地方定义
  5. 您的第二个选择主题递归地通知第一个选择。这是错误的。

流程应该是:

    <sj:select name = "bean.country" 
               list = "countries" 
            listKey = "id"
          listValue = "description"
               href = "%{loadCountriesAction}"
              value = "%{bean.country}"
     onChangeTopics = "reloadState" 
   onCompleteTopics = "reloadState" />

    <sj:select name = "bean.state" 
               list = "states" 
            listKey = "id"
          listValue = "description"
               href = "%{loadStatesAction}"
              value = "%{bean.state}" 
       reloadTopics = "reloadState" 
    deferredLoading = "true" />
于 2014-09-23T12:03:39.697 回答