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("comm andInforma tion");
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.getComman dCenterID( )%>" ><%= ccInfo.getCommandCenterNam e() %></option>
<%
}
%>
</select>   ;
</td>
<%
}
%>
any help greatly apprecaited
thanks
J
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("comm
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.getComman
<%
}
%>
</select>  
</td>
<%
}
%>
any help greatly apprecaited
thanks
J
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ok, then split.
Actually I assumed that the context's web.xml is for Servlet 2.4.
Actually I assumed that the context's web.xml is for Servlet 2.4.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.getAttribu te("comman dInformati on")%>">
<option value="<%=something%>"><%= somthingel se%></opti on>
</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.
<%@ taglib uri="WEB-INF/Options.tld" prefix="Opt" %>
<select name="commandID" class="formstyle">
<option value="all" selected>All</option>
<List:GiveOptions use="<%=request.getAttribu
<option value="<%=something%>"><%=
</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.
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.
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 ;-)