aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Creating and populating a List of javaBeans in a JSP w/o using scriptlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Creating and populating a List of javaBeans in a JSP w/o using scriptlets" Watch "Creating and populating a List of javaBeans in a JSP w/o using scriptlets" New topic
Author

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

Tex Martin
Greenhorn

Joined: Sep 09, 2010
Posts: 7
    
    1

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


SCJP, SCWCD
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

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.


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tex Martin
Greenhorn

Joined: Sep 09, 2010
Posts: 7
    
    1

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

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

Joined: Sep 09, 2010
Posts: 7
    
    1

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
Dan Hill
Greenhorn

Joined: Apr 06, 2007
Posts: 4

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

Joined: Sep 09, 2010
Posts: 7
    
    1

Hi Dan
I'm glad you found it useful.
Good luck to you too.
Cheers
Tex
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating and populating a List of javaBeans in a JSP w/o using scriptlets