Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bean and for each

 
syd kahn
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This noob needs a little direction. I was a ASP and ASP.NET developer until the company told me - This new project is going to be done in JSP's - go figure... Ahem - back to the topic

I built a bean that goes out and connects to the database gets a result set. Then uses a string builder to construct all the output code and dump it back as a string. Great, all this works...

The problem is that "I" can change the formatting of the output, but the Web Page designers can't.

What I am looking for is a way to use the bean to do what beans do best (get the data), and html to do what it does best - format the output.

I want to put a for each (or while next) or whatever in the actual JSP page, and using ${} inside plain old HTML - this gives "them" the option of tweaking the page without bothering me.

I have googled my fingers numb trying to find anything would show me how to implement this.

I have many options - expose the recordset - build a collection of hastables built from individual rows of data. lots of options flit about - but nothing seems elegant enough to actually spend the time to build something.

I guess the actual quest is how would someone inmplmemtn an iterator in a bean to parse out rows of data.... links to examples would be stellar...

do i sound confused - well good - i am....
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type of on-page formatting are you looking for?

The JSTL fmt tags already make it easy to format numerical and date data.

For things likes SSNs and phone numbers (coincedentally the subject of another active discussion), or other formats not provided by existing tags, you can write your own tags (which is much easier in the JSP 2.0 environment than JSP 1.x).

If you are sending tabular data to the page, I generally just send an Object[][] array and use nested forEach tags with formatting/custom tags to present the data in the desired format.
[ August 23, 2005: Message edited by: Bear Bibeault ]
 
syd kahn
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say object[][] and nested for loops - i hate to ask - but is there a piece of sample code out there that you could suggest me looking at - i have been doing java for 2 weeks now, and my skills are noticably lacking.

The Object [][] i suppose is a doubly dimentioned (sorry for the asp nomenclature) array and i would do a for i, for j and get object[i][j]... i'll take a stab at doing that... Next i'll try doind an ArrayList of ArrayLists... so i don't have to do object[100][6] and hope that there is never more than 100...

Thanks for the suggestion...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say that dataset is a scoped variable that denotes an Object[][] array (yes, that's a doubly-dimensioned array of Object instances). To build a simple table:



Once you've got that down, you can start embellishing it.

I usually have the above defined within custom tag whose elements specify formatting information for the cells, and applies the formatting to the cell data.
 
Stefan Evans
Bartender
Posts: 1778
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My preferred approach is to iterate through the result set in the java bean, and dump the data into a value object (another bean)

For instance if you are trying to display a product list, I would have a bean class "Product", with attributes name, price, description, imageLink...

Running the query, I would construct objects of type product from the ResultSet, and then send a list of "Product" objects to the page via a request attribute.

It then makes the jsp a bit more logical to read:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, that is the approach I would also use for non-tabular data.
 
syd kahn
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok - i formally give up... i am well and truly stuck.


is what i want without the sql tag: the best i could come up with is:



which is not very pretty and violates my premis of keeping code out of the page....
i want to use something like:
vector and hastable is used since it is threadsafe and this is jsp.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic