You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Scott Sauyet <li...@sauyet.com> on 2005/06/09 16:48:08 UTC

Newbie question about ExternalLink

I'm brand-new to Tapestry.  I've read the tapestry-user list back 
through the beginning of the month (200 messages already!)  I've been 
through a basic tutorial, and can do some simple things with Tapestry, 
but I haven't yet really gotten a feel for it.  I suspect the question 
below is pretty naive, but I'm hoping someone has a few minutes to 
either suggest a solution or point me to an appropriate reference.  BTW, 
although new to Tapestry, I'm fairly experienced with Java and XML, and 
have used several other web framworks (Struts, SpringMVC).

----------

I want to create links to external pages based on internal data.  The 
url parameters would be based on data stored in one of my objects.  In 
PHP or JSP I would use processing instructions inside the href attribute 
of my template to grab the data.  I'm not sure how to do it here.

More specifically, I'm used to doing something like (in some pseudo-app 
syntax):

     <p>View this location in <a 
href="http://maps.google.com/maps?q=<?location.address.line?>+<?location.address.city?>+<?location.address.state?>+<?location.address.zip?>">Google 
Maps</a>
     or <a 
href="http://maps.yahoo.com/maps_result?addr=<?location.address.line?>&csz=<?location.address.city?>+<?location.address.state?>+<?location.address.zip?>&country=us">Yahoo 
Maps</a>
     or <a 
href="http://www.mapquest.com/maps/map.adp?country=US&addtohistory=&searchtab=home&address=<?location.address.line?>&<?location.address.city?>&state=<?location.address.state?>&zipcode=<?location.address.zip?>">Mapquest</a>.
     </p>

The point is that the address components (zip, citey, state, etc.) are 
properties of an Address object, which is a property of the Location 
object known to the page.

What would be the Tapestry way of building this?  I see the ExternalLink 
class, and am wondering if that's what I need, and if that's the case, 
do I need to build separate supporting classes for each of those 
different URL styles?  Should I be using some other Tapestry object?  Or 
is there a more direct way to embed this data in the page?  Everything I 
see allows me to embed data in HTML elements, but I dont' see how I 
might extend this to embedding data in attributes.

So... any suggestions?  Pointers to the right portions of the fine 
manual?  Or to a working example which does this?

Thanks for any help you can offer,

   -- Scott Sauyet

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


Re: Newbie question about ExternalLink

Posted by Scott Sauyet <li...@sauyet.com>.
Shawn,

Thanks for your help.  I think I'm starting to grasp more of this 
Tapestry stuff.


Shawn Church wrote:
> You are correct, the @Insert I showed would just render the URL string
> itself.  I was mainly trying to illustrate the example, but in reality
> you would probably want to use an @Any in that case as well, e.g.:
> 
> <a jwcid="@Any" href="ognl:yahooUrl">Yahoo</a>

Ahh, that's what I was missing -- arbitrary attributes are also replaced 
if they have Tapestry values.  With this I can easily see the trade-offs 
between the Java code and the other @Any method you suggested.  I knew 
it couldn't be as hard as I was making it!


> Regarding the "possibly cleaner" comment, I wasn't recommending doing
> any presentation formatting within your java code, but rather only
> providing the data needed by the page.  I don't normally encode any html
> in my java code unless I'm writing a component which is designed to
> render html.  By "possibly cleaner", I was really talking about avoiding
> a lot of messy OGNL string concatenations and other formatting, since it
> may become a problem if you need to (for example) embed single or double
> quotes in the string, or if your data objects may contain null values,
> or if you need to conditionally alter the format based on certain object
> values, etc. [ ... ]

Yes, that makes sense.  Clearly if I need to use the same URL format on 
multiple pages, Java will be the way to go.  I think I may end up doing 
it this way anyway, at least for the learning experience.

Thank you again.  Your detailed responses have made this quite clear. 
And I think I'm starting to like Tapestry.

   -- Scott

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


Re: Newbie question about ExternalLink

Posted by Shawn Church <sh...@boxity.com>.
Scott,

You are correct, the @Insert I showed would just render the URL string
itself.  I was mainly trying to illustrate the example, but in reality
you would probably want to use an @Any in that case as well, e.g.:

<a jwcid="@Any" href="ognl:yahooUrl">Yahoo</a>

