| Author |
ArrayList/List and EL
|
Tom Hilliard
Ranch Hand
Joined: Feb 03, 2012
Posts: 39
|
|
Hi all,
So I have a method that returns a List to a servlet. The servlet sets the List in the request for use on a JSP.
Now my problem is this, I use EL to print the values of the List using
Now this prints [Accommodation Name, Accommodation Town, Accommodation County, Accommodation Postcode]
Is there a way to access the elements within that row individually(using EL)? So I can print "Accommodation Name(As a href to another page), Accommodation Town, Accommodation County, Accommodation Postcode" without the square brackets
If not, I assume that because its a List I can't access the individual elements of each row? Does this mean I need to use ArrayList instead?
Regards
TH
|
 |
Tom Hilliard
Ranch Hand
Joined: Feb 03, 2012
Posts: 39
|
|
I think I may have figured it out.
I have changed my methods to return ArrayList instead of List.
Then changed my El to
Which results in Accommodation Name being printed by itself :)
TH
|
 |
Bear Bibeault
Author and opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50693
|
|
|
You should not need to return the specific List implementation. In fact, doing so is a rather poor practice. DId that actual end up making a difference?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Tom Hilliard
Ranch Hand
Joined: Feb 03, 2012
Posts: 39
|
|
Bear Bibeault wrote:You should not need to return the specific List implementation. In fact, doing so is a rather poor practice. DId that actual end up making a difference?
It makes no difference whether returning a List or ArrayList, I get the same result for either.
So what your saying is I don't need to return specifically an ArrayList, just a List?
EDIT: I have changed my methods to return a List and I still get exactly the same result, is this better practice? And why is it?
Regards
TH
|
 |
Bear Bibeault
Author and opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50693
|
|
Tom Hilliard wrote:It makes no difference whether returning a List or ArrayList, I get the same result for either.
That's what I thought would happen.
So what your saying is I don't need to return specifically an ArrayList, just a List?
Absolutely. There is no need to reveal the List implementation being used. All that matters is that it is a List.
is this better practice? And why is it?
This is a general Java concept. Revealing the List implementation is a poor practice because it locks the implementation to an ArrayList rather than any List implementation.
As to the original issue, we need more info. What exactly is owneraccomms, and how is it being established as a scoped variable?
(Also, I advise to rethink your naming. "owneraccomms" not so good.)
|
 |
Tom Hilliard
Ranch Hand
Joined: Feb 03, 2012
Posts: 39
|
|
Bear Bibeault wrote:
As to the original issue, we need more info. What exactly is owneraccomms, and how is it being established as a scoped variable?
(Also, I advise to rethink your naming. "owneraccomms" not so good.)
Sorry Bear, I obviously didn't make myself clear in saying I've solved my issue. I thought that a List was like an array (1 column). What I didnt realise is that I could call and element index. So I am now doing this
Which prints this output
Instead of
which I was getting before.
Interms of the Attribute name owneraccomms, what is wrong with that and can you suggest something better if you know it is a List of accommodations an owner owns, each accommodation in the List has a name, city, county and postcode. The List is set in the request using setAttribute()
Regards
TH
|
 |
Bear Bibeault
Author and opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50693
|
|
ownerAccomodations would be better.
Bear's rules for names:
Follow conventions. (In the case of owneraccoms, you failed to use camel case)Words are good; no need to freeze-dry them.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
|
Actually I wouldn't have returned any kind of List for that requirement. You need something with a name, a city, a county, and a postcode? Then create a Java bean class which has those properties, and just use the properties in your EL. Using a List (or an array) is what beginners do before they learn that Java is an object-oriented language.
|
 |
Bear Bibeault
Author and opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50693
|
|
|
Paul is wise. Heed his words.
|
 |
Tom Hilliard
Ranch Hand
Joined: Feb 03, 2012
Posts: 39
|
|
Paul Clapham wrote:Actually I wouldn't have returned any kind of List for that requirement. You need something with a name, a city, a county, and a postcode? Then create a Java bean class which has those properties, and just use the properties in your EL. Using a List (or an array) is what beginners do before they learn that Java is an object-oriented language.
I need a list of those things mentioned, an owner can have may accommodations. Is that possible using beans? I've heard of them but don't know exactly what they do!
TH
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
Then here's a link to the Wikipedia article: JavaBeans. Skip over the section about the JavaBeans API (not relevant to web applications) and go to the section about conventions and the examples following.
The EL is designed to understand JavaBeans which are written using those conventions. So if you had an Accomodation class with a getCounty() method, and you had a scoped variable named "accomodation" which referred to one of those objects, then you could get its county like this:
|
 |
 |
|
|
subject: ArrayList/List and EL
|
|
|