You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Laurie Mac <la...@hotmail.com> on 2004/07/09 16:26:32 UTC

Dynamically generating ActionLinks

Hi,

I'm working on developing a framework for managing undergraduate programming 
projects using Tapestry.  I'm having a problem with generating links.  We 
need a menu bar that has links to all the projects a user is involved in.  
Each project has some number of children, so it's a hierarchical structure.  
Also, each project has some number of other links within it, and each link 
may or may not display depending on the user.  I don't believe there's a 
component that can handle this kind of structure, so I wanted to use 
IMarkupWriter to print the HTML directly from the Java code.  The problem 
I'm having is in generating the URLs.  When Tapestry processes a template 
normally, it does various transformations to assign unique ids and such to 
the links.  My page is having trouble in the rewind phase because it's not 
matching up the ids properly.  How can I create the ActionLink the same way 
Tapestry does?

So far, I've tried to work around it like so:
(There is a Main page which uses the Border component which has an embedded 
ProjectTree component.  I added a "menuLink" in the ProjectTree 
specification, so I could use getComponent() to pass the object to 
getLink().)

Inside ProjectTree.java I've got this code:
    Object[] parameters= new Object[1];
    parameters[0]= "0";
    IEngineService service = 
getPage().getEngine().getService(Tapestry.ACTION_SERVICE);
    ILink link = service.getLink(getPage().getRequestCycle(), 
getComponent("menuLink"),  parameters);
    writer.begin("li");
        writer.attribute("class", "project");
        writer.begin("a");
            writer.attribute("href", link.getURL());
            writer.attribute("listener", "ognl:listeners.onClick");
            writer.attribute("parameters", p.getProjectPath());
                 writer.print(p.getName());
            writer.end("li");

But the links print out as:
<a 
href="/helium.Tapestry/app?service=action/1/Main/0/$Border.$ProjectTree.menuLink" 
listener="ognl:listeners.onClick" parameters="Helium">Helium</a>

instead of adding the $0 etc. that it normally does.

Is there a better way to do this?  It seems like the sort of thing there 
would be a simple command for, but I haven't been able to find it anywhere.

Thanks in advance for your help,
~Laurie

_________________________________________________________________
MSN Premium with Virus Guard and Firewall* from McAfeeŽ Security : 2 months 
FREE*   
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Dynamically generating ActionLinks

Posted by Howard Lewis Ship <hl...@gmail.com>.
Why are you using ActionLink?  For what you are trying to do,
DirectLink is appropriate.

I would like to remove ActionLink and the action service entirely, but
my loyal following won't let me :-)

You can bind parameters to the link using the parameters parameter of
the DirectLink component, then retrieve those values via
cycle.getServiceParameters().


On Fri, 9 Jul 2004 10:00:51 -0700, Philippe Paravicini
<pp...@intersight.com> wrote:
> Have you tried using a DirectLink and passing a list of parameters
> (such as ProjectId) ?
> 
> In your .html page:
> 
>         <a href="#" jwcid="@DirectLink"
> listener="ognl:listeners.toProjectPage"
> parameters="ognl:projectId">...</a>
> 
> Then the listener in your java code would look something like:
> 
>         /** responds to a Student clicking on a Project link */
>         public void toProjectPage(IRequestCycle cycle)
>         {
>                 String projectId  = this.getStringParam(cycle, 0);
>                 ProjectDetailPage page =
> (ProjectDetailPage)cycle.getPage("ProjectDetail");
>                 page.setProjectId(projectId);
>                 cycle.activate(page);
>         }
> 
> where getStringParam(cycle,0) is the optional convenience method (I
> have an equivalent one for Integer params):
> 
>         /**
>          * Convenience method that returns the N-th string parameter passed in
> the url
>          * through a DirectLink or External Link
>          */
>         public static String getStringParam(IRequestCycle cycle, int idx)
>         {
>                 Object[] params = cycle.getServiceParameters();
>                 if (params != null)
>                 {
>                         int count = 0;
>                         for (int i=0; i < params.length ; i++)
>                         {
>                                 if (params[i] instanceof String)
>                                 {
>                                         if (count == idx) return (String)params[i];
>                                         count++;
>                                 }
>                         }
>                 }
>                 return null;
>         }
> 
> I hope this helps !
> 
> Philippe Paravicini
> eCommerce Developer
> 
> 
> 
> On Jul 9, 2004, at 7:26 AM, Laurie Mac wrote:
> 
> > Hi,
> >
> > I'm working on developing a framework for managing undergraduate
> > programming projects using Tapestry.  I'm having a problem with
> > generating links.  We need a menu bar that has links to all the
> > projects a user is involved in.  Each project has some number of
> > children, so it's a hierarchical structure.  Also, each project has
> > some number of other links within it, and each link may or may not
> > display depending on the user.  I don't believe there's a component
> > that can handle this kind of structure, so I wanted to use
> > IMarkupWriter to print the HTML directly from the Java code.  The
> > problem I'm having is in generating the URLs.  When Tapestry processes
> > a template normally, it does various transformations to assign unique
> > ids and such to the links.  My page is having trouble in the rewind
> > phase because it's not matching up the ids properly.  How can I create
> > the ActionLink the same way Tapestry does?
> >
> > So far, I've tried to work around it like so:
> > (There is a Main page which uses the Border component which has an
> > embedded ProjectTree component.  I added a "menuLink" in the
> > ProjectTree specification, so I could use getComponent() to pass the
> > object to getLink().)
> >
> > Inside ProjectTree.java I've got this code:
> >    Object[] parameters= new Object[1];
> >    parameters[0]= "0";
> >    IEngineService service =
> > getPage().getEngine().getService(Tapestry.ACTION_SERVICE);
> >    ILink link = service.getLink(getPage().getRequestCycle(),
> > getComponent("menuLink"),  parameters);
> >    writer.begin("li");
> >        writer.attribute("class", "project");
> >        writer.begin("a");
> >            writer.attribute("href", link.getURL());
> >            writer.attribute("listener", "ognl:listeners.onClick");
> >            writer.attribute("parameters", p.getProjectPath());
> >                 writer.print(p.getName());
> >            writer.end("li");
> >
> > But the links print out as:
> > <a
> > href="/helium.Tapestry/app?service=action/1/Main/0/
> > $Border.$ProjectTree.menuLink" listener="ognl:listeners.onClick"
> > parameters="Helium">Helium</a>
> >
> > instead of adding the $0 etc. that it normally does.
> >
> > Is there a better way to do this?  It seems like the sort of thing
> > there would be a simple command for, but I haven't been able to find
> > it anywhere.
> >
> > Thanks in advance for your help,
> > ~Laurie
> >
> > _________________________________________________________________
> > MSN Premium with Virus Guard and Firewall* from McAfee® Security : 2
> > months FREE*
> > http://join.msn.com/?pgmarket=en-ca&page=byoa/
> > prem&xAPID=1994&DI=1034&SU=http://hotmail.com/
> > enca&HL=Market_MSNIS_Taglines
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 


