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
feeSubmit.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<head>
<title>Fee Submission Form</title>
<style type="text/css">@import url(css/main.css);</style>
<style>
.errorMessage {
color:green;
}
</style>
<body bgcolor="#A3A3FF">
<center>
<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"/>
<s:submit/>
</s:form>
</center>
</body>
feeReceipt.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Fee Submission Reciept</title> </head> <body bgcolor="#9E9EE8"> <center> <h2><u>Fee detail </u></h2> <table border="0" colspacing="5" colspading="15"> <tr> <td>Roll No - </td> <td><s:property value="rollNo"/></td> </tr> <tr> <td>Student Name - </td> <td><s:property value="name"/></td> </tr> <tr> <td>Fee Amount - </td> <td><s:property value="amount"/></td> </tr> <tr> <td>Submission Date - </td> <td><s:date name="submissionDate" format="MMM dd, yyyy"/></td> </tr> </table> </center> </body> </html>
Then write an action class
TypeConversionExample.java
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) {
this.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;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println(getSubmissionDate());
System.out.println(submissionDate);
return SUCCESS;
}
}
After writing an action class make a .properties file and pass the values as.
TypeConversionExample-conversion.properties
submissionDate=net.roseindia.converter.DateConverterClaz amount=net.roseindia.converter.CurrencyConverterClaz
You can make properties file for proper error message as. This properties file must be of same name as action class name
TypeConversionExample.properties
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
DateConverterClaz.java
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);
format.setLenient(false);
try {
String[] dateString = (String[]) object;
convertedDate = format.parse(dateString[0]);
System.out.println("Today "+convertedDate);
return convertedDate;
} catch (Exception e) {
e.toString();
throw new TypeConversionException("Given Date is Invalid");
}
}
return null;
}
}
Class for Currency Conversion
CurrencyConverterClaz.java
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) {
stringBuilder.append(convert);
} else {
stringBuilder.append(char2);
}
}
return stringBuilder.toString();
}
@Override
public Object convertValue(Map<String, Object> arg0, Object arg1,
Member arg2, String arg3, Object value, Class type) {
// TODO Auto-generated method stub
if(value==null){
return null;
}
else if(type==String.class){
NumberFormat newCurrency = new DecimalFormat("#,##0.00");
String formatedCurrency=newCurrency.format((Double) value);
System.out.println(formatedCurrency);
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
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="roseindia" namespace="/" extends="struts-default"> <action name="showForm"> <result>/jsp/feeSubmit.jsp</result> </action> <action name="viewReport" class="net.roseindia.TypeConversionExample"> <result name="input">/jsp/feeSubmit.jsp</result> <result name="success">/jsp/feeReceipt.jsp</result> </action> </package> </struts>
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
|
|