Seems the mail has lost.

--- "K.M Tong" <[EMAIL PROTECTED]> wrote:
> From: "K.M Tong" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Subject: [PATCH] Charset Problem
> Date: Tue, 4 Dec 2001 20:32:25 +0800
> 
> Hi,
> 
> I have tried to input Chinese Characters using JSP
> charset encoding UTF-8,
> when submitted to ActionForm, those characters
> become monsters.
> 
> The solution is given by
> http://www.jguru.com/faq/view.jsp?EID=391295
> I've tried to patch specifically for UTF-8, and
> changes to jakarta-commons's BeanUtils.java
> and struts' RequestUtils.java
> 
> Hopefully, there would be a way to specify in a
> higher level that matches the JSP's encoding
> and the ActionForm so that the "utf-8" would not
> have to hard-coded in RequestUtils.java.
> 
> Thanks.
> 
> Regards,
> kmtong
> [EMAIL PROTECTED]
> 
>   
> > Index: BeanUtils.java
>
===================================================================
> RCS file:
>
/home/cvspublic/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v
> retrieving revision 1.6
> diff -u -r1.6 BeanUtils.java
> --- BeanUtils.java    2001/09/19 02:04:10     1.6
> +++ BeanUtils.java    2001/12/04 11:32:03
> @@ -1,5 +1,5 @@
>  /*
> - * $Header:
>
/home/cvspublic/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v
> 1.6 2001/09/19 02:04:10 craigmcc Exp $
> + * $Header:
>
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v
> 1.6 2001/09/19 02:04:10 craigmcc Exp $
>   * $Revision: 1.6 $
>   * $Date: 2001/09/19 02:04:10 $
>   *
> @@ -578,5 +578,212 @@
>  
>      }
>  
> +    /**
> +     * Populate the JavaBeans properties of the
> specified bean, based on
> +     * the specified name/value pairs.  This method
> uses Java reflection APIs
> +     * to identify corresponding "property setter"
> method names, and deals
> +     * with setter arguments of type
> <code>String</code>, <code>boolean</code>,
> +     * <code>int</code>, <code>long</code>,
> <code>float</code>, and
> +     * <code>double</code>.  In addition, array
> setters for these types (or the
> +     * corresponding primitive types) can also be
> identified.
> +     * <p>
> +     * The particular setter method to be called
> for each property is
> +     * determined using the usual JavaBeans
> introspection mechanisms.  Thus,
> +     * you may identify custom setter methods using
> a BeanInfo class that is
> +     * associated with the class of the bean
> itself.  If no such BeanInfo
> +     * class is available, the standard method name
> conversion ("set" plus
> +     * the capitalized name of the property in
> question) is used.
> +     * <p>
> +     * <strong>NOTE</strong>:  It is contrary to
> the JavaBeans Specification
> +     * to have more than one setter method (with
> different argument
> +     * signatures) for the same property.
> +     *
> +     * @param bean JavaBean whose properties are
> being populated
> +     * @param properties Map keyed by property
> name, with the
> +     *  corresponding (String or String[]) value(s)
> to be set
> +     * @param fromEnc Encoding used for the input
> string(s)
> +     * @param toEnc   Encoding used for populating
> to setters
> +     *
> +     * @exception IllegalAccessException if the
> caller does not have
> +     *  access to the property accessor method
> +     * @exception InvocationTargetException if the
> property accessor method
> +     *  throws an exception
> +     */
> +    public static void populate(Object bean, Map
> properties,
> +             String fromEnc, String toEnc)
> +        throws IllegalAccessException,
> InvocationTargetException {
> +
> +        if ((bean == null) || (properties == null))
> +            return;
> +
> +        /*
> +        if (debug >= 1)
> +           
> System.out.println("BeanUtils.populate(" + bean + ",
> " +
> +                               properties + ")");
> +        */
> +
> +        // Loop through the property name/value
> pairs to be set
> +        Iterator names =
> properties.keySet().iterator();
> +        while (names.hasNext()) {
> +
> +            // Identify the property name and
> value(s) to be assigned
> +            String name = (String) names.next();
> +            if (name == null)
> +                continue;
> +            Object value = properties.get(name);     //
> String or String[]
> +
> +            /*
> +            if (debug >= 1)
> +                System.out.println("  name='" +
> name + "', value.class='" +
> +                                   (value == null ?
> "NONE" :
> +                                  
> value.getClass().getName()) + "'");
> +            */
> +
> +            // Get the property descriptor of the
> requested property (if any)
> +            PropertyDescriptor descriptor = null;
> +            try {
> +                descriptor =
> PropertyUtils.getPropertyDescriptor(bean, name);
> +            } catch (Throwable t) {
> +                /*
> +                if (debug >= 1)
> +                    System.out.println("   
> getPropertyDescriptor: " + t);
> +                */
> +                descriptor = null;
> +            }
> +            if (descriptor == null) {
> +                /*
> +                if (debug >= 1)
> +                    System.out.println("    No such
> property, skipping");
> +                */
> +                continue;
> +            }
> +            /*
> +            if (debug >= 1)
> +                System.out.println("    Property
> descriptor is '" +
> +                                   descriptor +
> "'");
> +            */
> +
> +            // Identify the relevant setter method
> (if there is one)
> +            Method setter = null;
> +            if (descriptor instanceof
> IndexedPropertyDescriptor)
> +                setter =
> ((IndexedPropertyDescriptor) descriptor).
> +                    getIndexedWriteMethod();
> +            else if (descriptor instanceof
> MappedPropertyDescriptor)
> +                setter =((MappedPropertyDescriptor)
> descriptor).getMappedWriteMethod();
> +
> +            if (setter == null)
> +                setter =
> descriptor.getWriteMethod();
> +            if (setter == null) {
> +                /*
> +                if (debug >= 1)
> +                    System.out.println("    No
> setter method, skipping");
> +                */
> +                continue;
> +            }
> +            Class parameterTypes[] =
> setter.getParameterTypes();
> +            /*
> +            if (debug >= 1)
> +                System.out.println("    Setter
> method is '" +
> +                                   setter.getName()
> + "(" +
> +                                  
> parameterTypes[0].getName() +
> +                                  
> (parameterTypes.length > 1 ?
> +                                    ", " +
> parameterTypes[1].getName() : "" )
> +                                   + ")'");
> +            */
> +            Class parameterType =
> parameterTypes[0];
> +            if (parameterTypes.length > 1)
> +                parameterType = parameterTypes[1]; 
>     // Indexed or mapped setter
> +
> +            // Convert the parameter value as
> required for this setter method
> +            Object parameters[] = new Object[1];
> +            if (parameterTypes[0].isArray()) {
> +                if (value instanceof String) {
> +                    String values[] = new
> String[1];
> +                    values[0] = convert((String)
> value, fromEnc, toEnc);
> +                    parameters[0] =
> ConvertUtils.convert((String[]) values,
> +                    parameterType);
> +                } else if (value instanceof
> String[]) {
> +                    parameters[0] =
> ConvertUtils.convert(
> +                             convert((String[]) value, fromEnc, toEnc),
> +                             parameterType);
> +                } else {
> +                    parameters[0] = value;
> +                }
> +            } else {
> +                if (value instanceof String) {
> +                    parameters[0] =
> ConvertUtils.convert(
> +                             convert((String) value, fromEnc, toEnc),
> +                             parameterType);
> +                } else if (value instanceof
> String[]) {
> +                    parameters[0] =
> ConvertUtils.convert(
> +                             convert((String[]) value, fromEnc, toEnc)[0],
> +                             parameterType);
> +                } else {
> +                    parameters[0] = value;
> +                }
> +            }
> +
> +            // Invoke the setter method
> +            /*
> +            if (debug >= 1)
> +                System.out.println("    Setting to
> " +
> +                                   (parameters[0]
> == null ? "NULL" :
> +                                    "'" +
> parameters[0] + "'"));
> +            */
> +            try {
> +                PropertyUtils.setProperty(bean,
> name, parameters[0]);
> +            } catch (NoSuchMethodException e) {
> +                /*
> +                if (debug >= 1) {
> +                    System.out.println("    CANNOT
> HAPPEN: " + e);
> +                    e.printStackTrace(System.out);
> +                }
> +                */
> +            }
> +
> +        }
> +
> +        /*
> +        if (debug >= 1)
> +           
>
System.out.println("============================================");
> +        */
> +
> +    }
> +
>  
> +    /** little helper for encoding conversion */
> +    private static String convert(String str,
> String fromEnc, String toEnc) {
> +        if (str != null) {
> +          try {
> +            return new
> String(str.getBytes(fromEnc), toEnc);
> +          } catch (Exception e) {
> +            // if cannot convert, do nothing
> +            e.printStackTrace();
> +            return str;
> +          }
> +        } else {
> +          return null;
> +        }
> +    }
> +
> +    /** little helper for encoding conversion */
> +    private static String[] convert(String str[],
> +                                    String fromEnc,
> String toEnc) {
> +        if (str != null) {
> +          String outstr[] = new String[str.length];
> +          for (int i=0; i<str.length; i++) {
> +            try {
> +              outstr[i] = convert(str[i], fromEnc,
> toEnc);
> +            } catch (Exception e) {
> +              // if cannot convert, do nothing
> +              e.printStackTrace();
> +              return str;
> +            }
> +          }
> +          return outstr;
> +        } else {
> +          return null;
> +        }
> +    }
>  }
> +
> > Index: RequestUtils.java
>
===================================================================
> RCS file:
>
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
> retrieving revision 1.25
> diff -u -r1.25 RequestUtils.java
> --- RequestUtils.java 2001/11/21 18:48:42     1.25
> +++ RequestUtils.java 2001/12/04 11:45:48
> @@ -741,7 +741,7 @@
>  
>          // Set the corresponding properties of our
> bean
>          try {
> -            BeanUtils.populate(bean, properties);
> +            BeanUtils.populate(bean, properties,
> "8859_1", "utf-8");
>          } catch (Exception e) {
>              throw new
> ServletException("BeanUtils.populate", e);
>          }
> 


__________________________________________________
Do You Yahoo!?
Send FREE video emails in Yahoo! Mail!
http://promo.yahoo.com/videomail/

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to