GeeCON Prague 2014*
The moose likes JSP and the fly likes Another EL/Map iteration question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Another EL/Map iteration question" Watch "Another EL/Map iteration question" New topic
Author

Another EL/Map iteration question

Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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

Joined: Jan 20, 2008
Posts: 137
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jan 20, 2008
Posts: 137
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jan 10, 2002
Posts: 61315
    
  66

I actually prefer using wrapper types as they are set to null if (non-required) attributes are omitted.
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

Have you tried getList() vs isList()? Just a stab...
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
Yes, I also tried making the Boolean property isListCode() but the result is the same. Thinking...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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

Joined: Jan 20, 2008
Posts: 137
Perhaps having the isList() method return boolean rather than Boolean would fix the problem?


Survey says: Yes! Thank you.
 
GeeCON Prague 2014
 
subject: Another EL/Map iteration question