问题
How do I use jsp:useBean with collections like Lists and Maps correctly?
I can get Lists by using
type="List<MyObject>"
but I cannot get the List to instantiate if nothing is passed.
class="ArrayList<MyObject>"
And the above attribute gives an error:
The value for the useBean class attribute ArrayList
<MyObject> is invalid.
And both the attributes fail for a HashMap.
Currently, I'm reading the objects using getAttribute() but it'll be cleaner to use the tags and set them up.
回答1:
Have you tried just
class="java.util.ArrayList"
Remember, the actual type name doesn't include the generic type, that's syntactic sugar used within the Java language itself, and <jsp:useBean> isn't part of the Java language.
来源:https://stackoverflow.com/questions/3263642/jps-usebean-with-hashmap