开发者

Tomcat 6.0 does not allow generic ArrayList to be used for useBean

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.开发者_Go百科util.ArrayList is invalid.

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