在Struts2中实现<sj:select> </sj:select>

时间:2014-09-23 11:08:56

标签: java jsp struts2 struts2-jquery struts2-jquery-plugin

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

这是我的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 个答案:

答案 0 :(得分:1)

您的代码中存在巨大的混乱。请记住:

  1. 每个私人财产都需要一个Getter和一个Setter(在某些情况下可以避免它们,但除非你确切地知道你在做什么,否则总是生成它们);具体而言,您的bean中的block和您的操作中的hospitalfloor会遗漏它们。
  2. 我没有在任何地方看到您的bean属性,而在JSP页面中您多次引用它(bean.statebean.country等。)
  3. 如果您需要发布某些内容(因为您要从select1发布值以填充select2,您需要它),您必须将您的元素放在表单中。在<sj:select/>的特定情况下,请勿在表单中添加action属性,因为您已经在href <sj:select/>属性中指定了操作网址} S:

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

        <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" />