Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Creating and populating a List of javaBeans in a JSP w/o using scriptlets

 
Greenhorn
Posts: 10
1
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone
I'm studying for the SCWCD using "Head First Servlets and JSP" (2nd edition)
On page 521 I found an example of a simple tag with an attribute, and I have managed to make it work as described.
The JSP tag Invocation does not state how the movieCollection came about, so I made a scriptlet (shame on me!) doing the job:


(I made a Movie javaBean containing a title String and a genre String)

But - what's nagging me is, that since scriptlets are considered "bad" (for all sorts of sensible reasons), I wanted to do what the scriptlet does, but in a scriptless way.

I have tried using <c:set> combined with EL etc. but, until now, to no avail.
I couldn't even manage to create the List with the scriptlet and populate it using <c:set>

I'm sure that some of you consider this a trivial matter, so I'm eager to learn from you.

p.s. I'm aware that I could hit a Servlet and let it create and populate the List and set it as an attribute on the request and then forward the request to the JSP, but I think that's kind of missing the point.

p.p.s. The movie genres are not to be taken seriously :-)

Cheers everyone
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really, really, really don't want to use servlets (which in my opinion is the only right choice, as it allows JSP do the only thing it should do - present data - not manage, create and control), you could hit for an EL function which should cope with that.

EDIT: for what I know, you will not be able to add anything to the list with <c:set> as it only allows you to put something in the java.util.Map or to execute setter method of some property of JavaBean. As List doesn't have any setter (just add(-)) method it is hard to achieve.

You can also use the EL 2.2 (which is bundled in Tomcat 7) which allows you to invoke a method, so ${yourArray.add("movie")} should work.
 
Marshal
Posts: 67414
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tex Martin wrote:p.s. I'm aware that I could hit a Servlet and let it create and populate the List and set it as an attribute on the request and then forward the request to the JSP, but I think that's kind of missing the point.


Missing the point? How so?

JSPs are meant to be consumers, not producers, of data. The JSTL gives a limited amount of flexibility but is not intended to be used in place of servlet controllers.

The proper place to create the data structures that the JSP will consume is in its page controller.
 
Tex Martin
Greenhorn
Posts: 10
1
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pedro, hi Bear
Thank you so much for your quick responds. I really appreciate it.
Please excuse me for my bad choice of words (english is not my native tongue) - Of course you are both right; It would be a far better choice, to construct and populate the mentioned datastructure in a Servlet Controller. The thing is, that you are considering it in a "real-world scenario".

My situation however, is a pedagogical one. It irritates me to have to resort to scriptlets, when working with excamples from the chapters in HFSJ concerning scriptless JSP's. I was expecting to be able to replace the scriplet with some combination of JSTL and EL. My frustration just grew while failing with one attempt after the other.

I shall try Pedro's suggestion (creating an EL function)- I need to train EL functions anyway.

On the other hand, I promise that I will never (or at least only as a last resort) create and/or populate a datastructure in a JSP, in any "real-world scenario".

Once again; thank you for your friendly welcome, I think I'll like it here :-)

Cheers Tex Martin
 
Bear Bibeault
Marshal
Posts: 67414
173
Mac Mac OS X IntelliJ IDE jQuery Java
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem Rancher!

Hey, don't forget to post the results of your struggle. :-)

PS. I'm a real newbie on JavaRanch, but I must say I have never been on a forum like that. It feels like home and it's really one, big, friendly place ;-)

Cheers!
 
Tex Martin
Greenhorn
Posts: 10
1
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pedro, Bear and ranchers in general
The results of my struggle:
The modified JSP using the tag with attribute AND the new EL function:

The TLD, modified with the EL function declaration:

The EL function implementation:

What I obtained from this, is the ability to avoid creating the movielist by means of a Scriptlet. Of course the added value is questionable; it could be argued that it is limited to a pedagogical value only.

I made one interesting observation though: In the TLD, in the function declaration, it is of no consequence whether the returntype in the <function-signature> element is "List", "java.util.List" or even "HorseDo" they all work equally well. However, leaving it out yields a runtime error, when the JSP is compiled: "jsp.error.tld.invalid.signature" (@Jetty-6.1.19)

I guess only the first variant is portable across server implementations :-)

Thanks for the inspiration Pedro and Bear, see you round...
cheers
Tex Martin
 
Greenhorn
Posts: 4
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for creating this thread, Tex! I was going through the exact same motions as you were. Also, thank you for posting your code solutions!

Good luck on the SCWCD!
 
Tex Martin
Greenhorn
Posts: 10
1
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan
I'm glad you found it useful.
Good luck to you too.
Cheers
Tex
 
We don't have time for this. We've gotta save the moon! Or check this out:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic