Solved

jstl instead of scriplets

Posted on 2006-06-22
9
654 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:jaggernat
  • 2
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Accepted Solution

by:
evnafets earned 168 total points
ID: 16965411
<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
 
LVL 27

Assisted Solution

by:rrz
rrz earned 166 total points
ID: 16965431
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
 
LVL 4

Expert Comment

by:evnafets
ID: 16965818
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:rrz
ID: 16965868
Ok, then split.
Actually I assumed that the context's web.xml is for Servlet 2.4.  
0
 
LVL 2

Assisted Solution

by:Mr_Lenehan
Mr_Lenehan earned 166 total points
ID: 16969658
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
 
LVL 2

Expert Comment

by:Mr_Lenehan
ID: 16969710
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
 
LVL 10

Author Comment

by:jaggernat
ID: 16970956
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now