• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

alternate to jsp:declaration

 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any alternate to

<jsp:declaration>
int count = 0 ;
</jsp:declaration>
...my html code
<jsp:scriplet>
count ++ ;
</jsp:scriptlet>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keeping track of a incrementing count? Easy.

Do you have a copy of the JSP and JSTL specs handy? If not, you should.

Read the chapter on the EL in the JSP spec, and the section on <c:set> in the JSTL spec.
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read jsp spec chapter on el and also the c:set part. I added following line but its not working

<c:set var="count" value="${0}"/>

<table....
<td align... class='<%= count % 2 == 0 ? "...":"..." %>' />
...more <td's>
/table>

it says count cannot be resolved.
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read jsp spec chapter on el and also the c:set part. I added following line but its not working

<c:set var="count" value="${0}"/>

<logic:iterate ....>

<td align... class='<%= count % 2 == 0 ? "...":"..." %>' />
...more <td's>
</logic:iterate>

<jsp:scriptlet>
count ++ ;
</jsp:scriptlet>
it says count cannot be resolved. I am using this jsp:scriplet because still I dont know how to increment the value. I think I can do ${count ++} instead but still it says count cannot be resolved.
Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<td align... class='<%= count % 2 == 0 ? "...":"..." %>' />


The JSTL and EL work with scoped variables. Scriplets with scriplet variables.

Scoped variables that you create with <c:set> are not accessible (at least not that way) to scriplets.

<jsp:scriptlet>


Are you actually using the JSP XML document notation for your JSPs?
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Now I have replaced it as follows and its fine

class='${count % 2==0 ?"td_cellshading01":"td_cellshading01even"}'

but how do i increase this count.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using <logic:iterate> vs <c:forEach>? The latter will count the iterations for you.
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well logic:iterate is counting iterations for me. so that is fine. but i change the value of class attribute (css style) for every other row. that is why i m using class='count % 2 == 0 ? "even row style class" : "odd row style class"'

second why i m doing it this way is because i m just mantaining this code. It is working fine. Why i m doing this is because i think it would be better and this way i could learn EL and also jstl to certain extent because normally i m used to of using struts tags.

Thanks
Imad
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a worthy goal, but be aware that mixing scriptlets. JSTL, Struts tags and EL all in the same page can be rather messy as they don't interoperate all that well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic