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?
1 Answers
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.
-
hey thanks for reply.. in my bean class i got arraylist and return it.after that i did the things you said this is my jsp if i run this org.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.4 confused.. – LaknathR Jul 15 '10 at 08:58
-
Post the root causes. They tell something about the root causes of the problem. – BalusC Jul 15 '10 at 11:21
-
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) – LaknathR Jul 15 '10 at 11:43
-
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) – LaknathR Jul 15 '10 at 11:44
-
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) – LaknathR Jul 15 '10 at 11:45
-
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 – LaknathR Jul 15 '10 at 11:49
-
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 Jul 15 '10 at 11:53
-
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 ... – LaknathR Jul 15 '10 at 11:59
-
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... – LaknathR Jul 15 '10 at 12:25
-
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.. – LaknathR Jul 16 '10 at 09:09