Regarding the "possibly cleaner" comment, I wasn't recommending doing
any presentation formatting within your java code, but rather only
providing the data needed by the page.  I don't normally encode any html
in my java code unless I'm writing a component which is designed to
render html.  By "possibly cleaner", I was really talking about avoiding
a lot of messy OGNL string concatenations and other formatting, since it
may become a problem if you need to (for example) embed single or double
quotes in the string, or if your data objects may contain null values,
or if you need to conditionally alter the format based on certain object
values, etc.  I've found it is often better to wrap as much of this sort
of logic as possible within my java code.  By better, I mean more
reliable, more readable, more easily maintained.  The java code would
typically not provide any html formatting, but would provide as much of
the data needed by the page as possible.  Keep in mind that the java
code backing a particular page is really intended only to serve the
needs of that page.

This recommendation is especially important if you end up needing
similar html rendering in multiple pages.  In this case, you might write
a single utility class to provide Map URLs, and then you can easily drop
them in any page you like (provided the pages have access to your
utility class).  This is a case where it might in fact justify creating
a custom component.

However, in some cases it is fine to just format everything with OGNL,
so it really depends on your use case.  You might also consider defining
a .page component of an @Any type, and do your formatting there, so at
least your .html page is cleaner.  There's an example of this on the
Tapestry @Any component reference.

Shawn


Quoting Scott Sauyet <li...@sauyet.com>:

> Shawn Church wrote:
> > ExternalLink is used to provide a URL which may be used outside
> the
> > context of a Tapestry webapp to link back into the webapp.  [ ...
> ]
> 
> See, I told you I was a newbie!  :-)
> 
> > I think what you are wanting is to provide links to external
> > (non-Tapestry) applications from within a Tapestry app.  
> 
> Exactly.
> 
> >                                                          There are
> many
> > ways to do this, but two common ways are to either use the @Any
> > component to build the URL within your .html (or .page) directly,
> or a
> > possibly cleaner way is to build the URL strings within your .java
> page
> > handler.
> > 
> 
> > Using the @Any solution:
> > 
> > <a jwcid="@Any" href='ognl:
> "http://maps.yahoo.com/maps_result?addr=" +
> > location.address.line + "&csz=" + location.address.city'> Yahoo Map
> </a>
> 
> Thank you.  The @Any solution is perhaps what I was looking for.
> 
> 
> > Using the .java method solution:
> > 
> > public String getYahooUrl()
> > {
> >    String url = "http://maps.yahoo.com/maps_result";
> >    url += "?addr=" + getLocation().getAddress().getLine();
> >    url += "&csz=" + getLocation().getAddress().getCity();
> >    return url;
> > }
> > 
> > <span jwcid="@Insert" value="ognl:yahooUrl" raw="ognl:true"/>
> 
> I'm not certain about this Java method.  It seems to me that it would
> 
> just output the URL into the document.  Is there a way to get this
> into 
> an href attribute?
> 
> The other question about the Java method is whether this is
> considered 
> good practice in Tapestry.  It seems to me a matter of moving 
> presentation data out to the template and back into the Java code,
> which 
> seems counter to most other MVC systems I've seen, especially as I
> would 
> need to add methods for every new URL format needed.
> 
> So when you say that this is "possibly cleaner", is the Tapestry 
> experience that it's better to keep the templates clean and offload
> some 
> formatting into Java?  I'm surprised if this is the case, but would
> love 
> to hear the arguments in favor of it.
> 
> Thank you very much for your helpful response.
> 
>    -- Scott
> 
> ---------------------------------------------------------------------
> 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


Re: Newbie question about ExternalLink

Posted by Scott Sauyet <li...@sauyet.com>.
Shawn Church wrote:
> ExternalLink is used to provide a URL which may be used outside the
> context of a Tapestry webapp to link back into the webapp.  [ ... ]

See, I told you I was a newbie!  :-)

> I think what you are wanting is to provide links to external
> (non-Tapestry) applications from within a Tapestry app.  

Exactly.

>                                                          There are many
> ways to do this, but two common ways are to either use the @Any
> component to build the URL within your .html (or .page) directly, or a
> possibly cleaner way is to build the URL strings within your .java page
> handler.
> 

> Using the @Any solution:
> 
> <a jwcid="@Any" href='ognl: "http://maps.yahoo.com/maps_result?addr=" +
> location.address.line + "&csz=" + location.address.city'> Yahoo Map </a>

Thank you.  The @Any solution is perhaps what I was looking for.


> Using the .java method solution:
> 
> public String getYahooUrl()
> {
>    String url = "http://maps.yahoo.com/maps_result";
>    url += "?addr=" + getLocation().getAddress().getLine();
>    url += "&csz=" + getLocation().getAddress().getCity();
>    return url;
> }
> 
> <span jwcid="@Insert" value="ognl:yahooUrl" raw="ognl:true"/>

