tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Tapestry > Link Components FAQ
Date Wed, 30 Nov 2011 00:36:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/12/_/styles/combined.css?spaceKey=TAPESTRY&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/TAPESTRY/Link+Components+FAQ">Link
Components FAQ</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~hlship">Howard
M. Lewis Ship</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{since} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
How do I create a Link back to the current page from a component? <br> <br>Sometimes
it is useful to create a link back to the current page, but you don&#39;t always know
the name of the page (the link may appear inside a deeply nested subcomponent).  Fortunately,
this is easy.  <br> <br>{code} <br>&lt;t:pagelink page=&quot;prop:componentResources.pageName&quot;&gt;refresh
page&lt;/t:pagelink&gt; <br>{code} <br> <br>Every component has
an extra property, componentResources, added to it: it&#39;s the instance of [ComponentResources|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ComponentResources.html]
that represents the link between your code and all of Tapestry&#39;s structure around
your class. One of the properties of ComponentResources is pageName, the name of the page.
By binding the PageLink&#39;s page parameter with the &quot;prop:&quot; binding
prefix, we ensure that we bind to a computed property; this is necessary because the PageLink.page
parameter defaults to the &quot;literal:&quot; binding prefix. <br> <br>As
an added benefit, if the page class is ever renamed or moved to a different package, the pageName
property will automatically adjust to the new name. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >{scrollbar} <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <style type='text/css'>/*<![CDATA[*/
table.ScrollbarTable  {border: none;padding: 3px;width: 100%;padding: 3px;margin: 0px;background-color:
#f0f0f0}
table.ScrollbarTable td.ScrollbarPrevIcon {text-align: center;width: 16px;border: none;}
table.ScrollbarTable td.ScrollbarPrevName {text-align: left;border: none;}
table.ScrollbarTable td.ScrollbarParent {text-align: center;border: none;}
table.ScrollbarTable td.ScrollbarNextName {text-align: right;border: none;}
table.ScrollbarTable td.ScrollbarNextIcon {text-align: center;width: 16px;border: none;}

/*]]>*/</style><div class="Scrollbar"><table class='ScrollbarTable'><tr><td
class='ScrollbarPrevIcon'><a href="/confluence/display/TAPESTRY/BeanEditForm+FAQ"><img
border='0' align='middle' src='/confluence/images/icons/back_16.gif' width='16' height='16'></a></td><td
width='33%' class='ScrollbarPrevName'><a href="/confluence/display/TAPESTRY/BeanEditForm+FAQ">BeanEditForm
FAQ</a>&nbsp;</td><td width='33%' class='ScrollbarParent'><sup><a
href="/confluence/display/TAPESTRY/Frequently+Asked+Questions"><img border='0' align='middle'
src='/confluence/images/icons/up_16.gif' width='8' height='8'></a></sup><a
href="/confluence/display/TAPESTRY/Frequently+Asked+Questions">Frequently Asked Questions</a></td><td
width='33%' class='ScrollbarNextName'>&nbsp;<a href="/confluence/display/TAPESTRY/Component+Events+FAQ">Component
Events FAQ</a></td><td class='ScrollbarNextIcon'><a href="/confluence/display/TAPESTRY/Component+Events+FAQ"><img
border='0' align='middle' src='/confluence/images/icons/forwd_16.gif' width='16' height='16'></a></td></tr></table></div>

<h2><a name="LinkComponentsFAQ-LinkComponents"></a>Link Components</h2>

<h3><a name="LinkComponentsFAQ-HowdoIaddqueryparameterstoaPageLinkorActionLink%3F"></a>How
do I add query parameters to a PageLink or ActionLink?</h3>

<p>These components do not have parameters to allow you to specify query parameters
for the link; they both allow you to specify a <em>context</em> (one or more values
to encode into the request path).</p>

<p>However, you can accomplish the same thing with a little code and markup.  For example,
to create a link to another page and pass a query parameter, you can replace your PageLink
component with a standard <tt>&lt;a&gt;</tt> tag:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;a href=<span class="code-quote">"${profilePageLink}"</span>&gt;Display
Profile (w/ full details)&lt;/a&gt;
</pre>
</div></div>

<p>In the matching Java class, you can create the Link programmatically:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  @Inject
  <span class="code-keyword">private</span> PageRenderLinkSource linkSource;

  <span class="code-keyword">public</span> Link getProfilePageLink()
  {
    Link link = linkSource.createPageRenderLinkWithContext(DisplayProfile.class, user);

    link.addParameterValue(<span class="code-quote">"detail"</span>, <span
class="code-keyword">true</span>);

    <span class="code-keyword">return</span> link;
  }
</pre>
</div></div>

<p>... and in the DisplayProfile page:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class DisplayProfile
{
  void onActivate(@RequestParameter(<span class="code-quote">"detail"</span>)
<span class="code-object">boolean</span> detail)
  {
    . . .
  }
}
</pre>
</div></div>

<p>The @RequestParameter annotation directs Tapestry to extract the query parameter
from the request and coerce it to type boolean.  You can use any reasonable type for such
a parameter (int, long and Date are common).</p>

<p>A similar technique can be used to add query parmeters to component event URLs (the
type generated by the ActionLink or EventLink components), by injecting the ComponentResources,
and invoking method <tt>createEventLink()</tt>.</p>




<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Added in
5.3</b><br /></td></tr></table></div>
<div style="border-right: 20px solid #D8E4F1;border-left: 20px solid #D8E4F1;">
<p>You may also bind a link component's <tt>parameters</tt> parameter; this
is a Map of additional query parameters to add to the URL.  The Map keys should be strings,
and the Map values will be encoded to strings.  Tapestry 5.3 also adds a literal map syntax
to the <a href="/confluence/display/TAPESTRY/Property+Expressions" title="Property Expressions">property
expression language</a>.</p>

</div>

<h3><a name="LinkComponentsFAQ-HowdoIcreateaLinkbacktothecurrentpagefromacomponent%3F"></a>How
do I create a Link back to the current page from a component?</h3>

<p>Sometimes it is useful to create a link back to the current page, but you don't always
know the name of the page (the link may appear inside a deeply nested subcomponent).  Fortunately,
this is easy. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;t:pagelink page=<span class="code-quote">"prop:componentResources.pageName"</span>&gt;refresh
page&lt;/t:pagelink&gt;
</pre>
</div></div>

<p>Every component has an extra property, componentResources, added to it: it's the
instance of <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ComponentResources.html"
class="external-link" rel="nofollow">ComponentResources</a> that represents the link
between your code and all of Tapestry's structure around your class. One of the properties
of ComponentResources is pageName, the name of the page. By binding the PageLink's page parameter
with the "prop:" binding prefix, we ensure that we bind to a computed property; this is necessary
because the PageLink.page parameter defaults to the "literal:" binding prefix.</p>

<p>As an added benefit, if the page class is ever renamed or moved to a different package,
the pageName property will automatically adjust to the new name.</p>

<style type='text/css'>/*<![CDATA[*/
table.ScrollbarTable  {border: none;padding: 3px;width: 100%;padding: 3px;margin: 0px;background-color:
#f0f0f0}
table.ScrollbarTable td.ScrollbarPrevIcon {text-align: center;width: 16px;border: none;}
table.ScrollbarTable td.ScrollbarPrevName {text-align: left;border: none;}
table.ScrollbarTable td.ScrollbarParent {text-align: center;border: none;}
table.ScrollbarTable td.ScrollbarNextName {text-align: right;border: none;}
table.ScrollbarTable td.ScrollbarNextIcon {text-align: center;width: 16px;border: none;}

/*]]>*/</style><div class="Scrollbar"><table class='ScrollbarTable'><tr><td
class='ScrollbarPrevIcon'><a href="/confluence/display/TAPESTRY/BeanEditForm+FAQ"><img
border='0' align='middle' src='/confluence/images/icons/back_16.gif' width='16' height='16'></a></td><td
width='33%' class='ScrollbarPrevName'><a href="/confluence/display/TAPESTRY/BeanEditForm+FAQ">BeanEditForm
FAQ</a>&nbsp;</td><td width='33%' class='ScrollbarParent'><sup><a
href="/confluence/display/TAPESTRY/Frequently+Asked+Questions"><img border='0' align='middle'
src='/confluence/images/icons/up_16.gif' width='8' height='8'></a></sup><a
href="/confluence/display/TAPESTRY/Frequently+Asked+Questions">Frequently Asked Questions</a></td><td
width='33%' class='ScrollbarNextName'>&nbsp;<a href="/confluence/display/TAPESTRY/Component+Events+FAQ">Component
Events FAQ</a></td><td class='ScrollbarNextIcon'><a href="/confluence/display/TAPESTRY/Component+Events+FAQ"><img
border='0' align='middle' src='/confluence/images/icons/forwd_16.gif' width='16' height='16'></a></td></tr></table></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/TAPESTRY/Link+Components+FAQ">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=23335486&revisedVersion=9&originalVersion=8">View
Changes</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message