Home Struts Struts2 Struts 2 Format Examples

Related Tutorials


 
 

Share on Google+Share on Google+

Struts 2 Format Examples

Advertisement
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

     

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.

     


 
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 14, 2008

Related Tutorials

Discuss: Struts 2 Format Examples   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
vrutika
May 25, 2011
date format example

what will b the code in index.jsp?? how will d struts2Format.jsp will b mapped??
Yogi
June 14, 2012
taOpSdqtYMHGyE

JCaptcha4Struts2 is a Struts 2 Plugin which allows to add Captcha suroppt to your Struts 2 Applications with a single JSP tag, and slight configuration in your struts.xml. It uses the powerful JCaptcha engine internally to generate the captcha.
DMCA.com