Link to home
Start Free TrialLog in
Avatar of jaggernat
jaggernat

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of evnafets
evnafets

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of evnafets
evnafets

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 ;-)
Ok, then split.
Actually I assumed that the context's web.xml is for Servlet 2.4.  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of jaggernat

ASKER

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.