|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.velocity.tools.view.tools.LinkTool
View tool to make building URIs pleasant and fun! :)
This class is equipped to be used with a toolbox manager, for example the ServletToolboxManager included with VelocityViewServlet. This class implements interface ViewTool, which allows a toolbox manager to pass the required context information.
This class is not thread-safe by design. A new instance is needed for the processing of every template request. This means this tool should only be used in the request scope, not application or session scopes.
Nested Class Summary | |
protected class |
LinkTool.QueryPair
Internal util class to handle representation and encoding of key/value pairs in the query string |
Field Summary | |
private java.lang.String |
anchor
The anchor set for this link. |
protected javax.servlet.ServletContext |
application
A reference to the ServletContext |
private static java.lang.reflect.Method |
encode
Java 1.4 encode method to use instead of deprecated 1.3 version. |
static java.lang.String |
HTML_QUERY_DELIMITER
Standard HTML delimiter for query data ('&') |
private java.util.ArrayList |
queryData
A list of query string parameters. |
private java.lang.String |
queryDataDelim
The current delimiter for query data |
protected javax.servlet.http.HttpServletRequest |
request
A reference to the HttpServletRequest. |
protected javax.servlet.http.HttpServletResponse |
response
A reference to the HttpServletResponse. |
private java.lang.String |
uri
The URI reference set for this link. |
static java.lang.String |
XHTML_QUERY_DELIMITER
XHTML delimiter for query data ('&') |
Constructor Summary | |
LinkTool()
Default constructor. |
Method Summary | |
LinkTool |
addQueryData(java.lang.String key,
java.lang.Object value)
Adds a key=value pair to the query data. |
protected LinkTool |
copyWith(LinkTool.QueryPair pair)
For internal use. |
protected LinkTool |
copyWith(java.lang.String uri)
For internal use. |
protected LinkTool |
copyWithAnchor(java.lang.String anchor)
For internal use. |
private LinkTool |
duplicate()
This is just to avoid duplicating this code for both copyWith() methods |
static java.lang.String |
encodeURL(java.lang.String url)
Use the new URLEncoder.encode() method from java 1.4 if available, else use the old deprecated version. |
java.lang.String |
getBaseRef()
Returns the full URI of this template without any query data. |
java.lang.String |
getContextPath()
Returns the context path that addresses this web application, e.g. |
java.lang.String |
getContextURL()
Returns the URI that addresses this web application. |
java.lang.String |
getQueryData()
Returns this link's query data as a url-encoded string e.g. |
java.lang.String |
getURI()
Returns the current URI of this link as set by the setURI(String) or setRelative(String) methods. |
void |
init(java.lang.Object obj)
Initializes this tool. |
LinkTool |
setAnchor(java.lang.String anchor)
Returns a copy of the link with the specified anchor to be added to the end of the generated hyperlink. |
LinkTool |
setRelative(java.lang.String uri)
Returns a copy of the link with the specified context-relative URI reference converted to a server-relative URI reference. |
LinkTool |
setURI(java.lang.String uri)
Returns a copy of the link with the given URI reference set. |
protected void |
setXhtml(boolean useXhtml)
Controls the delimiter used for separating query data pairs. |
java.lang.String |
toString()
Returns the full URI reference that's been built with this tool, including the query string and anchor, e.g. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final java.lang.String HTML_QUERY_DELIMITER
public static final java.lang.String XHTML_QUERY_DELIMITER
protected javax.servlet.ServletContext application
protected javax.servlet.http.HttpServletRequest request
protected javax.servlet.http.HttpServletResponse response
private java.lang.String uri
private java.lang.String anchor
private java.util.ArrayList queryData
private java.lang.String queryDataDelim
private static java.lang.reflect.Method encode
Constructor Detail |
public LinkTool()
Method Detail |
protected void setXhtml(boolean useXhtml)
Controls the delimiter used for separating query data pairs. By default, the standard '&' character is used.
This is not exposed to templates as this decision is best not made at that level.
Subclasses may easily override the init() method to set this appropriately and then call super.init()
useXhtml
- if true, the XHTML query data delimiter ('&')
will be used. if false, then '&' will be used.protected LinkTool copyWith(LinkTool.QueryPair pair)
pair
- the query parameter to addprotected LinkTool copyWith(java.lang.String uri)
uri
- uri stringprotected LinkTool copyWithAnchor(java.lang.String anchor)
private LinkTool duplicate()
public void init(java.lang.Object obj)
init
in interface ViewTool
obj
- the current ViewContext
java.lang.IllegalArgumentException
- if the param is not a ViewContextpublic LinkTool setAnchor(java.lang.String anchor)
Returns a copy of the link with the specified anchor to be added to the end of the generated hyperlink.
Example:<a href='$link.setAnchor("foo")'>Foo</a>
<a href="#foo">Foo</a>
anchor
- an internal document reference
public LinkTool setRelative(java.lang.String uri)
Returns a copy of the link with the specified context-relative URI reference converted to a server-relative URI reference. This method will overwrite any previous URI reference settings but will copy the query string.
Example:<a href='$link.setRelative("/templates/login/index.vm")'>Login Page</a>
<a href="/myapp/templates/login/index.vm">Login Page</a>
uri
- A context-relative URI reference. A context-relative URI
is a URI that is relative to the root of this web application.
public LinkTool setURI(java.lang.String uri)
Returns a copy of the link with the given URI reference set. No conversions are applied to the given URI reference. The URI reference can be absolute, server-relative, relative and may contain query parameters. This method will overwrite any previous URI reference settings but will copy the query string.
uri
- URI reference to set
public java.lang.String getURI()
Returns the current URI of this link as set by the setURI(String) or setRelative(String) methods. Any conversions have been applied. The returned URI reference does not include query data that was added with method addQueryData().
public LinkTool addQueryData(java.lang.String key, java.lang.Object value)
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.
key
- key of new query parametervalue
- value of new query parameter
public java.lang.String getQueryData()
Returns this link's query data as a url-encoded string e.g.
key=value&foo=this+is+encoded
.
public java.lang.String getContextURL()
Returns the URI that addresses this web application. E.g.
http://myserver.net/myapp
. This string does not end
with a "/". Note! This will not represent any URI reference or
query data set for this LinkTool.
public java.lang.String getContextPath()
Returns the context path that addresses this web
application, e.g. /myapp
. This string starts
with a "/" but does not end with a "/" Note! This will not
represent any URI reference or query data set for this
LinkTool.
public java.lang.String getBaseRef()
http://myserver.net/myapp/stuff/View.vm
Note! The returned String will not represent any URI reference
or query data set for this LinkTool. A typical application of
this method is with the HTML base tag. For example:
<base href="$link.baseRef">
public java.lang.String toString()
http://myserver.net/myapp/stuff/View.vm?id=42&type=blue#foo
.
Typically, it is not necessary to call this method explicitely.
Velocity will call the toString() method automatically to obtain
a representable version of an object.
toString
in class java.lang.Object
public static java.lang.String encodeURL(java.lang.String url)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |