tags:

views:

74

answers:

1

I have a arraylist with some data. I need to bind that data to a dropdown list in JSP and get the selected index. Can any one help me for this?

A: 

You can use JSTL c:forEach to iterate over a list (if not done yet, just drop jstl-1.2.jar in /WEB-INF/lib to install it).

<select name="item">
    <c:forEach items="${list}" var="item">
        <option value="${item.value}">${item.label}</option>
    </c:forEach>
</select>

This assumes that you've a List<Item> where Item look like this:

public class Item {
    private String value;
    private String label;
    // Add/generate c'tors, getters and setters.
}

In the server side you can obtain the selected item as follows:

String selectedItem = request.getParameter("item");

You can alternatively also use a Map<String, String> instead of a List<Item> where Item is actually a key-value pair. You can then iterate over it the following way:

<select name="item">
    <c:forEach items="${map}" var="entry">
        <option value="${entry.key}">${entry.value}</option>
    </c:forEach>
</select>

Related answers:


Update: As per the comments, you should never copy server-specific libraries like Tomcat/lib/*.* into the webapp's /WEB-INF/lib or anywhere else in (default) runtime classpath (e.g. JRE/lib/ext). This would only lead to collisions in the classpath which leads to this kind of errors and it will make your webapp unrunnable and unportable. You should keep the server-specific libraries at their default location. Cleanup the /WEB-INF/lib from any server-specific libraries.

You probably copied server-specific libraries there because you wasn't able to compile your servlets. Copying the libraries in /WEB-INF/lib is the wrong solution. You should basically just specify those libraries in the compiletime classpath. It's unclear which IDE you're using, but if it's Eclipse, this can be done easily: first add the server in Servers view, then associate your dynamic webapp project with the added server. On a brand new web project you can choose the server during project creation wizard. On existing web projects, you can modify it in Targeted Runtimes section in project's properties. This way Eclipse will automatically add the server-specific libraries to the project's buildpath.

BalusC
hey thanks for reply.. in my bean class i got arraylist and return it.after that i did the things you saidthis is my jsp<select name="item"> <c:forEach items="${item.list}" var="name"> <option value="<c:out value="${name}" />"> </c:forEach></select>if i run thisorg.apache.jasper.JasperException: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext; org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)and some root cause exceptios..i added some additional jars in to web-inf/lib and version of xml is 2.4confused..
Post the root causes. They tell something about the root causes of the problem.
BalusC
this is one ...root cause javax.servlet.ServletException: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext; org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:774) org.apache.jsp.Onto_0020web_jsp._jspService(Onto_0020web_jsp.java:104) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
java.lang.NoSuchMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext; javax.servlet.jsp.jstl.core.LoopTagSupport.unExposeVariables(LoopTagSupport.java:587) javax.servlet.jsp.jstl.core.LoopTagSupport.doFinally(LoopTagSupport.java:323) org.apache.jsp.Onto_0020web_jsp._jspx_meth_c_005fforEach_005f0(Onto_0020web_jsp.java:145) org.apache.jsp.Onto_0020web_jsp._jspService(Onto_0020web_jsp.java:89) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98) javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
this is a exception...exception org.apache.jasper.JasperException: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext; org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:321) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:257) javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
i added jsp-api and servlet-api and el-api by reading some forums and also check the version of the server in my xml file(2.4)... also give the same error that i mentined above..thanks
Get rid of all server specific libraries in webapp's `/WEB-INF/lib`!! It would only conflict with the ones of the server where you're running the webapp on. Leave the server specific libraries there in the server implementation and don't ever touch/copy/move them.
BalusC
thanks for ur quick reply...still confusing...i added that files in to web-inf/lib folder...just copy and paste....which lib should i remove from the lib folder..thanks ...
i removed the all the libs from web-inf folder.. but still getting the same error.. i put those lib in to web-inf folder because that error occurs when i compile firstly.. i thought the error occurs with my jstl lib...how can i fix...
error triggered with my jstl tags.. my servlet version is 2.4 . then i added jstl and standard.jar in to web-inf folder.. then there is no error.. and i changed my taglib as, <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> no errors..thanks for your involvement..