ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rick Reumann <rick.reum...@gmail.com>
Subject Re: re: struts vs ibatis - Integer type
Date Mon, 09 May 2005 14:19:11 GMT
I haven't tested this Integer Converter but this should work....

//CustomStringToIntegerConverter.java

import org.apache.commons.beanutils.Converter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CustomStringToIntegerConverter implements Converter {

public Object convert(Class type, Object value) {
Integer theInteger = null;
if (value != null && !value.toString().trim().equals("") && 
(value instanceof String && type == java.lang.Integer.class) ) {
theInteger = Integer.valueOf( value );
}
}
return theInteger;
}
}

Then to implement, have a servlet listener that runs on start up and 
registers this converter and you are done:

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class YourServletContextListener implements ServletContextListener {

public void contextDestroyed(ServletContextEvent contextEvent) {
//might want to remove stuff when context destroyed
}

public void contextInitialized(ServletContextEvent contextEvent) {
ServletContext context = contextEvent.getServletContext();
try {
ConvertUtils.register(new CustomStringToIntegerConverter(), Integer.class);

} catch (Exception ex) {
//log errors
}
}
}

In web.xml before servlet definitions:

<listener>
<listener-class>com.whatever.YourServletContextListener</listener-class>
</listener>
-- 
Rick

Mime
View raw message