2

Given the following preview of my JSP file in my project :

<%@ page contentType="text/html; charset=utf-8" language="java"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="beans.UserBean"%>
<jsp:useBean id="userBean" class="beans.UserBean" scope="session" />
<jsp:useBean id="students" type="ArrayList<beans.UserBean>" scope="session" />
<jsp:useBean id="teachers" type="ArrayList<beans.UserBean>" scope="session" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

I get the following error in Eclipse :

Undefined type: ArrayList

What's wrong with it ? even though that I'm importing the ArrayList , Eclipse doesn't recognize it and shows the above message in the following two lines :

<jsp:useBean id="students" type="ArrayList<beans.UserBean>" scope="session" />
<jsp:useBean id="teachers" type="ArrayList<beans.UserBean>" scope="session" />

For clearer picture :

Any idea where did I go wrong ? Thanks

JAN
  • 21,236
  • 66
  • 181
  • 318
  • I'm not sure the `page import` directive is respected in `jsp:useBean`, only in scriptlets. Does the FQ class name work? – millimoose Jun 30 '12 at 23:54

1 Answers1

4

The type attribute should represent the fully qualified name of the class, not the generic declaration or so. Even more, JSP/EL is not aware of generic types in any way.

Use java.util.ArrayList instead:

<jsp:useBean id="students" type="java.util.ArrayList" scope="session" />
<jsp:useBean id="teachers" type="java.util.ArrayList" scope="session" />

All those @page import are unnecessary. They are only used when using scriptlets (those oldschool <% %> things which has been discouraged since JSP 2.0).

By the way, if those arraylists are been prepared and put in the scope by a servlet beforehand and all you need is to just access them in EL, then you do not need those <jsp:useBean> tags at all. Using the type attribute instead of the class attribute hints less or more that you're actually using a servlet. It'll work as good without those <jsp:useBean> tags. See also our servlets wiki page.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555