[英]Implementing <sj:select /> in Struts2
我當前正在處理一個項目,我的應用程序中有多個選擇框,每個值應根據在第一個列表中選擇的上一個值進行更改,這是我的代碼,我沒有獲得第二個選擇列表。
這是我的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的新手。
您的代碼中亂七八糟。 請記住:
block
自己的豆子,在hospitalfloor
的地板上丟失它們。 bean
屬性,而在JSP頁面中您多次引用了它( bean.state
, bean.country
等)。 如果需要發布某些內容(並且由於要從select1發布值以填充select2,則需要它),則必須將元素放入表單中。 在<sj:select/>
的特定情況下,不要在表單本身中放置action
屬性,因為您已經從<sj:select/>
的href
屬性中指定了動作網址:
<s:form> <!-- stuff --> <sj:select ... /> <!-- stuff --> <sj:select ... /> <!-- stuff --> </s:form>
流程應為:
<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" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.