Display selected values in dropdown box in struts 2 page

403 Views Asked by At

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.

1

There are 1 best solutions below

0
gcpdev-guy On

Fixed it this way.

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