Sunday, March 1, 2009

Using url and param tags in Struts 2

This blog post intends to answer the following questions : -
  1. How to make use of the url tag in Struts 2?
  2. How to invoke an action from a url link in Struts 2?
  3. How to pass parameters to an action from a url link in Struts 2?
  4. How to use the url tag to invoke actions in Struts 2?
The url tag in Struts 2 can come in very handy if you need to use a url in many places and dont want to repeat the code again and again. The url tag is simple and extremely easy to use. You need to specify an id attribute for your url and you can also use actions instead of simple links in the url. Adding parameters to the url is simple and is done by using the param tag.

...
...
<s:url id="someId" value="http://myurl.com"/>
<s:a href="%{someId}">Click Me</s:a>
...
...
You can also specify a relative url.


Actions can also be called instead of url's. This is done as follows.
...
...
<s:url id="someId" action="actionName"/>
<s:a href="%{someId}">Click Me</s:a>
...
...

Here is an example where you can pass parameters to the action by using the param tag in the url.
...
...
<s:url id="someId" action="actionName">
<s:param name="parameterName">parameterValue</s:param>
</s:url>
<s:a href="%{someId}">Click Me</s:a>
...
...

That's all there is to it folks!

Happy Programming ;)

Signing Off
Ryan

1 comment:

Anonymous said...

Hi rayan,
But How can I retrieve the parameter passed in the url in an action class??