Solved

jstl instead of scriplets

Posted on 2006-06-22
9
694 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A solution for Fortify Path Manipulation.
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.

623 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