zoukankan      html  css  js  c++  java
  • ActionForm作为类型转换

     <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="提交">
        </form>

    ActionForm:

    package com.bjsxt.struts;

    import org.apache.struts.action.ActionForm;

    /**
     * struts中的类型转换测试
     * @author Administrator
     *
     */
    public class TypeConvertActionForm extends ActionForm {

     private int intValue;
     
     private double doubleValue;
     
     private boolean booleanValue;
     
     private java.sql.Date sqlDate;
     
     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;
     }
    }

    Action:

    package com.bjsxt.struts;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;

    /**
     * struts中的类型转换测试
     * @author Administrator
     *
     */
    public class TypeConvertTestAction extends Action {

     @Override
     public ActionForward execute(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response)
       throws Exception {
      return mapping.findForward("success");
     }

    }

    配置信息:

      <action path="/typeconvert"
        type="com.bjsxt.struts.TypeConvertTestAction"
        name="typeconvertForm"
        scope="request"
      >
       <forward name="success" path="/typeconvert_success.jsp"/> 
      </action>

    jsp页面:

    <body>
     intValue:${typeconvertForm.intValue }<br>
     doubleValue:${typeconvertForm.doubleValue }<br>
     booleanValue:${typeconvertForm.booleanValue }<br>
     java.sql.dateValue:${typeconvertForm.sqlDate }<br>
     java.util.utilValue:${typeconvertForm.utilDate }<br>
    </body>

    ActionForm在收集java.util.Date的时候不能转换,所以如果要转换到java.util.Date那么就要实现一个转换器,该转换器实现Convert类,实现convert方法,然后将转换器注册

    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类型转换器
     * @author Administrator
     *
     */
    public class UtilDateConverter implements Converter {

     public Object convert(Class type, Object value) {
      System.out.println("UtilDateConverter.value=" + value);
      if (value == null) {
       return value;
      }
      if (value instanceof Date) {
       return value;
      }
      Date d = null;

      //将字符串转换为Date
      if (value instanceof String) {
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
       try {
        d = sdf.parse((String)value);
       } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }
      return d;
     }

    }

    注册为插件:

    package com.bjsxt.struts;

    import java.util.Date;

    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;

    public class UtilDateConverterInitWithPlugin implements PlugIn {

     public void destroy() {
     }

     public void init(ActionServlet servlet, ModuleConfig config)
       throws ServletException {
      System.out.println("UtilDateConverterInitWithPlugin.init()");
      ConvertUtils.register(new UtilDateConverter(), Date.class);
     }
    }

    在struts-config.xml中配置:

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

    ActionForm是一个数据传输对象(DTO),vo是值对象(VO=DTO)

  • 相关阅读:
    day4-1
    day3-1
    day1-3
    day2-1
    day1-2
    day1 1
    对象的高度整合
    类和数据类型
    对象的绑定方法
    python总结
  • 原文地址:https://www.cnblogs.com/jinzhengquan/p/1953915.html
Copyright © 2011-2022 走看看