struts1 plugin
struts plugin 在struts.xml中注册之后,在系统启动之后调用 init 方法,通常在init方法中进行转化器的注册,在destory中移除转化器
1. struts文件
<plug-in className="com.test.util.DoubleConverterPlugIn"></plug-in>
2.插件类实现
public class DoubleConverterPlugIn implements PlugIn{
public DoubleConverterPlugIn() {
}
public void destroy() {
// 把注册移除
ConvertUtils.deregister();
}
public void init(ActionServlet arg0, ModuleConfig arg1)throws ServletException {
ConvertUtils.register(new DoubleConverter(), Double.class);
}
}
3. Double转化器的实现,此转化器在form bean中属性是double类型赋值时候自动调用
public class DoubleConverter implements Converter{
public Object convert(Class type, Object value) {
Double doubleValue = new Double(0);
if(value != null && !"".equals(value)){
if(value instanceof String){
String str = (String) value;
str = str.replaceAll(",", "");
try{
doubleValue = new Double(str);
}catch (Exception e) {
e.printStackTrace()
}
}
}
return doubleValue;
}
}