• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Output JCoTable rows using JSTL c:forEach

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created a java webapp that reads a custom table (consisting of 13 fields per record) from a SAP backend into a JCoTable in the webapp and this all works fine.

My requirement is to output all the records on the JCoTable onto a JSP using EL. How do I reference each individual field for output purposes? i.e.



As far as I know I can't use the JCoTable directly to forward to jsp and have to map it to something else but I'm open to correction on that.

I have tried using an ArrayList<String>, TreeSet and HashMap to output the table contents on the JSP, using a servlet to forward on the request attribute to the JSP, but I cannot directly access each field, I can only output the contents as one long string.

Do I need to create an object of my table fieldnames (ArrayList<customtable>) and proceed that way? Or what?

I can however access each field using a POJO with so do I need to create an EL function for each table field and use them on my jsp to do the same?

Any and all help greatly appreciated.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Declan Barrett wrote:As far as I know I can't use the JCoTable directly to forward to jsp and have to map it to something else but I'm open to correction on that.


I assume that is because JCoTable is similar to a JDBC result set in that it iterates internally over rows? In that case, yes, you need to iterate over it and capture the results in a list.

Do I need to create an object of my table fieldnames (ArrayList<customtable>) and proceed that way?



I would approach it this way. Create a bean class that captures each column of interest as a property, and send a list of them to the JSP for iterating over with forEach.

<c:forEach items="${claimstable}" var="clms">


Please use more readable names than clms.  If each row represents a claim, then claim is a good name to use.

     ${clms.field1}


Yes, that is the correct way to reference a property of a bean. There is no need for EL functions.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. forEach is part of the JSTL, not the EL.
 
Declan Barrett
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, So now have made some progress (thanks Bear) but I am only outputting the contents of the last record on the JCoTable.

My code is:


The output of which is


Any pointers as to where I'm going wrong? Is it where I'm getting each string?

Thanks.

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 6 creates a single instance of the bean which the body of the loop continually updates. So you never end up with more than one bean. You should be creating a new bean for each row.
 
Declan Barrett
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
D'oh !!!

Thanks Bear, much obliged as always - I couldn't see the wood for the trees, I'll give it a go tomorrow when I'm back at the coalface.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Declan Barrett
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both of the following code snippets work for my purposes but which is the more technically correct re OOP principles, best use of resources etc?



OR

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first fragment you create an array and put data into its elements. Does your code go on to use that array for anything? If not, then don't create it. (I guess you'd put that under "best use of resources".)
 
Declan Barrett
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic