小编典典

实施中 在Struts2中

jsp

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

这是我的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;
}
}

这是bean类。

private String buildingName;
private String block;

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

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


阅读 266

收藏
2020-06-08

共1个答案

小编典典

您的代码中乱七八糟。请记住:

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

    <s:form>
     <!-- stuff -->
     <sj:select ... />
     <!-- stuff -->
     <sj:select ... />
     <!-- stuff -->
    

  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" />
2020-06-08