You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Matthew Van Horn <ma...@ashleyassociates.co.jp> on 2004/08/11 06:08:43 UTC
LinkTool patch - was: Re:AbstractPagerTool
> -----Original Message-----
> From: Nathan Bubna [mailto:nathan@esha.com]
> Sent: Wednesday, August 11, 2004 1:39 AM
> To: Velocity Users List
> Subject: Re: AbstractPagerTool
>
>
> Matthew Van Horn said:
> I am thinking there should be a
> updateQueryData(String name, String value) which would add the
> name/value but if it already existed, it would overwrite the old
> value.
sounds useful, but it would probably be better named
setQueryData(String,Object).
> Right now, I am using too much logic in my template to do this.
> (checking the name of each param in a loop, before calling
> addQueryData) It is not as maintainable as I'd like.
>
> What do you think of this idea? The way the QueryPairs are stored, I
> probably have to do the same loop w/ conditional over the ArrayList,
> but at least template designers don't have to hard code the parameter
> names to check for.
it's a good idea. wanna contribute a patch? (preferrably as diff -u)
Here is an example of how I use the new method:
#set($searchlink = $link.setAction('myAction'))
<!-- copy all the existing key=value pairs -->
#foreach($name in $request.parameterNames)
#set($searchlink = $searchlink.setQueryData($name,
$request.getParameter($name)))
#end
<!-- and no worries about duplicating pagesize and index anymore ->
#set($searchlink =
$searchlink.setQueryData("pagesize",$!pager.itemsPerPage))
#if( $pager.prevIndex )
<a
href="$searchlink.setQueryData('index',$!pager.prevIndex)">PREVIOUS</a>
#end
Here is the patch:
Index: LinkTool.java
===================================================================
RCS file:
/home/cvspublic/jakarta-velocity-tools/src/java/org/apache/velocity/tool
s/view/tools/LinkTool.java,v
retrieving revision 1.15
diff -u -r1.15 LinkTool.java
--- LinkTool.java 12 Mar 2004 20:30:32 -0000 1.15
+++ LinkTool.java 11 Aug 2004 03:20:41 -0000
@@ -20,6 +20,7 @@
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
@@ -29,7 +30,6 @@
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.view.context.ViewContext;
-import org.apache.velocity.tools.view.tools.ViewTool;
/**
* View tool to make building URIs pleasant and fun!
@@ -166,6 +166,40 @@
return copy;
}
+ /**
+ * For internal use.
+ *
+ * Copies 'that' LinkTool into this one and adds the new query
data, overwriting any existing parameter with the same name.
+ *
+ * @param pair the query parameter to add or update
+ */
+ protected LinkTool copyOver(QueryPair pair)
+ {
+ LinkTool copy = duplicate();
+ if (copy.queryData != null)
+ {
+ // set the copy's query data to a shallow clone
of
+ // the current query data array
+ copy.queryData =
(ArrayList)this.queryData.clone();
+ }
+ else
+ {
+ copy.queryData = new ArrayList();
+ }
+
+ for (Iterator iter = copy.queryData.iterator();
iter.hasNext();) {
+ QueryPair element = (QueryPair) iter.next();
+ if (element.key.equals(pair.key)) {
+ iter.remove();
+ }
+ }
+ //add new pair to this LinkTool's query data
+ copy.queryData.add(pair);
+ return copy;
+ }
+
+
+
/**
* For internal use.
@@ -366,6 +400,25 @@
{
return copyWith(new QueryPair(key, value));
}
+
+
+ /**
+ * <p>Adds a key=value pair to the query data. This returns a
new LinkTool
+ * containing both a copy of this LinkTool's query data and the
new data.
+ * Query data is URL encoded before it is appended.</p>
+ *
+ * <p>If the key already exists in the query data, the existing
value is overwritten,
+ * otherwise the behaviour is identical to {@link
#addQueryData}</p>
+ *
+ * @param key key of new query parameter
+ * @param value value of new query parameter
+ *
+ * @return a new instance of LinkTool
+ */
+ public LinkTool setQueryData(String key, Object value)
+ {
+ return copyOver(new QueryPair(key, value));
+ }
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org