This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Output JCoTable rows using JSTL c:forEach  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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.
 
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. forEach is part of the JSTL, not the EL.
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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

 
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!