Type conversion example
Posted on: January 27, 2011 at 12:00 AM
In this tutorial you will learn how to pass the request parameter value in the type conversion class

Type Conversion Example

An example of type conversion is given below, which converts the date and currency of given request parameters into object.

In the given example a user enters the student fee data into the form and clicks on Submit button, Then the converted value display on the browser.

At first make JSP


<%@ taglib prefix="s" uri="/struts-tags" %>
<title>Fee Submission Form</title>
<style type="text/css">@import url(css/main.css);</style>
.errorMessage {
<body bgcolor="#A3A3FF">
<h2>Enter Fee Detail </h2>
<s:form action="viewReport">
<s:textfield name="rollNo" label="Roll No"/>
<s:textfield name="name" label="Student Name"/>
<s:textfield name="submissionDate" label="Submission Date"/>
<s:textfield name="amount" label="Fee Amount"/>


<%@ taglib prefix="s" uri="/struts-tags" %>
<title>Fee Submission Reciept</title>
<body bgcolor="#9E9EE8">
<h2><u>Fee detail </u></h2>

<table border="0" colspacing="5" colspading="15">
<td>Roll No - </td>
<td><s:property value="rollNo"/></td>
<td>Student Name - </td>
<td><s:property value="name"/></td>
<td>Fee Amount - </td>
<td><s:property value="amount"/></td>
<td>Submission Date - </td>
<td><s:date name="submissionDate" format="MMM dd, yyyy"/></td>

Then write an action class

package net.roseindia;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class TypeConversionExample extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private String name;
	private int rollNo;
	private double amount;
	private Date submissionDate;
	private String datePattern="yyyy-mm-dd";
	public String getDatePattern() {
		return datePattern;
	public void setDatePattern(String datePattern) {
		this.datePattern = datePattern;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public int getRollNo() {
		return rollNo;
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	public double getAmount() {
		return amount;
	public void setAmount(double amount) {
		this.amount = amount;
	public Date getSubmissionDate() {
		return submissionDate;
	public void setSubmissionDate(Date submissionDate) {
		this.submissionDate = submissionDate;
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;

After writing an action class make a .properties file and pass the values as.


You can make properties file for proper error message as. This properties file must be of same name as action class name

invalid.fieldvalue.amount=Please enter the correct amount
invalid.fieldvalue.submissionDate=Please enter a date in yyyy-mm-dd format

Now make a type conversion class.

The class given below is for Date Conversion

package net.roseindia.converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import ognl.DefaultTypeConverter;

import com.opensymphony.xwork2.conversion.TypeConversionException;

public class DateConverterClaz extends DefaultTypeConverter {

	private Date convertedDate;
	 public Date convertValue(Map context, Object object, Class type) {
	    	System.out.println("Inside Date Converter");
	        if (type == Date.class) {
	        	System.out.println("inside If Block");
	            String datePattern = "yyyy-MM-dd";
	            DateFormat format = new SimpleDateFormat(datePattern);
	            try {
	                String[] dateString = (String[]) object;
	                convertedDate = format.parse(dateString[0]);
	                System.out.println("Today "+convertedDate);
	                return convertedDate;
	            } catch (Exception e) {
	                throw new TypeConversionException("Given Date is Invalid");
        return null;


Class for Currency Conversion

package net.roseindia.converter;

import java.lang.reflect.Member;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Map;

import com.opensymphony.xwork2.conversion.TypeConverter;
public class CurrencyConverterClaz implements TypeConverter{
	StringBuilder stringBuilder;
	public String replace(String string, char character, String convert) {
		if (string == null) {
			return null;
		int length = string.length();
		stringBuilder= new StringBuilder(string.length() * 2);
		for (int i = 0; i < length; i++) {
			char char2 = string.charAt(i);
			if (char2 == character) {
			} else {
		return stringBuilder.toString();

	public Object convertValue(Map<String, Object> arg0, Object arg1,
			Member arg2, String arg3, Object value, Class type) {
		// TODO Auto-generated method stub
		return null;
		else if(type==String.class){
			NumberFormat newCurrency = new DecimalFormat("#,##0.00");
			String formatedCurrency=newCurrency.format((Double) value);
			return formatedCurrency;
		else if ( type == Double.TYPE  || type == Double.class) {
			String[] strings = (String[]) value;
			String doubleValue = strings[0];
			return Double.parseDouble(replace(doubleValue, ',', ""));
		return null;

Finally comes the job of mapping


<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"


<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="roseindia" namespace="/" extends="struts-default">

<action name="showForm">

<action name="viewReport" class="net.roseindia.TypeConversionExample">
<result name="input">/jsp/feeSubmit.jsp</result>
<result name="success">/jsp/feeReceipt.jsp</result>


Now compile the code start your tomcat open the browser and type the URL http://localhost:8080/typeConversion/showForm.action

The output appears as given below

When you run this application it will display message as shown below:


Download this example code

Related Tags for Type conversion example: