aspose file tools*
The moose likes JSP and the fly likes JSTL for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL for loop" Watch "JSTL for loop" New topic
Author

JSTL for loop

san krish
Ranch Hand

Joined: Aug 11, 2010
Posts: 41
I want to use a JSTL c:foreach loop.<c:foreach>
The loop should start with the current year and end with current year-100(2011 to 1911).
I used scriptlets for begin and end tag
//
SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy");
int year = Integer.parseInt(simpleDateformat.format(date));
int numYears = 100;
//
what would be the value for 'step' variable for decrement operation? i mean if i give step='-1' i get error. How do i write the decrement operation?
can someone provide me for loop for the above functionality? Thanks in advance
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
I was not aware that c:forEach has any problem with decrementing, and I'm not really able to test that right now.

But as a simple workaround (in case you won't get the proper solution), you can just have some i counter incremented (the traditional way from 0 to 100, and inside the foreach block use (currentYear - i) for year (so you'll just emulate the decrementation).
san krish
Ranch Hand

Joined: Aug 11, 2010
Posts: 41
Thanks for your reply.
But I want to know the validation with decrement operation something like "step=-1". If you have time could you please let me know.

Anyone else, who knows a solution and worked with decrement, please let me know what would be the error.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

The first place you should always check is the JSTL Specification.

From page 43:
If specified, step must be >= 1


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

If all you can get is 1 through 100, and what you need is 2011 through 1912, that just sounds like a simple matter of some algebra to me.
san krish
Ranch Hand

Joined: Aug 11, 2010
Posts: 41
Oh Thanks!
I was blind. did not look into it. That explains.
So the only way would be workaround right? increment then decrement?
san krish
Ranch Hand

Joined: Aug 11, 2010
Posts: 41
Can anyone point out the error in this c:foreach loop
<%int year = Integer.parseInt(simpleDateformat.format(date)) - 100;%>

<c:forEach var="i" begin="<%=year %>" end="<%=year + 100 %>">
<option value="<c:out value="${i+100}"/>"><c:out value="${i+100}"/></option>
</c:forEach>

basically i want a dropdown from 2011 to 1911
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
Your year goes from 100 years ago until now.
For each such year, you're printing the "100 year after" year. Your output probably starts with now and goes 100 years in the future.
Try this:

<%int year = Integer.parseInt(simpleDateformat.format(date));%>

<c:forEach var="i" begin="0" end="100">
<option value="<cut value="${year - i}"/>"><cut value="${year - i}"/></option>
</c:forEach>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

You can't mix scripting variables with the JSTL and EL.
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
You're right... I think I've only used scriptlets once a few years ago...
So, let's assume the ${year} is set as a request attribute...
san krish
Ranch Hand

Joined: Aug 11, 2010
Posts: 41
Thanks all for your valuable time and help...
now I have come up with a new question.

I have a JSTL that displays 3 dropdowns,(month/day/year) as follows:
<form:select path="months" items="${amonths}"/>
<form:select path="days" items="${adays}"/>
<form:select path="years" items="${ayears}"/>
months dropdown has values from (1-12), day dropdown from (1-31) year dropdown from (2011-1911)
Now i want to make JS validation such that, if month is selected as '2' for Feb, days dropdown should display values from (1-28) dynamically and so for every month.
Jan should show 1-31 feb 1-28 except leap yr march 1-31 and so on...
How can i achieve this in JS?
san krish
Ranch Hand

Joined: Aug 11, 2010
Posts: 41
I do not want to use Ajax though. Is it possible in JS?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

Who said anything about Ajax or even JavaScript?

Just do some math to get the right values.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL for loop