我目前正在处理一个项目,我的应用程序中有多个选择框,每个值都应根据第一个列表中选择的先前值进行更改,这是我的代码,我没有获得第二个选择列表。
这是我的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 的新手。
最佳答案
您的代码中存在巨大困惑。请记住:
- 每个私有(private)属性都需要一个 Getter 和一个 Setter(在某些情况下可以避免它们,但除非您确切知道自己在做什么,否则总是生成它们);具体来说,
block
在你的 bean 中和hospitalfloor
在你的行动中缺少他们。 - 我没有看到你的
bean
属性定义在任何地方,而在 JSP 页面中您多次引用它(bean.state
、bean.country
等)。 如果您需要发布某些内容(并且由于您要发布 select1 中的值来填充 select2,因此您需要它),则必须将元素放入表单中。在
<sj:select/>
的具体情况下,不要输入action
表单本身中的属性,因为您已经从href
指定了一个操作 url<sj:select/>
的属性s:<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" />
关于java - 实现 <sj :select/> in Struts2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25993547/