Using Struts2 Tags (III)
18 January 2008This post is in continuation to Using Struts2 Tags (I & II).
First, in the head element, we use the url tag to inject a page reference into the HTML link tag.
<link href="<s:url value="/css/tutorial.css"/>" rel="stylesheet" type="text/css"/>
Note that the reference is absolute. We can move the page around without worrying about resolving relative references.
In the “Commands” section, we use the url link again, to inject a reference to an Action.
<li><a href="<s:url action="Register"/>">Register</a></li>
When the link is rendered, the tag will automatically append the appropriate extension, so that we do not need to embed that fact all over the application.
The tag will also URL-encode the link with the Java session ID, if needed, so that the Java session can be retained accross requests.
Finally, in the Languages section on the Hello page, we use the url tag along with the param and a tags to create a link with request parameters.
<s:url id="url" action="Start"> <s:param name="request_locale">en</s:param> </s:url> <s:a href="%{url}">English</s:a>
The param tag will add the parameter “request_locale=en” to the Start Action URL, and store it under the name “url”. The tag then adds the “url” reference into the hyperlink.
Creating Wildcard Mappings
Since the Start page is nothing but links, we don’t need an Action class. But, we should still add a mapping, so that we can use use an action URI. If we link only to actions, and never to pages, then it’s easy to add an Action class later.
<action name="Start" > <result>/tutorial/Start.jsp</result> </action>
As we create the application, we will often want to go directly to a page. To make prototyping easy, we can change the Start entry to a wilcard mapping.
Go through the other posts on this topic.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Using Struts2 Tags (III)