I'm not certain about this Java method.  It seems to me that it would 
just output the URL into the document.  Is there a way to get this into 
an href attribute?

The other question about the Java method is whether this is considered 
good practice in Tapestry.  It seems to me a matter of moving 
presentation data out to the template and back into the Java code, which 
seems counter to most other MVC systems I've seen, especially as I would 
need to add methods for every new URL format needed.

So when you say that this is "possibly cleaner", is the Tapestry 
experience that it's better to keep the templates clean and offload some 
formatting into Java?  I'm surprised if this is the case, but would love 
to hear the arguments in favor of it.

Thank you very much for your helpful response.

   -- Scott

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


Re: Newbie question about ExternalLink

Posted by Shawn Church <sh...@boxity.com>.
ExternalLink is used to provide a URL which may be used outside the
context of a Tapestry webapp to link back into the webapp.  The target
Tapestry page needs to implement IExternalPage and the
activateExternalPage method, and it is up to your app to do whatever it
wants with the parameter array passed to this method.

I think what you are wanting is to provide links to external
(non-Tapestry) applications from within a Tapestry app.  There are many
ways to do this, but two common ways are to either use the @Any
component to build the URL within your .html (or .page) directly, or a
possibly cleaner way is to build the URL strings within your .java page
handler.

Using the @Any solution:

<a jwcid="@Any" href='ognl: "http://maps.yahoo.com/maps_result?addr=" +
location.address.line + "&csz=" + location.address.city'> Yahoo Map </a>

Using the .java method solution:

public String getYahooUrl()
{
   String url = "http://maps.yahoo.com/maps_result";
   url += "?addr=" + getLocation().getAddress().getLine();
   url += "&csz=" + getLocation().getAddress().getCity();
   return url;
}

<span jwcid="@Insert" value="ognl:yahooUrl" raw="ognl:true"/>


Shawn


Quoting Scott Sauyet <li...@sauyet.com>:

> I'm brand-new to Tapestry.  I've read the tapestry-user list back 
> through the beginning of the month (200 messages already!)  I've been
> 
> through a basic tutorial, and can do some simple things with
> Tapestry, 
> but I haven't yet really gotten a feel for it.  I suspect the
> question 
> below is pretty naive, but I'm hoping someone has a few minutes to 
> either suggest a solution or point me to an appropriate reference. 
> BTW, 
> although new to Tapestry, I'm fairly experienced with Java and XML,
> and 
> have used several other web framworks (Struts, SpringMVC).
> 
> ----------
> 
> I want to create links to external pages based on internal data.  The
> 
> url parameters would be based on data stored in one of my objects. 
> In 
> PHP or JSP I would use processing instructions inside the href
> attribute 
> of my template to grab the data.  I'm not sure how to do it here.
> 
> More specifically, I'm used to doing something like (in some
> pseudo-app 
> syntax):
> 
>      <p>View this location in <a 
>
href="http://maps.google.com/maps?q=<?location.address.line?>+<?location.address.city?>+<?location.address.state?>+<?location.address.zip?>">Google
> 
> Maps</a>
>      or <a 
>
href="http://maps.yahoo.com/maps_result?addr=<?location.address.line?>&csz=<?location.address.city?>+<?location.address.state?>+<?location.address.zip?>&country=us">Yahoo
> 
> Maps</a>
>      or <a 
>
href="http://www.mapquest.com/maps/map.adp?country=US&addtohistory=&searchtab=home&address=<?location.address.line?>&<?location.address.city?>&state=<?location.address.state?>&zipcode=<?location.address.zip?>">Mapquest</a>.
>      </p>
> 
> The point is that the address components (zip, citey, state, etc.)
> are 
> properties of an Address object, which is a property of the Location
> 
> object known to the page.
> 
> What would be the Tapestry way of building this?  I see the
> ExternalLink 
> class, and am wondering if that's what I need, and if that's the
> case, 
> do I need to build separate supporting classes for each of those 
> different URL styles?  Should I be using some other Tapestry object? 
> Or 
> is there a more direct way to embed this data in the page? 
> Everything I 
> see allows me to embed data in HTML elements, but I dont' see how I 
> might extend this to embedding data in attributes.
> 
> So... any suggestions?  Pointers to the right portions of the fine 
> manual?  Or to a working example which does this?
> 
> Thanks for any help you can offer,
> 
>    -- Scott Sauyet
> 
> ---------------------------------------------------------------------
> 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