<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):

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>

The links in generated HTML 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:

 <a href="/myapp/userpage.page">Goto homepage (1)</a>
 <a href="/myapp/userpage.page">Goto homepage (2)</a>
 <a href="/myapp/userpage.page">Goto homepage (3)</a>
 <a href="/myapp/userpage.page">Goto homepage (4)</a>
 <a href="/myapp/userpage.page">Goto homepage (5)</a>
 <a href="/myapp/userpage.page">Goto homepage (6)</a>

When browser resolves the links it prepends them with server name and port number to build full address.

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>

The value of the links in generated HTML is unchanged:

 <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>

Browser resolves these links using <base> location as follows (shown is the full address that can be seen in the browser status bar when hovering over a link):

 <a href="userpage.jsp">Goto homepage (1)</a> <== http://mysite:8080/myapp/pages/userpage.jsp">
 <a href="/userpage.jsp">Goto homepage (2)</a> <== http://mysite:8080/userpage.jsp
 <a href="userpage">Goto homepage (3)</a> <== http://mysite:8080/myapp/pages/userpage
 <a href="/userpage">Goto homepage (4)</a> <== http://mysite:8080/userpage
 <a href="userpage.do">Goto homepage (5)</a> <== http://mysite:8080/myapp/pages/userpage.do
 <a href="/userpage.do">Goto homepage (6)</a> <== http://mysite:8080/userpage.do

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 server name and port number:

 <a href="/myappuserpage.jsp">Goto homepage (1)</a> <== http://mysite:8080/myappuserpage.jsp
 <a href="/myapp/userpage.jsp">Goto homepage (2)</a> <== http://mysite:8080/myapp/userpage.jsp
 <a href="/myappuserpage">Goto homepage (3)</a> <== http://mysite:8080/myappuserpage
 <a href="/myapp/userpage">Goto homepage (4)</a> <== http://mysite:8080/myapp/userpage
 <a href="/myappuserpage.do">Goto homepage (5)</a> <== http://mysite:8080/myappuserpage.do
 <a href="/myapp/userpage.do">Goto homepage (6)</a> <== http://mysite:8080/myapp/userpage.do

This is probably not the result that you wanted to achieve.

StrutsTaglibHtmlLink (last edited 2009-09-20 23:11:51 by localhost)