Friday, 15 June 2012

Custom Converter in Struts 2.3 and utilizing using annotations


In Struts we can write our own custom converter by extending to the StrutsTypeConverter and overriding the methods of the parent class. In the below example i have converted the string value received from the jsp to a Role object using the Struts annotation and the custom converter. I have not kept the Role class over here as you can convert this string to any object as per your requirement. Please don't forget to create a constructor in the POJO class as per the requirement other wise this converter wont work.

I have not overridden the convertToString method of the StrutsTypeConverter. This only because I am not using this conversion. It depends on the developer to override the methods according to the application need.


package com.lntinfotech.office.common;

import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lntinfotech.office.domain.Role;
import com.opensymphony.xwork2.conversion.TypeConversionException;

public class RoleConverter extends StrutsTypeConverter {
private Logger logger=LoggerFactory.getLogger(getClass());
@Override
public Role convertFromString(Map context, String[] values, Class toClass){
logger.debug("--> convertFromString String :"+values[0]);
Role role;
if (values != null && values.length > 0 && values[0] != null
&& values[0].length() > 0) {
try {
int id=Integer.valueOf(values[0]);
role=new Role(id);

} catch (Exception e) {
throw new TypeConversionException(e);
}
logger.debug("<-- convertFromString String :"+values[0]);
return role;
}
logger.debug("<-- convertFromString null");
return null;
}

@Override
public String convertToString(Map arg0, Object arg1) {
logger.debug("--> convertToString(Map arg0, Object arg1)");
// TODO Auto-generated method stub
return null;
}

}

In the above class we have created the a converter. So to use this we can use the Struts annotation. In the action class where you need this conversion to happen just annotate as 
@TypeConversion and give the complete path of the converter. I have removed other variables and method as its not necessary for this example. At run time when we are getting the dynamic value from the frontend the below code will call the converter and convert the value to the Role object and process the form as per the code. 

package com.lntinfotech.office.controller;

public class RegistrationAction extends ActionSupport {
private static final long serialVersionUID = 1L;

private Logger logger = LoggerFactory.getLogger(getClass());
        private Role role;

        public Role getRole() {
return role;
}

@TypeConversion(converter = "com.lntinfotech.office.common.RoleConverter")
public void setRole(Role role) {
this.role = role;
}

        @Action(value = "/createuser", results = {
@Result(location = "registrationSuccess.jsp", name = "success"),
@Result(location = "registration.jsp", name = "input"),
@Result(location = "registration.jsp", name = "error") }, className = "registerUser")
public String createUser() {
logger.debug("--> createUser()");
        //method code removed.
        return "success";
}

}

No comments:

Post a Comment