-- 
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Jakarta Tapestry
Creator, Jakarta HiveMind
http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Dynamically generating ActionLinks

Posted by Philippe Paravicini <pp...@intersight.com>.
Have you tried using a DirectLink and passing a list of parameters  
(such as ProjectId) ?

In your .html page:

	<a href="#" jwcid="@DirectLink"  
listener="ognl:listeners.toProjectPage"  
parameters="ognl:projectId">...</a>


Then the listener in your java code would look something like:

	/** responds to a Student clicking on a Project link */
	public void toProjectPage(IRequestCycle cycle)
	{
		String projectId  = this.getStringParam(cycle, 0);
		ProjectDetailPage page =  
(ProjectDetailPage)cycle.getPage("ProjectDetail");
		page.setProjectId(projectId);
		cycle.activate(page);
	}


where getStringParam(cycle,0) is the optional convenience method (I  
have an equivalent one for Integer params):

	/**
	 * Convenience method that returns the N-th string parameter passed in  
the url
	 * through a DirectLink or External Link
	 */
	public static String getStringParam(IRequestCycle cycle, int idx)
	{
		Object[] params = cycle.getServiceParameters();
		if (params != null)
		{
			int count = 0;
			for (int i=0; i < params.length ; i++)
			{
				if (params[i] instanceof String)
				{
					if (count == idx) return (String)params[i];
					count++;
				}
			}
		}
		return null;
	}

I hope this helps !

Philippe Paravicini
eCommerce Developer

On Jul 9, 2004, at 7:26 AM, Laurie Mac wrote:

> Hi,
>
> I'm working on developing a framework for managing undergraduate  
> programming projects using Tapestry.  I'm having a problem with  
> generating links.  We need a menu bar that has links to all the  
> projects a user is involved in.  Each project has some number of  
> children, so it's a hierarchical structure.  Also, each project has  
> some number of other links within it, and each link may or may not  
> display depending on the user.  I don't believe there's a component  
> that can handle this kind of structure, so I wanted to use  
> IMarkupWriter to print the HTML directly from the Java code.  The  
> problem I'm having is in generating the URLs.  When Tapestry processes  
> a template normally, it does various transformations to assign unique  
> ids and such to the links.  My page is having trouble in the rewind  
> phase because it's not matching up the ids properly.  How can I create  
> the ActionLink the same way Tapestry does?
>
> So far, I've tried to work around it like so:
> (There is a Main page which uses the Border component which has an  
> embedded ProjectTree component.  I added a "menuLink" in the  
> ProjectTree specification, so I could use getComponent() to pass the  
> object to getLink().)
>
> Inside ProjectTree.java I've got this code:
>    Object[] parameters= new Object[1];
>    parameters[0]= "0";
>    IEngineService service =  
> getPage().getEngine().getService(Tapestry.ACTION_SERVICE);
>    ILink link = service.getLink(getPage().getRequestCycle(),  
> getComponent("menuLink"),  parameters);
>    writer.begin("li");
>        writer.attribute("class", "project");
>        writer.begin("a");
>            writer.attribute("href", link.getURL());
>            writer.attribute("listener", "ognl:listeners.onClick");
>            writer.attribute("parameters", p.getProjectPath());
>                 writer.print(p.getName());
>            writer.end("li");
>
> But the links print out as:
> <a  
> href="/helium.Tapestry/app?service=action/1/Main/0/ 
> $Border.$ProjectTree.menuLink" listener="ognl:listeners.onClick"  
> parameters="Helium">Helium</a>
>
> instead of adding the $0 etc. that it normally does.
>
> Is there a better way to do this?  It seems like the sort of thing  
> there would be a simple command for, but I haven't been able to find  
> it anywhere.
>
> Thanks in advance for your help,
> ~Laurie
>
> _________________________________________________________________
> MSN Premium with Virus Guard and Firewall* from McAfee® Security : 2  
> months FREE*    
> http://join.msn.com/?pgmarket=en-ca&page=byoa/ 
> prem&xAPID=1994&DI=1034&SU=http://hotmail.com/ 
> enca&HL=Market_MSNIS_Taglines
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org