0

The select option value are dynamically bind in jsp page but the if the first value is Views all other option value are becoming Views like wise, if the value is Edit then all become Edit can't able to fix the problem, in this plevel is the arrayList it has either Edit or Views value, help me to find the solution....

<select id="change">
    <% for (Object level : plevel) { 
        if (level.equals("Views")) { %>
            <option><%=level%></option>
            <option>Edit</option> 
        <%} else if (level.equals("Edit")) {%>
        <option><%=level%></option>
        <option>Views</option>
        <% } %>
    </select>
}

This is my output image This is my output image

Selva
  • 546
  • 5
  • 12
  • 34
  • Can you post the output you are getting – Santhosh May 21 '14 at 05:17
  • @sankrish Like above only i am getting – Selva May 21 '14 at 05:26
  • @JonSkeet Can you assist me with this problem, I am voluntarily involving you... – Selva May 21 '14 at 05:29
  • @Selva post the rendered output for `select` html & expected output `select` html. Why are you closing the `select` inside the `for` loop. How muche `select` boxes need to be printed? – vjy May 21 '14 at 05:38
  • @vjy ony two select box at once need to be printed, the values are coming from `action class struts1.3.10` the 1st value has `Views` and 2nd value has`Edit` but displaying both as `Views` then 2nd select should display as `Edit` but not this is my problem – Selva May 21 '14 at 05:43
  • 1
    @Selva if you need two `select` boxes, you should not nest them inside a single for loop, you need to have separate iteration for each `select` box. – vjy May 21 '14 at 05:50

2 Answers2

0

As you have mentioned in the comment , you need 2 select boxes , do something like this , this could print one select box at a time for each object . If you need to have separate ones try as @vjy mentioned

    <% for (Object level : plevel) { 
        if (level.equals("Views")) { %>
            <select id="change1">
            <option><%=level%></option>
            <option>Edit</option> 
            </select>
        <%} %>   
        <% else if (level.equals("Edit")) { %>
             <select id="change2">
            <option><%=level%></option>
            <option>Views</option> 
            </select>
        <%} %>   
   <%}%>

Update:

using jstl you could do it easily

         <c:forEach var="temp" items="${plevel}">
                    <c:if test="${temp eq 'Views'}">
                    <select id="change">
                       <option>${temp}</option>
                    </select>
                    </c:if>
                    <c:if test="${temp eq 'Edit'}">
                    <select id="change2">
                       <option>${temp}</option>
                    </select>
                    </c:if>
         </c:forEach>

Try the above code , wrap the html separately from scriptlets . Also look into How to avoid Java code in JSP files?

Hope this helps !!

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • while copy paste form my code i forgot to paste it here, but i mentioned in my original copy of my code... even though having same problem.... – Selva May 21 '14 at 05:31
  • Have you checked what `plevel` contains . this need to be done in the server side . you should have sent two lists separately – Santhosh May 21 '14 at 05:33
  • yes I had checked already data in server side has no issue while displaying only has the issue – Selva May 21 '14 at 05:35
  • @selva will update my answer using `jstl` . try that – Santhosh May 21 '14 at 05:46
  • no without `tld` I want to get the output this is my task – Selva May 21 '14 at 05:49
0

If you need two select boxes you should not nest them inside a single for loop. Try with this code.

<select id="change1">
        <% for (Object level : plevel) { 
            if (level.equals("Views")) { %>
                <option><%=level%></option>
                <option>Edit</option> 
            <%} %>   
      <%}%>
</select>

    <select id="change2">
        <% for (Object level : plevel) { 
            if (level.equals("Edit")) { %>
                <option><%=level%></option>
                <option>Views</option> 
            <%} %>   
       <%}%>
</select>

It is always better to use jstl instead of writing java code in jsp

vjy
  • 1,184
  • 1
  • 10
  • 24