简体   繁体   English

如何比较两个在jsp中选择的arraylist和set选项?

[英]How to compare two arraylist and set option selected in jsp?

I have two tables: 我有两个表:

Table 1 geoCountry with columns 表1带列的geoCountry

country_name | country_code

Table 2 user_countries with columns 表2带有列的user_countries

country_name | username 

I want to set users country pre-selected (ie fetching from db) 我想设置用户国家预选(即从数据库中获取)

Here is code that I have tried 这是我尝试过的代码

     <select multiple="multiple" class="country" style="width: 200px">
         <%
           sql6 = "SELECT DISTINCT countryname FROM user_countries WHERE username = ?";
           ps6 = connection.prepareStatement(sql6);
           ps6.setString(1, user);
           rs6 = ps6.executeQuery();

            while (rs6.next()) {
                usercountries.add(rs6.getString(1));
             }

            for (int i = 0; i < geoCountry.size(); i++) {
                 for (int j = 0; j < usercountries.size(); j++) {
                    if (usercountries.get(j).equals(geoCountry.get(i))) {
                 %>
                <option value="<%=geoCountry.get(i)%>" selected="selected"><%=geoCountry.get(i)%></option>
                 <% } else {%>
                 <option value="<%=geoCountry.get(i)%>"><%=geoCountry.get(i)%></option>
              <% }
              }
           } %>
      </select>

it is showing duplicated values in select box .. why? 它在选择框中显示重复的值..为什么?

Remove second cycle with " usercountries", thms. 用“ usercountries” thms删除第二个循环。 like: 喜欢:

  for (int i = 0; i < geoCountry.size(); i++) {
         if (usercountries.contains(geoCountry.get(i)) {
             %>
            <option value="<%=geoCountry.get(i)%>" selected="selected"><%=geoCountry.get(i)%></option>
             <% } else {%>
             <option value="<%=geoCountry.get(i)%>"><%=geoCountry.get(i)%></option>
          <% }
       } %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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