For loop not working from static method  
Author Message
francan00





PostPosted: 2007-8-31 8:18:00 Top

java-programmer, For loop not working from static method I have a JSP that outputs 10 links and it works great but want to cut
down on the scriptlet lines in my JSP.
Now I want to put the for loop that outputs the 10 links into a source
file and call the class in my JSP using just one line scriptlet.

Here is what my current JSP looks like where it outputs the 10 links:

<jsp:useBean id="pageinfo" class="storm.Pageinfo" scope="session" />
.....
<%
if (pageinfo!=null)
{
for(int i=0;i < 10;i++)
{
out.println("<a href=moveto.jsp?inpage=" + i + ">" + i + "</
a>");
}
}

%>


Now my attempt to put it in a class outputs only 1 link instead of 10.

Source code for the Java class:

package storm;
import storm.*;

public class PageUtil
{
public static String theMethod(Pageinfo pageinfo)
{
if (pageinfo!=null)
{
for(int i=0;i < 10;i++)
{
return "<a href=moveto.jsp?inpage=" + i + ">" + i +
"</a>";
}
}
return "";
}
}



JSP scriptlet calling the static method:

<%= PageUtil.theMethod(pageinfo) %>


Please advise how I can get this to work. I am using Tomcat 4.1.27
and dont have JSTL.

 
Manish Pandit





PostPosted: 2007-8-31 13:07:00 Top

java-programmer >> For loop not working from static method On Aug 30, 5:17 pm, email***@***.com wrote:
> public static String theMethod(Pageinfo pageinfo)
> {
> if (pageinfo!=null)
> {
> for(int i=0;i < 10;i++)
> {
> return "<a href=moveto.jsp?inpage=" + i + ">" + i +
> "</a>";
> }
> }
> return "";
> }

You are returning after 1st iteration, that is why you see only 1
link.

Use a variable to buffer the text and then return it after the loop is
over. Like:

public static String theMethod(Pageinfo pageinfo)
{
StringBuffer buffer = new StringBuffer();
if (pageinfo!=null)
{
for(int i=0;i < 10;i++)
{
buffer.append("<a href=moveto.jsp?inpage=" + i + ">"
+ i +
"</a>");
}
}
return buffer.toString();
}

-cheers,
Manish

 
ejablow





PostPosted: 2007-9-2 13:42:00 Top

java-programmer >> For loop not working from static method Manish Pandit <email***@***.com> wrote:

> On Aug 30, 5:17 pm, email***@***.com wrote:
> > public static String theMethod(Pageinfo pageinfo)
> > {
> > if (pageinfo!=null)
> > {
> > for(int i=0;i < 10;i++)
> > {
> > return "<a href=moveto.jsp?inpage=" + i + ">" + i +
> > "</a>";
> > }
> > }
> > return "";
> > }
>
> You are returning after 1st iteration, that is why you see only 1
> link.
>
Aside from this Manish's advice, ask yourself whether you're doing the
right thing in the first place. People try to avoid putting Java
scriptlets into their web pages. Try to learn the Java Standard Tag
Library; you'll find that you need to write less Java code to do your
tasks. After all, they're paying you to solve prolems, not to write
code.

In this case, it would be:

<c:forEach var="i" begin="${0}" end="${10}">
<c:url value="moveto.jsp" var="link">
<c:param name="inpage" value="${i}"/>
</c:url>
<a href='<c:out value="${link}"/>' >
<c:out value="${i}"/>
</a>
</cc:forEach>
</c:forEach>
--
Respectfully,
Eric Jablow
 
 
francan00





PostPosted: 2007-9-6 6:43:00 Top

java-programmer >> For loop not working from static method On Sep 2, 1:41 am, email***@***.com (Eric Jablow) wrote:
> Manish Pandit <email***@***.com> wrote:
> > On Aug 30, 5:17 pm, email***@***.com wrote:
> > > public static String theMethod(Pageinfo pageinfo)
> > > {
> > > if (pageinfo!=null)
> > > {
> > > for(int i=0;i < 10;i++)
> > > {
> > > return "<a href=moveto.jsp?inpage=" + i + ">" + i +
> > > "</a>";
> > > }
> > > }
> > > return "";
> > > }
>
> > You are returning after 1st iteration, that is why you see only 1
> > link.
>
> Aside from this Manish's advice, ask yourself whether you're doing the
> right thing in the first place. People try to avoid putting Java
> scriptlets into their web pages. Try to learn the Java Standard Tag
> Library; you'll find that you need to write less Java code to do your
> tasks. After all, they're paying you to solve prolems, not to write
> code.
>
> In this case, it would be:
>
> <c:forEach var="i" begin="${0}" end="${10}">
> <c:url value="moveto.jsp" var="link">
> <c:param name="inpage" value="${i}"/>
> </c:url>
> <a href='<c:out value="${link}"/>' >
> <c:out value="${i}"/>
> </a>
> </cc:forEach>
> </c:forEach>
> --
> Respectfully,
> Eric Jablow- Hide quoted text -
>
> - Show quoted text -

Thanks, great example. I cant get JSTL loaded due to restrictions but
working on getting it within a year.