提问人:gcpdev-guy 提问时间:12/20/2021 最后编辑:Roman Cgcpdev-guy 更新时间:12/30/2021 访问量:342
在支柱 2 页面的下拉框中显示所选值
Display selected values in dropdown box in struts 2 page
问:
突出显示支柱 2 页面中所有可用部门下拉列表中的选定部门。 Form Bean 是这样构造的
public class HistForm {
private List deptSelectList=new ArrayList(); //holds all departments in ta list of NameValueTO
private SearchTO search= new SearchTO();
public List getDeptSelectList() {
return deptSelectList;
}
public void setDeptSelectList(List deptSelectList) {
this.deptSelectList = deptSelectList;
}
public SearchTO getSearch() {
return search;
}
public void setSearch(SearchTO search) {
this.search = search;
}
}
public class SearchTO implements Serializable {
private String[] deptFilter = new String[0];
public String[] getDeptFilter() {
return deptFilter;
}
public void setDeptFilter(String[] deptFilter) {
this.deptFilter = deptFilter;
}
}
public class NameValueTO implements java.io.Serializable {
private String name;
private String value;
public NameValueTO() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
我喜欢突出显示 search.deptFilter 的 String[] 中的选定部门,方法是使用 HistForm.deptSelectList 中的所有可用部门对其进行迭代。所以我在我的 jsp 页面中这样写,但如果条件不起作用。也不确定这是否是从所有部门中显示所选部门的正确方式。<s:if test="%{selectedDept == #dept.name}">
这是我迭代和显示部门的 jsp 的完整代码。 dept.name 包含部门编号,value 包含其文本值。
<select name="search.deptFilter" id="search.deptFilter" multiple="multiple" size="5" onchange="getClasses()">
<option value="-1">Select a Deparment </option>
<s:if test="%{search.deptFilter != NULL && search.deptFilter.length > 0}">
<s:iterator value="search.deptFilter" var="selectedDept">
<s:iterator value="deptSelectList" var="dept">
<s:if test="%{selectedDept == #dept.name}">
<option value="<s:property value='%{#dept.name}'/>" selected="selected"><s:property value="%{#dept.value}"/>-SELECTED</option>
</s:if>
<s:else >
<option value="<s:property value='%{#dept.name}'/>"><s:property value="%{#dept.value}"/></option>
</s:else>
</s:iterator>
</s:iterator>
</s:if>
<s:else>
<s:iterator value="deptSelectList" var="dept">
<option value="<s:property value='%{#dept.name}'/>"><s:property value="%{#dept.value}"/></option>
</s:iterator>
</s:else>
</select>
不确定为什么 if 条件不为 true,因为两者都是字符串类型。不确定它的正确语法是什么。尝试了几件事,但没有一个结果是真的。
答:
0赞
gcpdev-guy
12/30/2021
#1
以这种方式修复它。
<select name="search.deptFilter" id="search.deptFilter" multiple="multiple" size="5" onchange="getClasses()">
<option value="-1">Select a Department </option>
<s:if test="%{search.deptFilter != NULL && search.deptFilter.length > 0}">
<s:iterator value="%{deptSelectList}" var="dept">
<option value="<s:property value='%{#dept.name}'/>"
<s:if test="%{#dept.name in search.deptFilter}">
selected="selected"
</s:if>
>
<s:property value="%{#dept.value}"/>
</option>
</s:iterator>
</s:if>
<s:else>
<s:iterator value="deptSelectList" var="dept">
<option value="<s:property value='%{#dept.name}'/>"><s:property value="%{#dept.value}"/></option>
</s:iterator>
</s:else>
</select>
评论