Struts 2 - Data Tags

The Struts 2 data tags are primarily used to manipulate the data displayed on a page. Listed below are the important data tags: <Start here>

The Action Tag

This tag enables developers to call actions directly from a JSP page by specifying the action name and an optional namespace. The body content of the tag is used to render the results from the Action. Any result processor defined for this action in struts.xml will be ignored, unless the executeResult parameter is specified.

<div>Tag to execute the action</div>
<br />
<s:action name = "actionTagAction" executeresult = "true" />
<br />
<div>To invokes special method  in action class</div>
<br />
<s:action name = "actionTagAction!specialMethod" executeresult = "true" />

Check Detailed Example

The Include Tag

These include will be used to include a JSP file in another JSP page.

<-- First Syntax -->
<s:include value = "myJsp.jsp" />

<-- Second Syntax -->
<s:include value = "myJsp.jsp">
   <s:param name = "param1" value = "value2" />
   <s:param name = "param2" value = "value2" />

<-- Third Syntax -->
<s:include value = "myJsp.jsp">
   <s:param name = "param1">value1</s:param>
   <s:param name = "param2">value2</s:param>

Check Detailed Example

The Bean Tag

These bean tag instantiates a class that conforms to the JavaBeans specification. This tag has a body which can contain a number of Param elements to set any mutator methods on that class. If the var attribute is set on the BeanTag, it will place the instantiated bean into the stack's Context.

<s:bean name = "org.apache.struts2.util.Counter" var = "counter">
   <s:param name = "first" value = "20"/>
   <s:param name = "last" value = "25" />

Check Detailed Example

The Date Tag

These date tag will allow you to format a Date in a quick and easy way. You can specify a custom format (eg. "dd/MM/yyyy hh:mm"), you can generate easy readable notations (like "in 2 hours, 14 minutes"), or you can just fall back on a predefined format with key '' in your properties file.

<s:date name = "person.birthday" format = "dd/MM/yyyy" />
<s:date name = "person.birthday" format = "%{getText('some.i18n.key')}" />
<s:date name = "person.birthday" nice="true" />
<s:date name = "person.birthday" />

Check Detailed Example

The Param Tag

These param tag can be used to parameterize other tags. This tag has the following two parameters.

  • name (String) − the name of the parameter

  • value (Object) − the value of the parameter

      <ui:param name = "key"     value = "[0]"/>
      <ui:param name = "value"   value = "[1]"/>
      <ui:param name = "context" value = "[2]"/>

Check Detailed Example

The Property Tag

These property tag is used to get the property of a value, which will default to the top of the stack if none is specified.

<s:push value = "myBean">
   <!-- Example 1: -->
   <s:property value = "myBeanProperty" />

   <!-- Example 2: -->TextUtils
   <s:property value = "myBeanProperty" default = "a default value" />

Check Detailed Example

The Push Tag

These push tag is used to push value on stack for simplified usage.

<s:push value = "user">
   <s:propery value = "firstName" />
   <s:propery value = "lastName" />

Check Detailed Example

The Set Tag

These set tag assigns a value to a variable in a specified scope. It is useful when you wish to assign a variable to a complex expression and then simply reference that variable each time rather than the complex expression. The scopes available are application, session, request, page and action.

<s:set name = "myenv" value = ""/>
<s:property value = "myenv"/>

Check Detailed Example

The Text Tag

These text tag is used to render a I18n text message.

<!-- First Example -->
<s:i18n name = "struts.action.test.i18n.Shop">
   <s:text name = "main.title"/>

<!-- Second Example -->
<s:text name = "main.title" />

<!-- Third Examlpe -->
<s:text name = "i18n.label.greetings">
   <s:param >Mr Smith</s:param>

Check Detailed Example

The URL Tag

These url tag is used to create a URL.

<-- Example 1 -->
<s:url value = "editGadget.action">
   <s:param name = "id" value = "%{selected}" />

<-- Example 2 -->
<s:url action = "editGadget">
   <s:param name = "id" value = "%{selected}" />

<-- Example 3-->
<s:url includeParams="get">
   <s:param name = "id" value = "%{'22'}" />

Check Detailed Example