You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Apache Wiki <wi...@apache.org> on 2005/11/24 03:41:39 UTC

[Struts Wiki] Update of "StrutsTaglibHtmlLink" by MichaelJouravlev

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.

The following page has been changed by MichaelJouravlev:
http://wiki.apache.org/struts/StrutsTaglibHtmlLink

New page:
== <html:link> ==

Renders an HTML <a> element as an anchor definition (if "linkName" is specified) or as a hyperlink to the specified URL. URL rewriting will be applied automatically, to maintain session state in the absence of cookies. The content displayed for this hyperlink will be taken from the body of this tag.

The base URL for this hyperlink is calculated based on which of the following attributes you specify (you must specify exactly one of them):

 * forward - Use the value of this attribute as the name of a global ActionForward to be looked up, and use the module-relative or context-relative URI found there. If the forward is module-relative then it must point to an action and NOT to a page.
 * action - Use the value of this attribute as the name of a Action to be looked up, and use the module-relative or context-relative URI found there.
 * href - Use the value of this attribute unchanged.
 * page - Use the value of this attribute as a module-relative URI, and generate a server-relative URI by including the context path and module prefix.

==== Examples ====

Example:
web.xml defines standard Struts mapping using "*.do" extension:
{{{
 <servlet-mapping>
   <servlet-name>action</servlet-name>
   <url-pattern>*.page</url-pattern>
 </servlet-mapping>
}}}
Browser navigates to the action: "http://mysite:8080/myapp/Home.page"
Action forwards to the view: "/pages/home.jsp"
Page home.jsp has <html:base/> tag defined in the header. See StrutsTaglibHtmlBase.

==== Example 1: Using "action" attribute ====

Consider the following links defined in home.jsp page:
{{{
 <html:link action="userpage.jsp">Goto homepage (1)</html:link>
 <html:link action="/userpage.jsp">Goto homepage (2)</html:link>
 <html:link action="userpage">Goto homepage (3)</html:link>
 <html:link action="/userpage">Goto homepage (4)</html:link>
 <html:link action="userpage.do">Goto homepage (5)</html:link>
 <html:link action="/userpage.do">Goto homepage (6)</html:link>
}}}
Generated HTML would look like this:
{{{
 <a href="/mysite/userpage.page">Goto homepage (1)</a>
 <a href="/mysite/userpage.page">Goto homepage (2)</a>
 <a href="/mysite/userpage.page">Goto homepage (3)</a>
 <a href="/mysite/userpage.page">Goto homepage (4)</a>
 <a href="/mysite/userpage.page">Goto homepage (5)</a>
 <a href="/mysite/userpage.page">Goto homepage (6)</a>
}}}
All links are normalized using application context name. Extensions are stripped. Because web.xml defines extension mapping, default extension is added to action names in all links. 

When browser resolves the links it prepends them with "http://mysite:8080".

==== Example 2: Using "href" attribute ====

Consider the following links defined in home.jsp page:
{{{
 <html:link href="userpage.jsp">Goto homepage (1)</html:link>
 <html:link href="/userpage.jsp">Goto homepage (2)</html:link>
 <html:link href="userpage">Goto homepage (3)</html:link>
 <html:link href="/userpage">Goto homepage (4)</html:link>
 <html:link href="userpage.do">Goto homepage (5)</html:link>
 <html:link href="/userpage.do">Goto homepage (6)</html:link>
}}}
Generated HTML would look like this:
{{{
 <a href="userpage.jsp">Goto homepage (1)</a>
 <a href="/userpage.jsp">Goto homepage (2)</a>
 <a href="userpage">Goto homepage (3)</a>
 <a href="/userpage">Goto homepage (4)</a>
 <a href="userpage.do">Goto homepage (5)</a>
 <a href="/userpage.do">Goto homepage (6)</a>
}}}
The value of the links is unchanged, the leading slash is not added and extensions are unmodified. Browser resolves these links using <base> locaiton as follows (shown is the full address that can be seen in the browser status bar when hovering over a link):
{{{
 <a href="http://mysite:8080/myapp/pages/userpage.jsp">Goto homepage (1)</a>
 <a href="http://mysite:8080/userpage.jsp">Goto homepage (2)</a>
 <a href="http://mysite:8080/myapp/pages/userpage">Goto homepage (3)</a>
 <a href="http://mysite:8080/userpage">Goto homepage (4)</a>
 <a href="http://mysite:8080/myapp/pages/userpage.do">Goto homepage (5)</a>
 <a href="http://mysite:8080/userpage.do">Goto homepage (6)</a>
}}}
If you use "href" attribute for relative addressing, strip leading slash. Otherwise browser will resolve the link as server-absolute location.
==== Example 3: Using "page" attribute ====

Consider the following links defined in home.jsp page:
{{{
 <html:link page="userpage.jsp">Goto homepage (1)</html:link>
 <html:link page="/userpage.jsp">Goto homepage (2)</html:link>
 <html:link page="userpage">Goto homepage (3)</html:link>
 <html:link page="/userpage">Goto homepage (4)</html:link>
 <html:link page="userpage.do">Goto homepage (5)</html:link>
 <html:link page="/userpage.do">Goto homepage (6)</html:link>
}}}
<html:link> tag will regard the link as relative regardless of leading slash, and will build server-relative address by concatenating context path and the link. Generated HTML would look like this:
{{{
 <a href="/myappuserpage.jsp">Goto homepage (1)</a>
 <a href="/myapp/userpage.jsp">Goto homepage (2)</a>
 <a href="/myappuserpage">Goto homepage (3)</a>
 <a href="/myapp/userpage">Goto homepage (4)</a>
 <a href="/myappuserpage.do">Goto homepage (5)</a>
 <a href="/myapp/userpage.do">Goto homepage (6)</a>
}}}
When browser resolves the links it prepends them with "http://mysite:8080".
{{{
 <a href="http://mysite:8080/myappuserpage.jsp">Goto homepage (1)</a>
 <a href="http://mysite:8080/myapp/userpage.jsp">Goto homepage (2)</a>
 <a href="http://mysite:8080/myappuserpage">Goto homepage (3)</a>
 <a href="http://mysite:8080/myapp/userpage">Goto homepage (4)</a>
 <a href="http://mysite:8080/myappuserpage.do">Goto homepage (5)</a>
 <a href="http://mysite:8080/myapp/userpage.do">Goto homepage (6)</a>
}}}
This is probably not the result that you wanted to achieve.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org