2014-09-23 34 views
1

我目前正在使用一個項目,在我的應用程序中有多個選擇框,每個值應該根據第一個列表中選擇的先前值進行更改,這裏是我的代碼,我沒有得到第二個選擇列表。在Struts2中實現<sj:select />

這是我的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中的新成員。

回答

1

代碼中有一個巨大的混亂。請記住:

  1. 每個私有財產需要一個getter和二傳手(在某些情況下可能避免他們,但除非你知道自己在做什麼,總是來生成);特別是,您的bean中的block和您的操作中的hospitalfloor錯過了它們。
  2. 我沒有看到你的bean屬性定義在任何地方,而在JSP頁面中你多次引用它(bean.state,bean.country等)。
  3. 如果您需要發佈一些內容(並且因爲您發佈了select1中的值以填充select2,您需要它),所以您必須將您的元素放入表單中。在<sj:select/>特定情況下,不要放在窗體本身的action屬性,因爲你已經從href屬性<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" /> 
+0

有一個在展示這樣的例子。 – 2016-04-15 10:19:02