title: Display selected values in dropdown box in struts 2 page
body: <p>Highlight selected departments in the dropdown from all available departments in struts 2 page.
  Form bean is constructed like this</p>
  <pre class="lang-java prettyprint-override"><code>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;
    }     
  }
  </code></pre>
  <p>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 <code>&lt;s:if test=&quot;%{selectedDept == #dept.name}&quot;&gt;</code> is not working. Also not sure whether this is the right way of showing selected depts from the all depts.</p>
  <p>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.</p>
  <pre class="lang-html prettyprint-override"><code>&lt;select name=&quot;search.deptFilter&quot; id=&quot;search.deptFilter&quot; multiple=&quot;multiple&quot; size=&quot;5&quot; onchange=&quot;getClasses()&quot;&gt;
    &lt;option value=&quot;-1&quot;&gt;Select a Deparment &lt;/option&gt;
    
    &lt;s:if test=&quot;%{search.deptFilter != NULL &amp;&amp; search.deptFilter.length &gt; 0}&quot;&gt;
      &lt;s:iterator value=&quot;search.deptFilter&quot; var=&quot;selectedDept&quot;&gt;
        &lt;s:iterator value=&quot;deptSelectList&quot; var=&quot;dept&quot;&gt;
            &lt;s:if test=&quot;%{selectedDept == #dept.name}&quot;&gt;
                &lt;option value=&quot;&lt;s:property value='%{#dept.name}'/&gt;&quot; selected=&quot;selected&quot;&gt;&lt;s:property value=&quot;%{#dept.value}&quot;/&gt;-SELECTED&lt;/option&gt;
              &lt;/s:if&gt;
  
              &lt;s:else &gt;
                &lt;option value=&quot;&lt;s:property value='%{#dept.name}'/&gt;&quot;&gt;&lt;s:property value=&quot;%{#dept.value}&quot;/&gt;&lt;/option&gt;
              &lt;/s:else&gt;   
          &lt;/s:iterator&gt;
      &lt;/s:iterator&gt;
    &lt;/s:if&gt;
  
    &lt;s:else&gt;
      &lt;s:iterator value=&quot;deptSelectList&quot; var=&quot;dept&quot;&gt;
        &lt;option value=&quot;&lt;s:property value='%{#dept.name}'/&gt;&quot;&gt;&lt;s:property value=&quot;%{#dept.value}&quot;/&gt;&lt;/option&gt;
      &lt;/s:iterator&gt;   
    &lt;/s:else&gt;
   &lt;/select&gt;
  </code></pre>
  <p>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.</p>