Struts2 - Include Tag (Data 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.
Add the following code snippet into the struts.xml file.


struts.xml

<action name="includeTag" class="net.roseindia.includeTag">
   <result name="success">/pages/genericTags/includeTag.jsp</result>
</action>

Create an action class as shown below:
includeTag.java

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;

public class includeTag extends ActionSupport {
  private Date myBirthday;
  public String execute() throws Exception{
  setMyBirthday(new Date("Jan 12, 1984 11:21:30 AM"));
  return SUCCESS;
  }
  
  public Date getMyBirthday(){
  return myBirthday;
  }
}

Create a simple jsp (myBirthday.jsp) that we want to include in our main jsp page ie..includeTag.jsp.

myBirthday.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>



<html>

  <head>

  <title>Include Tag (Data Tag) Example!</title>

  </head>

  <body>

  <b><font color="#000080">My Birth Day (Date Format)</font></b>

  <table border="1" width="35%" bgcolor="ffffcc">

  <tr>

  <td width="50%"><b><font color="#000080">Date Format</font></b></td>

  <td width="50%"><b><font color="#000080">Date</font></b></td>

  </tr>

  <tr>

  <td width="50%">Day/Month/Year</td>

  <td width="50%"><s:date name="myBirthday" format="dd/MM/yyyy" /></td>

  </tr>

  <tr>

  <td width="50%">Month/Day/Year</td>

  <td width="50%"><s:date name="myBirthday" format="MM/dd/yyyy" /></td>

  </tr>

  <tr>

  <td width="50%">Month/Day/Year</td>

  <td width="50%"><s:date name="myBirthday" format="MM/dd/yy" /></td>

  </tr>

  <tr>

  <td width="50%">Month/Day/Year Hour<B>:</B>Minute</td>

  <td width="50%"><s:date name="myBirthday" format="MM/dd/yy hh:mm" /></td>

  </tr>

  <tr>

  <td width="50%">Month/Day/Year Hour<B>:</B>Minute<B>:</B>Second</td>

  <td width="50%"><s:date name="myBirthday" format="MM/dd/yy hh:mm:ss" /></td>

  </tr>

  <tr>

  <td width="50%">Nice Date (Current Date & Time)</td>

  <td width="50%"><s:date name="myBirthday" nice="false" /></td>

  </tr>

  </table>

  

  </body>

</html>


Now create a jsp page using tag as shown in the includeTag.jsp page. The tag includes another jsp using the value parameter
includeTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>
  <title>Include Tag Example!</title>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Include Tag (Data Tags) 
   Example!</span></h1>
  <s:include value="myBirthday.jsp" />
  </body>
</html>






Output of the includeTag.jsp :

Struts2 - Date Tag (Data Tag)

In this section, we are going to describe the Date tag. The date tag allows to format a Date in a quick and easy way. User can specify a custom format (eg. "dd/MM/yyyy hh:mm"), can generate easy readable notations (like "in 2 hours, 14 minutes"), or can just fall back on a predefined format with key 'struts.date.format' in the properties file.
If that key is not defined, it will finally fall back to the default DateFormat.MEDIUM formatting.

Note: If the requested Date object isn't found on the stack, a blank will be returned.

Configurable attributes are :-
1. name
2. nice
3. format

Add the following code snippet into the "struts.xml" file.

struts.xml

<action name="dateTag" class="net.roseindia.dateTag">
   <result name="success">/pages/genericTags/dateTag.jsp</result>
</action>

Create an action class as shown below:

dateTag.java

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;

public class dateTag extends ActionSupport {
  private Date currentDate;
  public String execute() throws Exception{
  setCurrentDate(new Date());
  return SUCCESS;
  }
  public void setCurrentDate(Date date){
  this.currentDate = date;
  }
  public Date getCurrentDate(){
  return currentDate;
  }
}


Now create a jsp page using tag as shown in the success.jsp page.
The date tag formats a Date in a quick and easy way. Here the "format" parameter specify a custom format (eg. "dd/MM/yyyy hh:mm") to follow.
The nice parameter is of Boolean type which decides whether to print out the date nicely
or not. By Default it is kept false which prints out date nicely i.e.
tag formats a date and similarly does not format a date, it is illustrated in our current jsp page.





dateTag.jsp


<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>
  <title>Date Tag (Data Tag) Example!</title>
  </head>
  <body>
  <h1><font color="#000080">Current Date Format</font></h1>
  <table border="1" width="35%" bgcolor="ffffcc">
  <tr>
  <td width="50%"><b><font color="#000080">Date Format</font></b></td>
  <td width="50%"><b><font color="#000080">Date</font></b></td>
  </tr>
  <tr>
  <td width="50%">Day/Month/Year</td>
  <td width="50%"><s:date name="currentDate" format="dd/MM/yyyy" /></td>
  </tr>
  <tr>
  <td width="50%">Month/Day/Year</td>
  <td width="50%"><s:date name="currentDate" format="MM/dd/yyyy" /></td>
  </tr>
  <tr>
  <td width="50%">Month/Day/Year</td>
  <td width="50%"><s:date name="currentDate" format="MM/dd/yy" /></td>
  </tr>
  <tr>
  <td width="50%">Month/Day/Year Hour<B>:</B>Minute</td>
  <td width="50%"><s:date name="currentDate" format="MM/dd/yy hh:mm" /></td>
  </tr>
  <tr>
  <td width="50%">Month/Day/Year Hour<B>:</B>Minute<B>:</B>Second</td>
  <td width="50%"><s:date name="currentDate" format="MM/dd/yy hh:mm:ss" /></td>
  </tr>
  <tr>
  <td width="50%">Nice Date (Current Date & Time)</td>
  <td width="50%"><s:date name="currentDate" nice="false" /></td>
  </tr>
  <tr>
  <td width="50%">Nice Date</td>
  <td width="50%"><s:date name="currentDate" nice="true" /></td>
  </tr>
  </table>
  
  </body>
</html>


Output of the dateTag.jsp :

Struts2 - Bean Tag (Data Tag)

In this section, we are going to describe the Bean Tag. The Bean tag is a generic tag that is used to instantiates a class that confirms 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 id attribute is set on the BeanTag, it will place the instantiated bean into the stack's Context.
Add the following code snippet into the struts.xml file.


struts.xml

<action name="beanTag" class="struts2.actionTag">
<result name="success">/pages/genericTags/bean.jsp</result> 
</action>

Create an action class as shown below:

beanTag.java

package net.struts2; 
import com.opensymphony.xwork2.ActionSupport;
public class beanTag extends ActionSupport { 
  public String execute() throws Exception{
    return SUCCESS;
  }
}

create a simple java bean as shown:


companyName.java

package net.struts2;

public class companyName {
  
  private String name;

  public void setName(String name){
  this.name =name ;
  }

  public String getName(){
  return name;
  }
}

Now create a jsp page using and tags as shown in the beanTag.jsp page. The bean tag instantiates the "net.struts2.companyName" class, it confirms to the JavaBeans specification. The id attribute is set on the BeanTag, it places the instantiated bean into the stack's Context. The body of tag contains a param element
Struts2 which is used to set the value for the setName() method of the "companyName" class and retrieves that value by calling the getName() method.


beanTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>
  <title>Bean Tag Example!</title>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Bean Tag 
   (Data Tags) Example!</span></h1>
  <s:bean name="net.struts2.companyName" id="uid">
  <s:param name="name">struts2</s:param> 
  <s:property value="%{name}" /><br>
  </s:bean>
  </body>
</html>

Struts2 - Action Tag (Data Tag)

In this section, we are going to describe the action tag. The action tag is a generic tag that is used 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.

Add the following code snippet into the struts.xml file.

struts.xml

<action name="actionTag" class="struts.actionTag">
<result name="success">/pages/genericTags/success.jsp</result> 
</action>

Create an action class as shown below:

actionTag.java

package net.roseindia; 
import com.opensymphony.xwork2.ActionSupport;
public class actionTag extends ActionSupport { 
   public String execute() throws Exception{
    return SUCCESS;
   }
}

Now create a jsp page using tag as shown in the success.jsp page. The action tag is used to call actions directly from a JSP page by specifying the action name and an optional namespace.

success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html> 
<head>
<title>Action Tag Example!</title> </head>
<body> 
<h1><span style="background-color: #FFFFcc">Action Tag
(Data Tags) Example!</span></h1>
<s:action name="success"> 
<b><i>The action tag will execute the result and include
it in this page.</i></b></div>
</s:action>
</body> 
</html>


Output of the success.jsp