struts转换器(转)

网上看到的,转过来,不过看到最后一行,不禁要

<li>测试ActionForm类型的自动转换</li><br>
 <form action="typeconvert.do" method="post">
  intValue:<input type="text" name="intValue"/><br>
  doubleValue:<input type="text" name="doubleValue"><br>
  booleanValue:<input type="text" name="booleanValue" /><br>
  java.sql.Date:<input type="text" name="sqlDate"><br>
  java.util.Date:<input type="text" name="utilDate"><br>
  <input type="submit" value="调试"><br>
 </form>

-----------------------------------------------------------------------------------------------------------

package com.bjsxt.struts;

import org.apache.struts.action.ActionForm;
/**
 * 测试ActionForm类型转换的自动转换
 * @author Administrator
 *
 */
public class TypeConvertorActionForm extends ActionForm

 {
     private int intValue;
 
     private double doubleValue;
 
     private boolean booleanValue;
 
     private java.sql.Date sqlDate;

    

      //struts内部的action包中ActionServlet,init()方法不能将其转化为该类,所以要手动写一个转换器

  private java.util.Date utilDate;

       
     public int getIntValue() {
          return intValue;
     }

     public void setIntValue(int intValue) {
          this.intValue = intValue;
     }

     public double getDoubleValue() {
          return doubleValue;
     }

     public void setDoubleValue(double doubleValue) {
          this.doubleValue = doubleValue;
     }

     public boolean isBooleanValue() {
          return booleanValue;
     }

     public void setBooleanValue(boolean booleanValue) {
          this.booleanValue = booleanValue;
     }

     public java.sql.Date getSqlDate() {
          return sqlDate;
     }

     public void setSqlDate(java.sql.Date sqlDate) {
          this.sqlDate = sqlDate;
     }

     public java.util.Date getUtilDate() {
          return utilDate;
     }

     public void setUtilDate(java.util.Date utilDate) {
          this.utilDate = utilDate;
     }
}

------------------------------------------------------------------------------------------------------------

java.util.Date的转换器

package com.bjsxt.struts;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.Converter;
/**
 * java.util.Date转换器
 * 实现Converter 接口convert方法

 *
 */
public class UtilDateConverter implements Converter {

     public Object convert(Class type, Object value) {
 
          if(value==null)//如果为空,返回
          {
               return value;
          }
          if(value instanceof Date)//如果类型是java.uitl.Date类型,返回
          {
               return value;
          }
          if(value instanceof String)//如果类型是String,开始转换成java.util.Date,再返回
          {         //也就是把一个字符串转换成一个日期
               SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//格式化日期的类
               try
               {
                    return sdf.parse((String)value);//将String转换为java.util.Date类型
               }
               catch (ParseException e) {
                e.printStackTrace();
               }
          }
          return null;
     }   

}
-----------------------------------------------------------------------------------------------------------------------------------

注册转换器(用一个servlet.java实现的)

package com.bjsxt.struts;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.beanutils.ConvertUtils;
/**
 * 清册java.util.Date转换器
 * @author Administrator
 *
 */
public class UtilDateConverterInitWithServlet extends HttpServlet {

 @Override
 public void init() throws ServletException {
      System.out.println("--------------------UtilDateConverterInitWithServlet---------------------------");
     //转换器
      UtilDateConverter converter=new UtilDateConverter();//实例一个转换器
      ConvertUtils.register(converter, java.util.Date.class);//将转换器注册进去
     }

}


web.xml的配制

<servlet>
   <servlet-name>UtilDateConverterInitWithServlet</servlet-name>
   <servlet-class>com.bjsxt.struts.UtilDateConverterInitWithServlet</servlet-class>
   <load-on-startup>6</load-on-startup>
  </servlet>

*****************另一种注册转换器的方法(struts plugin)*****************

package com.bjsxt.struts;

import javax.servlet.ServletException;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
/**
 * 注册java.util.Date,采用plugin
 * @author Administrator
 *实现PlugIn接口的init方法
 */
public class UtilDateConverterInitWithPlugin implements PlugIn {

         public void destroy() {
         }

         public void init(ActionServlet servlet, ModuleConfig config)//这是struts提供的
           throws ServletException {
          System.out.println("--------------------UtilDateConverterInitWithPlugin---------------------------");
      //转换器
      UtilDateConverter converter=new UtilDateConverter();//实例一个转换器
      ConvertUtils.register(converter, java.util.Date.class);//将转换器注册进去
         }

}
struts-config.xml的配置

    <plug-in className="com.bjsxt.struts.UtilDateConverterInitWithPlugin" />

-----------------------------------------------------------------------------------------------------------------------------

这里Action类就略了,这是接收界面

<body>
 <h1>测试ActionForm类型自动转换</h1><br>
 <hr>
 intValue:${typeconvertForm.intValue }<br>
 doubleValue:${typeconvertForm.doubleValue }<br>
 booleanValue:${typeconvertForm.booleanValue }<br>
 java.sql.Date:${typeconvertForm.sqlDate }<br>
 java.util.Date:${typeconvertForm.utilDate }<br>

<fmt:formatDate value="${typeconvertForm.utilDate }" pattern="yyyy-MM-dd"/>
</body>


=========================感想=================

其实也可不必要写转换器,可以都在ActionForm中都将将属性设为String类型,然后在Action(或jsp页面)中将其转为相应的类型.

如果要用的话,感觉用plugin比较好,因为转换器是struts的,在它的配置文件配比较明确。

原文地址:https://www.cnblogs.com/Fskjb/p/1697259.html