Struts 2 Format Examples

In this section you will learn how to format Date and numbers in Struts 2 Framework. Our Struts 2 Format Examples are very easy to grasp and you will learn these concepts in very small time.

Struts 2 Format Examples

Struts 2 Format Examples

     

In this section you will learn how to format Date and numbers in Struts 2 Framework. Our Struts 2 Format Examples are very easy to grasp and you will learn these concepts in very small time.

If you start developing any real world web application, at some point of time you will delve into the problem of displaying the date and number in some specific format. You can easily write your code and then format the output as string in your action class. But there is some drawback it like:

  • Your code are not aware of i18n. It will be very difficult to adapt to different locals.
  • Your action class are also clutter with unnecessary code.

Struts 2 in-built formatting functionality can save you from above problem. You can use s:text tag built-in formatting functionality to format your date and numbers.

Steps to Develop Example code

Write Action class

In the action class we are creating three fields:

private String productName;
private Double productCost;
private Date orderDate;

The values in these variables will be used for displaying on the jsp page. Here is the code of action class (OrderBean.java):

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


/**
 <p> Validate a user login. </p>
 */
public  class OrderBean  extends ActionSupport {

  private String productName;
  private Double productCost;

  public String execute() throws Exception {

  setOrderDate(new Date());
  setProductName("ICE Cream");
  setProductCost(102.08);
  return SUCCESS;

  }


  // ---- Order Date property ----

  /**
 <p>Field to store Today's Date.</p>
 <p/>
 */
  private Date orderDate;


  /**
 <p>Provide Today's Date.</p>
 *
 @return Returns the Todays date.
 */
  public Date getOrderDate() {
  return orderDate;
  }

  /**
 <p>Store new Date</p>
 *
 @param value The Order date to set.
 */
  public void setOrderDate(Date value) {
  orderDate = value;
  }

  public Double getProductCost() {
  return productCost;
  }

  public void setProductCost(Double productCost) {
  this.productCost = productCost;
  }

  public String getProductName() {
  return productName;
  }

  public void setProductName(String productName) {
  this.productName = productName;
  }

}

The execute method of action class populates Product Name, Product cost and Order date. 

  public String execute() throws Exception {

  setOrderDate(new Date());
  setProductName("ICE Cream");
  setProductCost(102.08);
  return SUCCESS;

     }

Writing JSP page

In the following jsp page (struts2format.jsp) you can view the actual usage of the tag.

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 Format Example!</title>

<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css"/>

</head>
<body>

<s:form action="Struts2Format" method="POST">

<tr>
<td align="right">Product Name:</td>
<td>
<b><s:text name="product.name">
<s:param name="value" value="productName"/>
</s:text>

</b>
</td>
</tr>

<tr>
<td align="right">Product cost:</td>
<td>
<s:text name="product.cost" >
<s:param name="value" value="productCost"/>
</s:text>

</td>
</tr>

<tr>
<td align="right">Order Date:</td>
<td>
<b><s:text name="product.orderDate">
<s:param name="value" value="orderDate"/>
</s:text>

</b>
</td>
</tr>

</s:form>

</body>

</html>

Please note that the formatting for product.name, product.cost and product.orderDate are defined in the package.properties message resource file.

Defining the formatting in properties file

In Struts 2 the formatting is defined in the message resource file (package.properties). The package.properties file is saved in the directory (package) where your action class resides. Here is the content of package.properties file:

product.cost= USD {0,number,##0.00}
product.orderDate = {0,date,dd-MMM-yyyy}
product.name = {0}

So, you can easily define or change the formatting of output just modifying the properties file.

Modification in struts.xml

Add the following line in struts.xml file:

<action name="Struts2Format" class="net.roseindia.OrderBean">
<result>/pages/struts2format.jsp</result>
</action>

To test the program, compile and package the code and then start tomcat. Type http://localhost:8080/struts2tutorial/roseindia/Struts2Format.action in the browser to view the result. Your browser should show the following output:

In this section you learnt how to use Struts 2 Format functionality.