Very nice. On May 5, 2011 9:32 AM, <[email protected]> wrote: > Author: robertdzeigler > Date: Thu May 5 16:31:50 2011 > New Revision: 1099864 > > URL: http://svn.apache.org/viewvc?rev=1099864&view=rev > Log: > TAP5-1496: Link components should support easily adding request parameters to the generated link > > Added: > tapestry/tapestry5/trunk/tapestry-core/src/test/app1/LinkQueryParameters.tml > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/LinkQueryParameters.java > Modified: > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/base/AbstractLink.java > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/base/AbstractLinkTest.java > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java > > Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/base/AbstractLink.java > URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/base/AbstractLink.java?rev=1099864&r1=1099863&r2=1099864&view=diff > ============================================================================== > --- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/base/AbstractLink.java (original) > +++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/base/AbstractLink.java Thu May 5 16:31:50 2011 > @@ -1,4 +1,4 @@ > -// Copyright 2007, 2008, 2009 The Apache Software Foundation > +// Copyright 2007, 2008, 2009, 2011 The Apache Software Foundation > // > // Licensed under the Apache License, Version 2.0 (the "License"); > // you may not use this file except in compliance with the License. > @@ -22,6 +22,8 @@ import org.apache.tapestry5.dom.Element; > import org.apache.tapestry5.ioc.annotations.Inject; > import org.apache.tapestry5.services.javascript.JavaScriptSupport; > > +import java.util.Map; > + > /** > * Provides base utilities for classes that generate clickable links. > */ > @@ -41,6 +43,14 @@ public abstract class AbstractLink imple > @Parameter("false") > private boolean disabled; > > + /** > + * If specified, the parameters are added to the link as query parameters in key=value fashion. > + * Both values will be coerced to string using value encoder. > + * @since 5.3 > + */ > + @Parameter(allowNull = false) > + private Map<String, ?> parameters; > + > @Inject > private ComponentResources resources; > > @@ -84,6 +94,8 @@ public abstract class AbstractLink imple > */ > protected final void writeLink(MarkupWriter writer, Link link, Object... namesAndValues) > { > + addParameters(link); > + > element = writer.element("a", "href", buildHref(link)); > > writer.attributes(namesAndValues); > @@ -94,6 +106,21 @@ public abstract class AbstractLink imple > } > > /** > + * Adds any user-defined parameters as query parameters. > + * @param link > + */ > + protected final void addParameters(Link link) > + { > + if (!resources.isBound("parameters")) > + return; > + > + for(Map.Entry<String,?> entry : parameters.entrySet()) > + { > + link.addParameterValue(entry.getKey(), entry.getValue()); > + } > + } > + > + /** > * Returns the most recently rendered {@link org.apache.tapestry5.Link} for this component. Subclasses calculate > * their link value as they render, and the value is valid until the end of the request, or the next time the same > * component renders itself (if inside a loop). > > Added: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/LinkQueryParameters.tml > URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/LinkQueryParameters.tml?rev=1099864&view=auto > ============================================================================== > --- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/LinkQueryParameters.tml (added) > +++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/LinkQueryParameters.tml Thu May 5 16:31:50 2011 > @@ -0,0 +1,30 @@ > +<html t:type="border" xmlns:t=" http://tapestry.apache.org/schema/tapestry_5_0_0.xsd" xmlns:p="tapestry:parameter"> > + <h1>Link Query Parameter Demo</h1> > + > + <ul> > + <li><a t:type="pagelink" page="LinkQueryParameters">Page Link With No Parameters</a></li> > + <li><a t:type="pagelink" page="LinkQueryParameters" parameters="emptyParameters">Page Link With Explicitly Empty Parameters</a></li> > + <li><a t:type="pagelink" page="LinkQueryParameters" parameters="nonEmptyParameters">Page Link With Parameters</a></li> > + > + <li><a t:type="actionlink">Action Link With No Parameters</a></li> > + <li><a t:type="actionlink" parameters="emptyParameters">Action Link With Explicitly Empty Parameters</a></li> > + <li><a t:type="actionlink" parameters="nonEmptyParameters">Action Link With Parameters</a></li> > + > + <li><a t:type="eventlink" event="parameterCheck">Event Link With No Parameters</a></li> > + <li><a t:type="eventlink" event="parameterCheck" parameters="emptyParameters">Event Link With Explicitly Empty Parameters</a></li> > + <li><a t:type="eventlink" event="parameterCheck" parameters="nonEmptyParameters">Event Link With Parameters</a></li> > + </ul> > + > + <div id="parametercheck"> > + <t:if test="hasParameters"> > + <ul> > + <li t:type="loop" source="parameters" value="paramName" class="qparam"> > + ${paramName}: ${paramVal} > + </li> > + </ul> > + <p:else> > + No Parameters > + </p:else> > + </t:if> > + </div> > +</html> > > Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/base/AbstractLinkTest.java > URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/base/AbstractLinkTest.java?rev=1099864&r1=1099863&r2=1099864&view=diff > ============================================================================== > --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/base/AbstractLinkTest.java (original) > +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/base/AbstractLinkTest.java Thu May 5 16:31:50 2011 > @@ -1,4 +1,4 @@ > -// Copyright 2007, 2008, 2009 The Apache Software Foundation > +// Copyright 2007, 2008, 2009, 2011 The Apache Software Foundation > // > // Licensed under the Apache License, Version 2.0 (the "License"); > // you may not use this file except in compliance with the License. > @@ -38,6 +38,9 @@ public class AbstractLinkTest extends In > MarkupWriter writer = new MarkupWriterImpl(); > > train_toURI(link, LINK_URI); > + //note that we aren't trying to test the parameters code here, so we only worry about the single, simplest branch. > + //The parameters code is fully tested in CoreBehaviorsTests. > + expect(resources.isBound("parameters")).andReturn(false); > > resources.renderInformalParameters(writer); > > @@ -66,6 +69,9 @@ public class AbstractLinkTest extends In > > train_toURI(link, LINK_URI); > > + //see note in no_anchor. > + expect(resources.isBound("parameters")).andReturn(false); > + > resources.renderInformalParameters(writer); > > replay(); > > Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java > URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java?rev=1099864&r1=1099863&r2=1099864&view=diff > ============================================================================== > --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java (original) > +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java Thu May 5 16:31:50 2011 > @@ -422,6 +422,66 @@ public class CoreBehaviorsTests extends > assertText("//li[1]", "NULL"); > } > > + private void openLinkParameterTest() > + { > + openLinks("Link Query Parameters Demo"); > + assertText("parametercheck", "No Parameters"); > + } > + > + //TAP5-1496 > + @Test > + public void links_with_unspecified_query_parameter_map() > + { > + openLinkParameterTest(); > + > + clickAndWait("link=Page Link With No Parameters"); > + assertText("parametercheck", "No Parameters"); > + > + clickAndWait("link=Action Link With No Parameters"); > + assertText("parametercheck", "No Parameters"); > + > + clickAndWait("link=Event Link With No Parameters"); > + assertText("parametercheck", "No Parameters"); > + } > + > + //TAP5-1496 > + @Test > + public void links_with_explicit_empty_query_parameter_map() > + { > + openLinkParameterTest(); > + > + clickAndWait("link=Page Link With Explicitly Empty Parameters"); > + assertText("parametercheck", "No Parameters"); > + > + clickAndWait("link=Action Link With Explicitly Empty Parameters"); > + assertText("parametercheck", "No Parameters"); > + > + clickAndWait("link=Event Link With Explicitly Empty Parameters"); > + assertText("parametercheck", "No Parameters"); > + } > + > + //TAP5-1496 > + @Test > + public void links_with_nonempty_query_parameter_map() > + { > + openLinkParameterTest(); > + > + clickAndWait("link=Page Link With Parameters"); > + assertText("xpath=(//li[@class='qparam'])[1]", "param1: value1"); > + assertText("xpath=(//li[@class='qparam'])[2]", "param2: 10"); > + > + //re-open between checks to make sure there is no "bleedover" between checks. > + openLinkParameterTest(); > + clickAndWait("link=Action Link With Parameters"); > + assertText("xpath=(//li[@class='qparam'])[1]", "param1: value1"); > + assertText("xpath=(//li[@class='qparam'])[2]", "param2: 10"); > + > + openLinkParameterTest();; > + clickAndWait("link=Event Link With Parameters"); > + assertText("xpath=(//li[@class='qparam'])[1]", "param1: value1"); > + assertText("xpath=(//li[@class='qparam'])[2]", "param2: 10"); > + } > + > @Test > public void recursive_components_are_identified_as_errors() > { > > Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java > URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java?rev=1099864&r1=1099863&r2=1099864&view=diff > ============================================================================== > --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java (original) > +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java Thu May 5 16:31:50 2011 > @@ -1,4 +1,4 @@ > -// Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation > +// Copyright 2006, 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation > // > // Licensed under the Apache License, Version 2.0 (the "License"); > // you may not use this file except in compliance with the License. > @@ -467,7 +467,10 @@ public class Index > > new Item("ValidatorMacroDemo", "Validator Macro Demo", "Using validator macros"), > > - new Item("AtInjectDemo", "@javax.inject.Inject Demo", "Using @javax.inject.Inject for injection") > + new Item("AtInjectDemo", "@javax.inject.Inject Demo", "Using @javax.inject.Inject for injection"), > + > + new Item("LinkQueryParameters", "Link Query Parameters Demo", > + "Providing Query Parameters directly to link components as a map of key=parameter name, value=parameter values") > > ); > > > Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/LinkQueryParameters.java > URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/LinkQueryParameters.java?rev=1099864&view=auto > ============================================================================== > --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/LinkQueryParameters.java (added) > +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/LinkQueryParameters.java Thu May 5 16:31:50 2011 > @@ -0,0 +1,94 @@ > +// Copyright 2011 The Apache Software Foundation > +// > +// Licensed under the Apache License, Version 2.0 (the "License"); > +// you may not use this file except in compliance with the License. > +// You may obtain a copy of the License at > +// > +// http://www.apache.org/licenses/LICENSE-2.0 > +// > +// Unless required by applicable law or agreed to in writing, software > +// distributed under the License is distributed on an "AS IS" BASIS, > +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > +// See the License for the specific language governing permissions and > +// limitations under the License. > +package org.apache.tapestry5.integration.app1.pages; > + > +import org.apache.tapestry5.Link; > +import org.apache.tapestry5.annotations.Property; > +import org.apache.tapestry5.ioc.annotations.Inject; > +import org.apache.tapestry5.services.PageRenderLinkSource; > +import org.apache.tapestry5.services.Request; > + > +import java.util.Collections; > +import java.util.HashMap; > +import java.util.List; > +import java.util.Map; > + > +/** > + * Page for testing the query parameter component parameter on all of the framework-supplied link components > + * (page, action, event) > + */ > +public class LinkQueryParameters > +{ > + > + @Property > + private String paramName; > + > + @Inject > + private Request request; > + > + @Inject > + private PageRenderLinkSource linkSource; > + > + public Map<String,?> getEmptyParameters() > + { > + return Collections.emptyMap(); > + } > + > + public Map<String, ?> getNonEmptyParameters() > + { > + Map<String, Object> map = new HashMap<String, Object>(); > + map.put("param1","value1"); > + map.put("param2", 10); > + return map; > + } > + > + public Link onAction() > + { > + return buildLink(); > + } > + > + public Link onParameterCheck() > + { > + return buildLink(); > + } > + > + //small hack for test simplicity: we'll generate a new page link, add any parameters we find in the current > + //request, and redirect to that instead of the default. > + private Link buildLink() > + { > + Link l = linkSource.createPageRenderLink(LinkQueryParameters.class); > + for(String param : request.getParameterNames()) > + { > + l.addParameter(param,request.getParameter(param)); > + } > + > + return l; > + } > + > + public boolean isHasParameters() > + { > + return !request.getParameterNames().isEmpty(); > + } > + > + public List<String> getParameters() > + { > + return request.getParameterNames(); > + } > + > + public String getParamVal() > + { > + return request.getParameter(paramName); > + } > + > +} > >
