简体   繁体   中英

Display selected values in dropdown box in struts 2 page

Highlight selected departments in the dropdown from all available departments in struts 2 page. Form bean is constructed like this

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;
  }     
}

I like to highlight selected departments which is in String[] of search.deptFilter by iterating over it with all the available departments which is in HistForm.deptSelectList. So I wrote like this in my jsp page but if condition <s:if test="%{selectedDept == #dept.name}"> is not working. Also not sure whether this is the right way of showing selected depts from the all depts.

This is the complete code of jsp where I iterate and display the depts. dept.name contains department number and value contains it's text 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>

Not sure why the if condition fails to be true since both are string type. Not sure what's the correct syntax for it. Tried couple of things but none results in true.

You have referenced name property of the dept variable, but it doesn't have such property. List is a generic type and it works with <s:if> expression #dept.name if it's parametrized .

Fix it as

private List<NameValueTO> deptSelectList=new ArrayList<>(); //holds all departments in ta list of NameValueTO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM