Reputation: 3
So I'm trying to build a table based on the search results from a database access.
I'm searching for courses, so I have a javabean called CourseJBean, which has a number of fields and the appropriate getters and setters (e.g. for the field 'id', getId() and setId(newId) are used). I also have a javabean called searchJBean, which holds an ArrayList of CourseJBeans (the ArrayList is parametrized). This is me dealing with an unknown number of courses being returned from the search. Then I want to fill a table with a course per row, with each cell with one of the course's properties.
I fill the searchJBean as follows:
searchJBean results = new searchJBean();
while (resultSet.next()) {
CourseJBean course = new CourseJBean();
course.setId(resultSet.getString(1));
course.setSection(resultSet.getString(2));
:
:
course.setExam(resultSet.getString(14));
course.setCredits(resultSet.getString(15));
results.addCourse(course);
}
In my JSP, I have a JSTL for-each loop going on
<c:forEach var="course" items="${requestScope.searchResults.courses}">
What I want this to do is to loop through the ArrayList (the field holding it is called 'courses') within the searchJBean (called 'searchResults') and then create a row with each cell holding a different property of the course, e.g.
<tr>
<td> ${course.id} </td>
<td> ${course.section} </td>
:
:
</tr>
Hopefully, this will populate a table with a whole lot of values. The CourseJBean's fields are all of type String, if that matters. I'm using JSP 2.0, JDK 1.6, and Java SE 6 on Netbeans 7.0 with an Apache 7 local server.
However, when I ran it like this, I got an error:
May 5, 2011 6:39:23 PM org.apache.catalina.core.StandardWrapperValveinvoke
SEVERE: Servlet.service() for servlet [cirr_search.do] in context with path [/Registrar] threw exception [An exception occurred processing JSP page /HTML-JSP/searchResults.jsp at line 55
52:
53: <tr>
54: <td>
55: ${course.id}
56: </td>
57: <td>
58: ${course.section}
Stacktrace:] with root cause javax.el.PropertyNotFoundException:
Property 'id' not readable on type java.lang.String
at javax.el.BeanELResolver$BeanProperty.read(BeanELResolver.java:291)
at javax.el.BeanELResolver$BeanProperty.access$000(BeanELResolver.java:239)
at ....
(and so on. If you want the complete log, I can paste it later.)
I think this meant that it's retrieving 'course' as a String, not a CourseJBean, which is why it can't find the property 'id'. I wasn't sure why this was happening, so after a bunch of Googling, I went with adding tags:
In the header of the page (between the head tags):
<jsp:useBean id="searchResults" scope="request" class="searchJBean"/>
At the beginning of the loop, i.e. after the tag:
<jsp:useBean id="course" scope="page" class="CourseJBean"/>
Both CourseJBean.java and searchJBean.java reside in the default package (for now -- I'm learning). I wasn't entirely sure how to use the "class" attribute, so that's what I put. But now when I run it, I get this error:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 14 in the jsp file: /HTML-JSP/searchResults.jsp
searchJBean cannot be resolved to a type
11: <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
12: <script type="text/javascript" src="JavaScript/searchResults.js"></script>
13: <title>cirr_search results</title>
14: <jsp:useBean id="searchResults" scope="request" class="searchJBean"/>
15: </head>
16:
which is repeated three times, and then this:
An error occurred at line: 52 in the jsp file: /HTML-JSP/searchResults.jsp
CourseJBean cannot be resolved to a type
49: <!-- Build the rows. -->
50:
51: <c:forEach var="course" items="${requestScope.searchResults.courses}">
52: <jsp:useBean id="course" type="CourseJBean" scope="page" class="CourseJBean"/>
53: <tr>
54: <td>
55: ${course.id}
which also repeats three times, and is followed by a stack trace that starts with
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:95)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
and continues for another ten or fifteen lines.
So my questions are:
What does this mean?
What is a 'type'?
Do I need to use tags even in JSP 2.0? Shouldn't it be able to find them?
The original error -- why would I get a String?
And most importantly, how do I fix this?
EDIT: The servlet is boring, so here's (part of) the DB access to make the CourseJBean:
List<CourseJBean> results = new ArrayList<CourseJBean>();
while (resultSet.next()) {
CourseJBean course = new CourseJBean();
course.setId(resultSet.getString(1));
course.setSection(resultSet.getString(2));
course.setTitle(resultSet.getString(3));
course.setProf(resultSet.getString(4));
course.setProfID(resultSet.getString(5));
course.setDept(resultSet.getString(6));
course.setDays(resultSet.getString(7));
course.setTime(resultSet.getString(8));
course.setArea(resultSet.getString(9));
course.setDiv(resultSet.getString(10));
course.setRoom(resultSet.getString(11));
course.setReg(resultSet.getString(12));
course.setMax(resultSet.getString(13));
course.setExam(resultSet.getString(14));
course.setCredits(resultSet.getString(15));
results.add(course);
}
return results;
The CourseJBean has the following fields:
private String id;
private String section;
private String title;
private String prof;
private String profID;
private String dept;
private String days;
private String time;
private String area;
private String div;
private String room;
private String reg;
private String max;
private String exam;
private String credits;
They're all strings because it's easier for me (right now) to just throw them into a table that way.
Here's the code to print the table:
<table id="results_table" border="1" cellpadding="5">
<!--Row headings-->
<tr>
<td>ID</td><td>Section</td><td>Title</td><td>Professor (email)</td>
<td>Department</td><td>Days</td><td>Times</td><td>Area/Div</td>
<td>Room</td><td>Reg/Max</td><td>Exam</td><td>Credits</td><td></td>
</tr>
<!-- Build the rows. -->
<c:forEach var="course" items="${requestScope.searchResults}">
<tr>
<td>${course.id}</td>
<td>${course.section}</td>
<td>${course.title}</td>
<td>${course.prof} (${course.profID})</td>
<td>${course.dept}</td>
<td>${course.days}</td>
<td>${course.time}</td>
<td>${course.area}</td>
<td>${course.room}</td>
<td>${course.reg}/${course.max}</td>
<td>${course.exam}</td>
<td>${course.credits}</td>
<td><buttonid="dropButton"type="button"value="${course.id}">Drop</button></td>
</tr>
</c:forEach>
</table>
When I run it like this, I get the same'PropertyNotFoundException':
javax.el.PropertyNotFoundException: Property 'id' not readable on type java.lang.String
I'm not sure where it's getting a string from. If I comment out the forEach loop completely, and just have ${searchResults}, I get this array:
[CourseJBean@554210, CourseJBean@16433e4, CourseJBean@18ada25, CourseJBean@f7bd29, CourseJBean@a3cf3e, CourseJBean@7af3e0, CourseJBean@21151e, CourseJBean@1f194d9, CourseJBean@1635a89, CourseJBean@1ccf0ad]
which if I understand correctly is an array of references to my CourseJBeans. When I ask for ${searchResults[3]}, it prints
CourseJBean@1482aa9
which I believe is a reference. So it has an array, but because it's of references, it's reading them as Strings, not references?
Also, if, in my servlet (using the first code below), I turn each course into an array, loop over it and print out each entry, I get the (second code set below) following:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
for (CourseJBean course : searchResults) {
String[] props = course.toArray();
out.write("<br/>");
for (String prop : props) {
out.write(prop + ",");
}
}
out.close();
produces
EC,EC134,E,4,MW, 1:00pm- 2:15pm,Principles of Macroeconomics,S, ,DIAM 141,21,35,15,Aguilar, laguilar,
EC,EC134,E,4,T, 3:00pm- 3:50pm,Principles of Macroeconomics,S, ,DIAM 141,21,35,15,Aguilar, laguilar,
EC,EC134,A,4,TR, 9:30am-10:45am,Principles of Macroeconomics,S, ,DIAM 141,35,35,15,Long, Jason.Long,
....... etc. (all 10 are here, with the correct data)
So the properties are being correctly put into the CourseJBean, and the array 'searchResults' correctly has the references to all 10 CourseJBeans, but yet it keeps telling me I have a String?
Sorry if that was more noise -- I've learned that it's better to have too much information than too little.
EDIT 2:
So the error I'm getting after cleaning out the stuff and putting things into packages:
javax.el.PropertyNotFoundException: Property 'id' not readable on type java.lang.String
javax.el.BeanELResolver$BeanProperty.read(BeanELResolver.java:291)
javax.el.BeanELResolver$BeanProperty.access$000(BeanELResolver.java:239)
javax.el.BeanELResolver.getValue(BeanELResolver.java:85)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
org.apache.jsp.HTML_002dJSP.searchResults_jsp._jspx_meth_c_005fforEach_005f0(searchResults_jsp.java:211)
org.apache.jsp.HTML_002dJSP.searchResults_jsp._jspService(searchResults_jsp.java:125)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
website.cirr_search.processRequest(cirr_search.java:113)
website.cirr_search.doGet(cirr_search.java:128)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
There are only two lines I recognize, the ones with website.cirr_search (again, small project), and at line 113 in cirr_search is just
search.forward(request, response);
where 'search' is my results page (gotten via RequestDispatcher). Not sure why that would be a problem though.
Upvotes: 0
Views: 3008
Reputation: 1108642
So I'm trying to build a table based on the search results from a database access.
You're using a servlet, right?.
... so I have a javabean called CourseJBean ... I also have a javabean called searchJBean...
Why not just a List<Course>
?
I'm using JSP 2.0, JDK 1.6, and Java SE 6 on Netbeans 7.0 with an Apache 7 local server.
Why not JSP 2.2? Tomcat 7 supports Servlet 3.0 / JSP 2.2.
SEVERE: Servlet.service() for servlet [cirr_search.do]
Are you using Struts?
An exception occurred processing JSP page /HTML-JSP/searchResults.jsp at line 55
55: ${course.id}
root cause javax.el.PropertyNotFoundException: Property 'id' not readable on type java.lang.String
I think this meant that it's retrieving 'course' as a String, not a CourseJBean
That's correct. It can also be that ${requestScope.searchResults.courses}
returned a String
.
I went with adding
<jsp:useBean>
tags:
Oh please no.
Both CourseJBean.java and searchJBean.java reside in the default package (for now -- I'm learning).
Never use the default package when you want to do a bit more than having a class with a main()
method. Classes in the default package are invisble to classes inside a package. Classes inside a package cannot access or import
classes in the default package. JSP/Servlet container itself exist of classes in a package and the generated JSP files are inside a package as well.
An error occurred at line: 14 in the jsp file: /HTML-JSP/searchResults.jsp
searchJBean cannot be resolved to a type14: <jsp:useBean id="searchResults" scope="request" class="searchJBean"/>
What does this mean?
The class is missing in the classpath (or it is simply invisible because it's in the default package).
What is a 'type'?
A class, an interface or an enum. In your case, a class.
Do I need to use
<jsp:useBean>
tags even in JSP 2.0? Shouldn't it be able to find them?
No you don't need them.
The original error -- why would I get a String?
Because you supplied a String.
And most importantly, how do I fix this?
Not sure. There's a lot of noise in the question, the real code of interest is missing. It should however not be that hard. You can find an example of how to create a search form with help of JSP and Servlet in this answer.
Upvotes: 4