File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes alternate to jsp:declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "alternate to jsp:declaration" Watch "alternate to jsp:declaration" New topic
Author

alternate to jsp:declaration

Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
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

Joined: Jan 10, 2002
Posts: 60060
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
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

Joined: Sep 13, 2005
Posts: 238
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

Joined: Jan 10, 2002
Posts: 60060
    
  65

<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

Joined: Sep 13, 2005
Posts: 238
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

Joined: Jan 10, 2002
Posts: 60060
    
  65

Why are you using <logic:iterate> vs <c:forEach>? The latter will count the iterations for you.
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
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

Joined: Jan 10, 2002
Posts: 60060
    
  65

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: alternate to jsp:declaration
 
Similar Threads
Strange question in mockup exam
jsp document
JSP query! Can anyone give solution for this
Declaring function doesn't work
Converting JSP to XML