• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing bean variable dynamically

 
ad da
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have a bean with variable names like
title_1
title_2

i m using JSTL to display
${promotions.doc[0].title_1}
${promotions.do[0].title_2}

anyway for me to loop through and do some think like
<c:forEach ...>
${promotions.doc[0].title_{i}}
</c:forEach>

I know about accessing vabriable dynamically as described in http://www.sitepoint.com/forums/java-jsp-35/processing-dynamic-variable-jstl-497960.html but that does not work for variables in bean

Any help?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The EL accesses properties, not variables. Define property getters for the values.

For a collection, you should be returning a collection. It's kludgey to dynamical add _1, _2 and the like (but it can be done).

Remember that you should be making things easy for the JSP, not doing weird things in the JSP to accommodate a bean that's poorly defined. Fix the bean rather than doing gymnastics in the JSP.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!