Tuesday 10 April 2012

Using PropertyEditorSupport

Your class can be extended to java.beans.PropertyEditorSupport class to convert the datatype of the variable to any other datatype or object. For example you have a user bean class defined as below.




User.java

public class User {
private double id;
private String fname;
private UserType userType;
public double getId() {
return id;
}
public void setId(double id) {
this.id = id;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public UserType getUserType() {
return userType;
}
public void setUserType(UserType userType) {
this.userType = userType;
}

}


As you can see, the user class have a field userType which is a object (related to another POJO class UserType) and in case if you want the user to enter the details from front end and enter it in the database it wont be possible. As the userType field may be in any other datatype other than UserType object. So you need to specify how to convert the data to userType. Please find the code for the UserType bean below.


UserType.java


public class UserType {
private double user_type_id;
private String user_Type;
public UserType(double user_type_id) {
super();
this.user_type_id = user_type_id;
}


public UserType() {
super();
}

public double getUser_type_id() {
return user_type_id;
}
public void setUser_type_id(double user_type_id) {
this.user_type_id = user_type_id;
}
public String getUser_Type() {
return user_Type;
}
public void setUser_Type(String user_Type) {
this.user_Type = user_Type;
}
}


Now the code which will specify how to convert the datatype to UserType object is done in UserTypeEditor.java

UserTypeEditor.java



public class UserTypeEditor extends java.beans.PropertyEditorSupport{

@Override
public void setAsText(String text)
    {
        double test=Double.valueOf(text);
                UserType userType=new UserType(test);
                setValue(userType);
    }
@Override
public String getAsText(){
UserType userType=(UserType)getValue();
return userType.toString();
}

}


In the above class you can see that the method setAsText(String text) is overridden in the UserTypeEditor class. this particular method specified how to change the String value to a object of type UserType. Below is the xml which specifies how to use the 
UserTypeEditor.java.

UserType.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>

<bean name="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="UserType">
<bean id="UserTypeEditor" class="UserTypeEditor">
</bean>
</entry>
</map>
</property>
</bean>

<bean id="reg" class="Registration">
<property name="user" ref="user" />
<property name="userType" ref="userType" />
</bean>
<bean id="user" class="User">
<property name="id" value="1" />
<property name="fname" value="Prajith" />
<property name="userType" value="1" />
</bean>
<bean id="userType" class="UserType">
<property name="user_type_id" value="1" />
<property name="user_Type" value="Admin" />
</bean>
 
</beans>


Registration.java

public class Registration {
private User user;
private UserType userType;
public void setUser(User user) {
System.out.println("registration setUser ");
this.user = user;
}
public void setUserType(UserType userType) {
System.out.println("registration setUserType ");
this.userType = userType;
}


public void printDetails(){
System.out.println("UserId" +user.getId());
System.out.println("Fname" +user.getFname());
System.out.println("UserTypeId" +userType.getUser_type_id());
System.out.println("UserTypeName" +userType.getUser_Type());
}
}


Main.java


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("UserType.xml");
    Registration reg=(Registration)context.getBean("reg");
     
    reg.printDetails();
}

}

2 comments:

  1. Harrah's Philadelphia Casino reopens with 676 slot machines
    A new casino 성남 출장마사지 in 태백 출장마사지 Chester 울산광역 출장안마 will soon close the doors of Harrah's Philadelphia for more than two weeks. The 이천 출장마사지 casino 익산 출장샵 will reopen the casino

    ReplyDelete