Avatar billede fredand Forsker
28. februar 2006 - 08:29 Der er 7 kommentarer og
1 løsning

How to iterate over links with struts and give them params?

Hello!

I got a problem with a BodyTag. The body tag is used like:

<mytags:LinkLoop>
    <html:link page="/by.jsp" scope="page" name="params">Buy</html:link>
</mytags:LinkLoop>

As you can see the tag will iterate and display a couple of links created from the Struts tag html:link.

The problem is that I would like to add some params to the link. The params will be set into the pageContext by my tag LoopLink. But I can not get it right. Please look at this code:

public class LoopLink extends BodyTagSupport
{
    int times = 10;
   
    public void doInitBody()
    {
        if(times>0)
        {
            HashMap hashMap = new HashMap();
            {
                hashMap.put("id", ""+times);
            }
            pageContext.setAttribute("params", hashMap);
        }
    }
   
   
    public int doAfterBody()
    {
        if(times>0)
        {
            HashMap hashMap = new HashMap();
            {
                hashMap.put("id", ""+times);
            }
            pageContext.setAttribute("params", hashMap);
            return EVAL_BODY_TAG;
        }
        else
        {
            return SKIP_BODY;
        }
    }
}


The problem is that the "params" does not seems to change. I just get 10 links with id like:
id=10
id=9
id=9
id=9
id=9
id=9
id=9
id=9
id=9
id=9

Do you see why this occurs?

Best regards
Fredrik
Avatar billede kalp Novice
28. februar 2006 - 09:07 #1
I cant see anywhere that count down time...

like this

time--;

or

time = time - 1;
Avatar billede fredand Forsker
28. februar 2006 - 10:39 #2
Hello!
Sorry, I forgot that. Right now I'm at work and my code are at home so I hope I now have give you the whole picture, see below!

    public int doAfterBody()
    {
        if(times>0)
        {
            HashMap hashMap = new HashMap();
            {
                hashMap.put("id", ""+times);
            }
            pageContext.setAttribute("params", hashMap);
time--;
            return EVAL_BODY_TAG;
        }
        else
        {
            return SKIP_BODY;
        }
    }
Avatar billede kalp Novice
28. februar 2006 - 10:45 #3
I not all in Struts, but i suppose that "if(times>0)" should be "while(times>0)" unless you keep calling doAfterBody from some other place..

might be sine you do get a print of 10 links:)

doInitBody()

should probably also have the time--; part... so try and change that first.
Avatar billede fredand Forsker
28. februar 2006 - 10:51 #4
Hello!
Correct me if I'm wrong when I say that doAfterBody is called each time the body of a tag should be evaluated by the Container???

Then I guess that doAfterBody is called untill it returns SKIP_BODY???

Fredrik
Avatar billede kalp Novice
28. februar 2006 - 10:54 #5
I forgot all about this:)

<mytags:LinkLoop>
    <html:link page="/by.jsp" scope="page" name="params">Buy</html:link>
</mytags:LinkLoop>


sorry
Avatar billede fredand Forsker
30. december 2006 - 13:31 #6
Please give svar so I can reward you!
Avatar billede kalp Novice
10. januar 2007 - 22:03 #7
Thanks for remembering:o)

This is a "Svar" ;)
Avatar billede fredand Forsker
11. januar 2007 - 09:11 #8
Thanks mate!
Hope to see you around again!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester