Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another EL/Map iteration question

 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Map<String, List<Integer>> that's a property of a bean class. I want to pass this map as a param from one jsp to another:



and turn it into a table where the contents of the Integer array is column 1 and the String key is column 2. And here's where I haven't been able to figure out the syntax:



That gets me a [ServletException in:/<<pathtothis>>.jsp] Unable to find a value for "value" in object of class "java.lang.String" using operator "."'.

Which suggests a problem on line 4, but I've no idea what to do about it. What is the EL syntax for iterating through the keys and values in a Map passed in as a param?
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on the fact that your keys are of type String and the error message is complaining that you are trying to get the "value" attribute of a String, my guess is that the outer c:for-Each element is returning the keys of the Map one at a time.

If I'm right, then the EL expression for the key (String) would be

and the EL expression for the value (List<Integer>) would be
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using a param, you are causing the collection to be flattened into a string.

Rather than an include, use a tag file where you can pass objects as explicit arguments to the tag.
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clearly I haven't used jsp:include enough, so now I get to learn something new as a side-effect of posting the wrong answer.

However learning new things is a Good Thing™, so the ten copies of duplicate code which I was about to clean up using jsp:include are now going to get cleaned up using a custom tag instead.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tag files are great "replacement" for the JSP 1.x include action. Rather than using params, or implicitly using scoped variables that tightly bind the parent and child files, tag files are easy to write (no Java) and explicitly define parameters. This allows for much cleaner and clearer code, and loose binding of the components.
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have an object of type com.foo.SomeBean, and I make my tag file's attribute as follows:

<%@ attribute name="someBean" required="true" type="com.foo.SomeBean" %>

can I then use the various JSTL/EL constructs to work with that object inside the tag file, iterating through whatever Lists or other stuff it may contain? I'm having a hard time understanding what is and is not allowed in tag files.
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, indeed you can. I just tried it myself earlier this week and it works fine. Once you declare the type of the attribute, as you did there. If you don't declare the type then it doesn't work quite as fine.
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can i declare a tag file attribute of a primitive type, like long or float? (I don't see how, as they aren't in a package.) Or should I just go back to my classes and make those properties Wrapper types? Thank you.
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know, but I would start by just declaring the tag parameter types as the wrapper types (e.g. java.lang.Long) and see if auto-boxing takes place.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually prefer using wrapper types as they are set to null if (non-required) attributes are omitted.
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, wrappers it is then. But I keep running into this odd problem: My class PA contains a List getDataRows() of PADataRow beans having these properties:



In other words, given a PADataRow I should be able to check the isList() property and then the memberCodes collection. In my tag file:


But I get the familiar Unable to find a value for "list" in object of class "foo.bar.PADataRow" using operator "."' . Is it trying to coerce the datarow variable into String?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried getList() vs isList()? Just a stab...
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I also tried making the Boolean property isListCode() but the result is the same. Thinking...
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps having the isList() method return boolean rather than Boolean would fix the problem?

(I looked in the EL spec but I couldn't find the part which explained how the "." operator works.)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Perhaps having the isList() method return boolean rather than Boolean would fix the problem?

Good insight. I think that the "is" protocol only works with boolean, but if that was the only problem, I'd have expected changing is to get would have resolved it.
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps having the isList() method return boolean rather than Boolean would fix the problem?


Survey says: Yes! Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic