Struts 2 Data Tags

In this section, we will discuss about the data tags (generic tags) provided with struts 2 framework .
Struts 2 Data tags, help to get the data from the ValueStack, or place the data into the ValusStack. The Struts 2 data tags are primarily used to manipulate the data displayed on a page.

a tag example

Struts 2 “a” tag is used to render a HTML “<a>” tag. The best practice is always use “<s:url>” tag to create the URL and embed it into the “a” tag.

<s:url value="https://www.dineshonjava.com" var="dojURL" />
<s:a href="%{dojURL}">Dinesh On Java</s:a>

Click here for detailed Example.

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

Click here for detailed Example.

The include tag:

In this section, we are going to describe the include tag. The include tag is a generic tag that is used to include a servlet’s output (result of servlet or a JSP page) to the current page.

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

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

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

Click here for detailed Example.

The Param Tag:

In this section, we are going to describe the param tag. The param tag is a generic tag that is used to parametrize other tags.
These param tag can be used to parametrize other tags. This tag has the following two parameters.

  • name (String) – the name of the parameter
  • value (Object) – the value of the parameter
<ui:component>
  <ui:param name="username">Dinesh</ui:param><br>
  <ui:param name="username">Anamika</ui:param><br>
  <ui:param name="username">Sweety</ui:param> 
  </ui:component>

Click here for detailed Example.

The Set Tag:

In this section, we are going to describe the Set tag . The set tag is a generic tag that is used to assign a value to a variable in a specified scope. 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.

<h1><span style="background-color: #FFFFcc">Set Tag 
   (Data Tags) Example!</span></h1>
  <s:set name="technologyName" value="%{'Java'}"/>
  Technology Name: <s:property value="#technologyName"/>

Click here for 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" />
</s:bean>

Click here for 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:ss”), you can generate easy readable notations (like “in 3 hours, 14 minutes, 23 seconds“), or you can just fall back on a predefined format with key ‘struts.date.format‘ in your properties file.

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

Click here for 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" />
</s:push>

Click here for 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" />
</s:push>

Click here for 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"/>
</s:i18n>
<s:text name="main.title" />

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

Click here for 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}" />
</s:url>

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

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

Click here for detailed Example.

<<Previous <<   || Index ||   >>Next >>
Previous
Next