In my JSP I get a warning for thi开发者_如何学JAVAs cast
<%
Collection<Server> svr = (Collection<Server>)request.getAttribute("serverCollection");
%>
instanceOf
doesn't seem to work here
<%
@SupressWarnings("unchecked")
Collection<Server> svr = (Collection<Server>)request.getAttribute("serverCollection");
%>
BTW, using scriplets is not very good thing, read this thread about avoiding scriplets.
You can't "satisfy" that warning. It is an unchecked cast, and you can't really do anything about it since the method returns an Object
.
If you're absolutely positive the attribute will always contain a Collection<Server>
you can add a @SuppressWarnings("unchecked")
annotation.
You could use JSTL instead of scriptlets. It would look like:
<c:set var="svr" value="${requestScope['serverCollection']}"/>
You cannot avoid this warning (except by suppressing it). The problem is that Java at runtime is only able to check that the object is of type Collection
at runtime when casting. It cannot check that it is of type Collection<Server>
. That is what the error means.
精彩评论