jstl instead of scriplets

hi experts

I have a <select > tag in my jsp i am populating the values in the dropdown from the action class. But i have used java code in my jsp to do that. Is there any way i can avoid java code and use jstl instead for the below code.

<%
List commandList = (List) request.getAttribute("commandInformation");
if( commandList != null )
{
  %>
        <td width="32%">
          <select name="commandID" class="formstyle">
                <option value="all" selected>All</option>
                <%
                for(int i=0; i<commandList .size(); i++)
                {
                  CommandVO ccInfo = (CommandVO) commandList .get(i);

                  %>

                  <option value="<%=ccInfo.getCommandCenterID()%>" ><%= ccInfo.getCommandCenterName() %></option>
                  <%

                }
                %>
          </select>&nbsp;&nbsp;&nbsp;
        </td>

<%
  }
%>

any help greatly apprecaited
thanks
J
LVL 10
jaggernatAsked:
Who is Participating?
 
evnafetsConnect With a Mentor Commented:
<td width="32%">
  <select name="commandId" class="formstyle">
  <c:forEach var="command" items="${requestScope.commandInformation}">
    <option value="<c:out value="${command.commandCenterId}"/>"><c:out value="${command.commandCenterName}"/></option>
  </c:forEach>
  </select>
</td>



0
 
rrzConnect With a Mentor Commented:
Here is my try. Which is very close to evnafets' solution. Give most of points to evnafets.
<c:if test="${not empty commandInformation}">
        <td width="32%">
          <select name="commandID" class="formstyle">
                <option value="all" selected>All</option>
            <c:forEach items="${commandInformation}" var="current">
            <option value="${current.commandCenterID}" >${current.commandCenterName}</option>
            </c:forEach>
          </select>&nbsp;&nbsp;&nbsp;
        </td>
</c:if>
</td>
0
 
evnafetsCommented:
rrz's is actually closer to the original - I missed a few things out. (the "all" option, &nbsp; etc etc)
The other difference is the use of <c:out> statements vs ${expr}.
I actually started with just ${expr} but seeing as the OP didn't specify the environment, thought I should give the generic solution. (and didn't want to bother with the explanation of JSP2.0 containers and EL...)
I know it doesn't look as nice, using the <c:out> tag, but I think its a little more portable, and a little more robust, in that it will escape any html special characters that show up in the data.

But rrz's contribution is truer to the original ;-)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
rrzCommented:
Ok, then split.
Actually I assumed that the context's web.xml is for Servlet 2.4.  
0
 
Mr_LenehanConnect With a Mentor Commented:
Something you may want to explore that I use all the time are JSP tags. Using a tag library will let you run a class and throw values from the class to the page.

Example of a JSP page using a tag:
<%@ taglib uri="WEB-INF/List.tld" prefix="List" %>
<table>
<List:Files someVar="a_var_your_class_wants">
   <tr><td>Filename<%=someVariableFromYourClass%></td><td><%=someVariableFromYourClass%></td></tr>
</List:Files>
</table>

In this example everything between the List:Files tag will get looped, creating a nice two columned table as it goes with no set num of rows.

If you have Web Dev's like we do then it allows them to do pretty things with the pages without needing to worry too much about messing with JSTL's code.  It is a little trickier to implement than just jstl, but if you come from a pure java as oppose to web dev background it makes things much easier for you (or at least I find).

This is a little used feature of JSP that is very useful.
Sun Official Docs: http://java.sun.com/products/jsp/tutorial/TagLibrariesTOC.html
Un-official and easier to follow: http://www.onjava.com/pub/a/onjava/2000/12/15/jsp_custom_tags.html

As your question was about jstl usage you should split the points between other comments and not this one. Just something for you to think about.
0
 
Mr_LenehanCommented:
Using a tag your code could be as simple as:

<%@ taglib uri="WEB-INF/Options.tld" prefix="Opt" %>
<select name="commandID" class="formstyle">
                <option value="all" selected>All</option>
                <List:GiveOptions use="<%=request.getAttribute("commandInformation")%>">
                <option value="<%=something%>"><%=somthingelse%></option>
                </List:GiveOptions>
</select>

Your class could then take care of casting to list and doing useful stuff, thus hiding evil looking java code from web devs.
0
 
jaggernatAuthor Commented:
well, yea thats not bad either. But i would still go for JSTL since its so logical. Meaning they have a tag <c:if>for "if" statement , <c:forEach> for "for" statement and so on which makes understanding of tags so easy.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.