Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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.
 
Search Tutorials:
 
Software Solutions and Services
 

 
Google Custom Search:
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation

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.

                         


 

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

how we can get focus on html field after validation in struts2
please tell me

Posted by Manohar Singh Shekhawat on Saturday, 09.20.08 @ 17:36pm | #80571

Is there any way to get a more global formatting set? As in for all floats, or all currencies, rather than on a action class basis?

Regards
Johan

Posted by Johan Snyman on Thursday, 05.15.08 @ 13:33pm | #60018

Hi,

This example is very helpful to me. I'd appreciate if you can provide a link to detail the syntax for properties for other types of fields, e.g., numbers.

Thanks,

Jane

Posted by Jane on Saturday, 09.8.07 @ 01:36am | #26349

Training Courses
Tell A Friend
Your Friend Name
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.