This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes Displaying alternative text when a tag's body is empty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying alternative text when a tag Watch "Displaying alternative text when a tag New topic
Author

Displaying alternative text when a tag's body is empty

Michael Arnett
Ranch Hand

Joined: Mar 22, 2001
Posts: 65
Hi all,
I have a custom tag that does a sql query and displays the results. That works fine. However, I woud like to have an alternate message displayed if there is no results. I think this is easy to handle, but I want to use the standard JSP custom tag method, not some crazy scriptlet or some other exotic method. What is the standardized way of producing alternate text when a tag does not return anything.
Thanks in advance,
-MLA


Sun Certified Programmer for the Java 2 Platform 1.4
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
How about tossing a counter variable, initialized to 0 beforehand, in your while(ResultSet.next()) loop so it will increment each time the while loop is executed.
This way, once you have exited the loop, you can test that variable to see if it has changed. If so, then you continue because results were returned from the DB. If the variable is still 0, then you exit out and display your suggested text because there were no results which matched your SQL statement.
If that is not the option you were looking for, you could always run two queries. The first being to select the count of the records which would be returned, the next to run the query again returning the actual rows retrieved instead of their count.
Sam Dalton
Author
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
Does the tag have a body already? IF not then try this:
Once you have done your query, check that the rs has data in it, if not then EVAL_BODY_INCLUDE, if it does, display your results and SKIP_BODY at the end.
Cheers
Sam


<a href="http://www.samjdalton.com" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of [http://www.amazon.com/exec/obidos/tg/detail/-/1590592255/qid=1068633302//ref=sr_8_xs_ap_i0_xgl14/104-4904002-9274339?v=glance&s=books&n=507846]Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/1861007701/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/186100561X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
 
 
subject: Displaying alternative text when a tag's body is empty