struts1 plugin

本文介绍了如何在Struts框架中通过struts.xml配置文件注册插件类,并在init方法中实现转化器的注册,以及在destory方法中移除转化器,具体展示了DoubleConverterPlugIn类的实现方式,包括转化器DoubleConverter的实现,用于在formbean中属性是double类型时自动调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
 }
}

转载于:https://www.cnblogs.com/rigid/p/3806414.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值