Re: JSTL Question
On 2009-10-09 13:22:45 -0400, Mongoose <verygoofyone@gmail.com> said:
Hi All,
I have an arraylist of objects. Each object has 3 properties. They
are:
DefectID - Integer
Description - VARCHAR
I'm fairly certain your object does not have a VARCHAR property, since
VARCHAR isn't a (standard) Java type. Show the class definition for the
objects in your list.
PriorityID - Integer
Be aware that JSP Expression Language (EL) uses bean accessors, and
isn't required to support properties with initial capital letters. The
EL expression ${foo.bar} is converted into the Java expression
foo.getBar() if there's an accessor, or foo.get('bar') if foo is a Map
implementation, or possibly foo.bar if bar is a visible field.
Your properties, as far as bean property names are concerned, are
likely defectID, description, and priorityID (capitalized thus).
I hit my Oracle database and poplulate a list with the aforementioned
objects. I'm just trying to display the list in my .jsp page. Can
someone please tell me what is wrong the code in my .jsp page that is
shown below?
Thanks
-------------------------------------------------------------------------------------------------------------------------------------------------
<%@page
import="java.util.ArrayList"%>
<%@page import="EricEnhancementServiceImpl" %>
<%@ page import="java.util.*" %>
<%
List results = new ArrayList();
EricEnhancementServiceImpl E = new EricEnhancementServiceImpl();
results = E.getDefects1();
pageContext.setAttribute("defects", results, pageContext.PAGE_SCOPE);
%>
Do not do this. Scriptlets in a JSP are bad form for a bunch fo
relatively good reasons: tool support sucks, it's harder to
automatically test, and it introduces the temptation to put *more* code
in the page in the future. If you're not using an MVC framework of some
kind, you can do a couple of things here:
1. What I'd do as a first approximation: Write a servlet that loads
E.getDefects1() into a request-scope attribute and then forwards to the
JSP, separating the controller logic (which is mostly "load some data"
here, but may not be in the future) into a servlet and leaving only
view logic (below) in the JSP. The servlet's doGet method would look
roughly like this untested snippet:
private final EricEnhancementService enhancementService
= new EricEnhancementServiceImpl();
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
List<?> results = enhancementService.getDefects1();
request.setAttribute("defects", results);
request.getRequestDispatcher("/WEB-INF/views/defects.jsp")
.forward(request, response);
}
(I've also corrected some stylistic complaints; feel free to un-correct
them, but take a minute to think about why I changed what I changed
first. And if you can't figure it out, ask.)
2. Move the above snippet into a custom JSP tag. You can write your own
tags that declare page-scoped variables: have a look at the Orion
taglib tutorial (it mostly uses standard EE APIs, so it can be adapted
to other web containers):
http://www.orionserver.com/docs/tutorials/taglibs/1.html
<html>
<head>
<title>display</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
</head>
<body>
<table border="1" width="80%">
<tr><th>Description</th></tr>
<c:forEach var="defect" items="${PageScope.defects}">
There is no object named 'PageScope' in any of the scopes in your page,
so the EL expression ${PageScope.defects} evaluates to null, so forEach
loops over nothing. If you want to refer to the implicit page scope
object directly, its name is 'pageScope'. However, as Lew mentioned,
you don't need to refer to it at all here: you can simply write
${defects}. The JSP compiler will try the page and request scopes (in
that order) automatically.
You should have a quick read through the EL primer:
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html
Pay particular attention to the list of implicit objects, and note that
EL (like the rest of Java) is case-sensitive.
<tr>
<td><c:out value="${defect.Description}"/></td>
As mentioned above, this is probably ${defect.description}.
If you don't know for sure that the description attribute is free of
HTML markup characters, you should pass escapeXml="true" to c:out as
well. If you're absolutely sure there are no markup characters in the
expression, and if your web.xml uses spec version 2.4 or higher, then
you can write ${defect.descrption} without using the <c:out /> tag at
all - the JSP engine in JSP 2 (Servlet 2.4) supports EL without any
special support.
</tr>
</c:forEach>
</table>
</body>
</html>
-o