JSTL Question

M

Mongoose

Hi All,

I have an arraylist of objects. Each object has 3 properties. They
are:

DefectID - Integer
Description - VARCHAR
PriorityID - Integer

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);

%>



<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}">
<tr>

<td><c:eek:ut value="${defect.Description}"/></td>

</tr>
</c:forEach>
</table>
</body>
</html>
 
L

Lew

Mongoose said:
I have an arraylist [sic] of objects.  Each object has 3 properties.  They
are:

DefectID          - Integer
Description      - VARCHAR [sic]
PriorityID         - Integer

I hit my Oracle database and poplulate a list with the aforementioned
objects.  I'm just trying to display the list in my .jsp [sic] page.  Can
someone please tell me what is wrong the code in my .jsp [sic] 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);

 %>

<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}">
                <tr>

                        <td><c:eek:ut value="${defect.Description}"/></td>

                </tr>
        </c:forEach>
</table>
</body>
</html>

You don't tell us what, if any, error message(s) you receive. Is it
something about "defect" not having a "Description" attribute? As far
as I can see, you have given the compiler no clue that the type of the
'List' element is anything other than 'Object'.

By universal Java convention, variables and attributes should be named
with an initial lower-case letter.

JSP trainers and book writers like Marty Hall strongly recommend
against scriptlet in JSPs.

Oracle uses VARCHAR2 as a data type, not VARCHAR. Since VARCHAR is
not a Java type, there is no way that your ArrayList elements have
that as an attribute type.

Any of these things could contribute to whatever problem you might be
having, but since you haven't told us what problem you're having or
even if you actually are having a problem, I cannot be sure.
 
M

Mongoose

Mongoose said:
I have an arraylist [sic] of objects.  Each object has 3 properties.  They
are:
DefectID          - Integer
Description      - VARCHAR [sic]
PriorityID         - Integer
I hit my Oracle database and poplulate a list with the aforementioned
objects.  I'm just trying to display the list in my .jsp [sic] page.  Can
someone please tell me what is wrong the code in my .jsp [sic] page that is
shown below?
---------------------------------------------------------------------------­----------------------------------------------------------------------

<%@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);

<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}">
                <tr>
                        <td><c:eek:ut value="${defect.Description}"/></td>
                </tr>
        </c:forEach>
</table>
</body>
</html>

You don't tell us what, if any, error message(s) you receive.  Is it
something about "defect" not having a "Description" attribute?  As far
as I can see, you have given the compiler no clue that the type of the
'List' element is anything other than 'Object'.

By universal Java convention, variables and attributes should be named
with an initial lower-case letter.

JSP trainers and book writers like Marty Hall strongly recommend
against scriptlet in JSPs.

Oracle uses VARCHAR2 as a data type, not VARCHAR.  Since VARCHAR is
not a Java type, there is no way that your ArrayList elements have
that as an attribute type.

Any of these things could contribute to whatever problem you might be
having, but since you haven't told us what problem you're having or
even if you actually are having a problem, I cannot be sure.

Hi Lew,

I actually don't receive any error messages. The .jsp page simply
displays the "Description" Table Header and nothing else. Also, I
realize that doing this is a poor practice but I was just trying to
quickly display the output (that obviously didn't work out as
planned..LOL). I would NEVER actually do this in any production
application . . . I realize it is poor practice. Description is
VARCHAR2 Oracle Datatype (typo). Since I don't get any error messages
it's hard for me to determine what is going wrong. My guess is that
I'm not using the correct syntax in <c:eek:ut> to refer to the
description property of each object . . . however I'm not
certain . . .
 
L

Lew

Mongoose said:
I actually don't receive any error messages.  The .jsp page simply
displays the "Description" Table Header and nothing else.  Also, I
realize that doing this is a poor practice but I was just trying to
quickly display the output (that obviously didn't work out as
planned..LOL).  I would NEVER actually do this in any production
application . . . I realize it is poor practice.  Description is
VARCHAR2 Oracle Datatype (typo).  Since I don't get any error messages
it's hard for me to determine what is going wrong.  My guess is that
I'm not using the correct syntax in <c:eek:ut> to refer to the
description property of each object . . . however I'm not
certain . . .

It's a mystery, but when I get simply no output from JSPs, I often
find an error message in the error logs for Tomcat.

I suspect that the compiler cannot tell that your element type is not
'Object', and 'Object' does not have a 'Description' [sic] attribute.

Maybe if you make your collection generic it would help. But look
through the error log first.
 
L

Lew

Lew said:
Maybe if you make your collection generic it would help.  But look
through the error log first.

A few minutes with the taglib docs indicates I was wrong.

Try dropping the "PageScope." from '<c:forEach var="defect" items="$
{PageScope.defects}">', thus

<c:forEach var="defect" items="${defects}">

Do clean up the definition of your defects type so that it conforms to
Java naming conventions.

If you still have trouble, an SSCCE would be most useful for anyone
trying to help you.

Really, an SSCCE should be a prerequisite for any request for help.
 
O

Owen Jacobson

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:eek:ut 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:eek:ut 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:eek:ut /> 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
 
A

Arne Vajhøj

Mongoose said:
I have an arraylist of objects. Each object has 3 properties. They
are:

DefectID - Integer
Description - VARCHAR
PriorityID - Integer

Those types are in the database - in Java they are int, String and int.
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?
<%@page import="java.util.ArrayList"%>
<%@page import="EricEnhancementServiceImpl" %>
<%@ page import="java.util.*" %>
<%
List results = new ArrayList();

No need for creating this object as you assign to result
two lines below.
EricEnhancementServiceImpl E = new EricEnhancementServiceImpl();
results = E.getDefects1();

pageContext.setAttribute("defects", results, pageContext.PAGE_SCOPE);

%>



<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}">

<c:forEach var="defect" items="${defects}">

should be OK.
<tr>

<td><c:eek:ut value="${defect.Description}"/></td>

Try:

<td><c:eek:ut value="${defect.description}"/></td>

ot with a recent JSP version:

</tr>
</c:forEach>
</table>
</body>
</html>

Arne
 
A

Arne Vajhøj

Lew said:
Oracle uses VARCHAR2 as a data type, not VARCHAR.

Oracle supports VARCHAR.

It has the same functionality as VARCHAR2.

Oracle recommends using VARCHAR2 in case they decide to
make VARCHAR follow SQL standard semantics.

I would not hold my breath until that happens.

In reality it is more of a coding style convention.

Arne
 
L

Lew

Those types are in the database - in Java they are int, String and int.

Actually, Java's 'Integer' is a closer match to SQL's 'INTEGER' than Java's
'int' is. Java 'int' does not support 'NULL' (Java's 'null').
 
A

Arne Vajhøj

Lew said:
Actually, Java's 'Integer' is a closer match to SQL's 'INTEGER' than
Java's 'int' is. Java 'int' does not support 'NULL' (Java's 'null').

Good point.

Very often Integer is better than int just for this reason.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top