3

In a jsp file I have this declaration:

<jsp:useBean scope="request" id="products" class="java.util.ArrayList<sgt.supermarket.entity.Product>"/>

This declaration works fine with GlassFish 2.1, however, when I switch to Tomcat 6.0, exceptions is thrown:

The value for the useBean class attribute java.util.ArrayList is invalid.

Is there any library missed for Tomcat that makes it behave different from Glass Fish 2.1?

David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
Mr Cold
  • 123
  • 2
  • 9

2 Answers2

5

EL isn't aware of parameterized types, so there is no need to do so. In JSP/EL there are absolutely no compile-time checks on that. EL is more based on reflection. I am however a bit surprised that it "works" in Glassfish as it isn't specified in JSP/EL specifciation.

Apart from it all, the jsp:useBean is fairly superfluous in a decent MVC application wherein a Servlet is been used to control and preprocess the requests.

List<Product> procucts = productDAO.list();
request.setAttribute("products", products);
request.getRequestDispatcher("products.jsp").forward(request, response);

The products is now just accessible in EL by ${products}. No need for a jsp:useBean.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Oh, look like I was using old stuff. I tried your suggestion and my code looks much cleaner now. Thanks. – Mr Cold Dec 16 '09 at 02:25
  • However, with that code, it's look like to me that errors will be impossible to be detected at compiling time (e.g. When you change the bean property name). Also, refactoring would never work.These was something that I was very concerned when I work with ASP.NET. – Mr Cold Dec 16 '09 at 03:00
  • How often would you change names? Refactoring is more a tool-issue than a language-issue. – BalusC Dec 16 '09 at 11:53
  • @BalusC Right, but the type generally isn't known at compile time, which doesn't leave the tools with much advice. With Intellij, the only way I can get proper refactoring is if I use jsp:useBean along with EL expressions. – Jeremy Dec 27 '13 at 17:32
  • @Jeremy: as said, it's more a tool issue. I don't have problems with Eclipse + JBoss Tools. – BalusC Dec 27 '13 at 18:08
  • @BalusC Are you able to control+click on any arbitrary EL expression that doesn't have any type hints a la jsp:useBean and have it jump directly to the definition? If the language itself doesn't allow for static analysis then it's more than a tooling issue. – Jeremy Dec 27 '13 at 18:14
0

If you are dealing with legacy code and simply want to get rid of all those raw-type JSP warnings then you may want to replace your line with this one:

<% request.setAttribute("products", 
       new java.util.ArrayList<sgt.supermarket.entity.Product>()); %>

But BalusC is still right: When you use the classic MVC pattern then it is better to create this product list in the controller and not in the view. And if you are using a view-first pattern then I suggest writing your own JSP Tag which creates and fills this product list in clean Java Code.

kayahr
  • 20,913
  • 29
  • 99
  • 147