You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Gabriel França Campolina <ga...@gmail.com> on 2004/11/05 16:13:07 UTC

How use the BeanUtils and interface converts

Hi,

I'd like know how I can use BeanUtils.copyProperties(form, model),
when I my objects have name equals but types noequals, example, in my
form I have a attribuite named "dateInsert" of type string, but in my
model I have this attribuite of the type java.util.Date. How I
specified to BeanUtils this type of conversion. I read some articles
in internet saw that I can implements a inteface converter, but i
don't know use. SomeBody can hope me, with link or example use this
classe???

Thanks folks,

--------------------------------------------------------------------------
Gabriel França Campolina
Sun Certified Programmer for the Java 2 Plataform

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How use the BeanUtils and interface converts

Posted by Radu Badita <ra...@gmail.com>.
A simpler alternative to that approach would be to simply make an
additional setter method in you bean (and getter if appropriate) for
your Date member which takes a String, parses it (preferably according
to the user's Locale) and sets your Date member. The form data should
be first validated, otherwise you'll get a ParseException.
I hope this is useful to you.


On Fri, 5 Nov 2004 09:46:06 -0600, Joe Germuska <jo...@germuska.com> wrote:
> Writing a date converter is pretty simple; the
> only reason there isn't one included in the
> beanutils distribution is that there's not a very
> easy way to configure the date format in a
> universal way.
> 
> Below is an example.  This one is implemented as
> a static inner class within the only class which
> uses it, but you could do it differently:
> 
>      static class SimpleDateFormatDateConverter
> implements org.apache.commons.beanutils.Converter
>      {
> 
>          String dateFormat = null;
> 
>          SimpleDateFormatDateConverter(String dateFormat)
>          {
>              this.dateFormat = dateFormat;
>          }
> 
>          public Object convert(Class clazz, Object obj)
>          {
> 
>              if (obj == null) return null;
>              if (obj instanceof java.util.Date) return obj;
>              String toParse = obj.toString();
>              if (toParse.trim().length() == 0) return null;
>              java.text.SimpleDateFormat fmt = new
> java.text.SimpleDateFormat(dateFormat);
>              try
>              {
>                  return fmt.parse(obj.toString());
>              }
>              catch (Exception ex)
>              {
>                  throw new org.apache.commons.beanutils.ConversionException(ex);
>              }
> 
>          }
>      }
> 
> Then, elsewhere in your Java code, you would
> register the converter doing something like this:
>          String dateFormat = "yyyy-MM-dd";
>          ConvertUtils.register(new
> SimpleDateFormatDateConverter(dateFormat),
> java.util.Date.class);
> 
> Since this code was written, beanutils has made
> good progress away from using static methods for
> everything, which makes it easier to have
> Converters in different parts of your app that
> behave differently.  See
> http://jakarta.apache.org/commons/beanutils/api/index.html
> and more specifically
> http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/BeanUtilsBean.html#BeanUtilsBean(org.apache.commons.beanutils.ConvertUtilsBean,%20org.apache.commons.beanutils.PropertyUtilsBean)
> for the doc details.
> 
> Joe
> 
> 
> 
> 
> At 12:13 PM -0300 11/5/04, Gabriel França Campolina wrote:
> >Hi,
> >
> >I'd like know how I can use BeanUtils.copyProperties(form, model),
> >when I my objects have name equals but types noequals, example, in my
> >form I have a attribuite named "dateInsert" of type string, but in my
> >model I have this attribuite of the type java.util.Date. How I
> >specified to BeanUtils this type of conversion. I read some articles
> >in internet saw that I can implements a inteface converter, but i
> >don't know use. SomeBody can hope me, with link or example use this
> >classe???
> >
> >Thanks folks,
> >
> >--------------------------------------------------------------------------
> >Gabriel França Campolina
> >Sun Certified Programmer for the Java 2 Plataform
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> --
> Joe Germuska
> Joe@Germuska.com
> http://blog.germuska.com
> "In fact, when I die, if I don't hear 'A Love
> Supreme,' I'll turn back; I'll know I'm in the
> wrong place."
>     - Carlos Santana
> 


-- 
Radu Badita
Galati, Romania
Phone : 004-0745-36.10.17
Email : radu.badita@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How to use getServletConfig( ) outside a Servlet.

Posted by Radu Badita <ra...@gmail.com>.
Hi,

I had a problem similar to yours, and here is how I solved it:
1. made a Struts PlugIn (a class that implements
org.apache.struts.action.PlugIn) and has a method init(ActionServlet ,
ModuleConfig) and a String property (let's say configFileName). The
property will be automatically set by Struts at startup, and then the
init(...) method will be called.
2. in the init(...) method I read the file: 
InputStream is =
actionServlet.getServletContext().getResourceAsStream(configFileName);
3. the config file name is set from the struts-config.xml as follows:
    <plug-in className="mypackage.MyPlugIn">
        <set-property value="/WEB-INF/config.xml" property="configFileName"/>
    </plug-in>

This should work for you, too. Good luck! :-)


On Fri, 05 Nov 2004 17:28:25 +0100, Morkeleb <as...@gmx.at> wrote:
> Hi.
> 
> I want to make a static class which access a configuration file in /WEB-INF/.
> I tried <Classname>.class.getResourceAsStream("/WEB-INF/config-file.xml"); but it wont work.
> So I thought I have to do it by using getServletConfig().getServletContext( ).getResourceAsStream("/WEB-INF/config-file.xml")
> but I did not figure out how to do that because getServletCofig only works in Servlets, not in static beans.
> 
> Can you help me?
> 
> Thanx in Advance,
> Lukas
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 


-- 
Radu Badita
Galati, Romania
Phone : 004-0745-36.10.17
Email : radu.badita@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


How to use getServletConfig( ) outside a Servlet.

Posted by Morkeleb <as...@gmx.at>.
Hi.

I want to make a static class which access a configuration file in /WEB-INF/.
I tried <Classname>.class.getResourceAsStream("/WEB-INF/config-file.xml"); but it wont work.
So I thought I have to do it by using getServletConfig().getServletContext( ).getResourceAsStream("/WEB-INF/config-file.xml")
but I did not figure out how to do that because getServletCofig only works in Servlets, not in static beans.

Can you help me?

Thanx in Advance,
Lukas




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How use the BeanUtils and interface converts

Posted by Joe Germuska <Jo...@Germuska.com>.
Writing a date converter is pretty simple; the 
only reason there isn't one included in the 
beanutils distribution is that there's not a very 
easy way to configure the date format in a 
universal way.

Below is an example.  This one is implemented as 
a static inner class within the only class which 
uses it, but you could do it differently:

     static class SimpleDateFormatDateConverter 
implements org.apache.commons.beanutils.Converter
     {

         String dateFormat = null;

         SimpleDateFormatDateConverter(String dateFormat)
         {
             this.dateFormat = dateFormat;
         }

         public Object convert(Class clazz, Object obj)
         {

             if (obj == null) return null;
             if (obj instanceof java.util.Date) return obj;
             String toParse = obj.toString();
             if (toParse.trim().length() == 0) return null;
             java.text.SimpleDateFormat fmt = new 
java.text.SimpleDateFormat(dateFormat);
             try
             {
                 return fmt.parse(obj.toString());
             }
             catch (Exception ex)
             {
                 throw new org.apache.commons.beanutils.ConversionException(ex);
             }


         }
     }


Then, elsewhere in your Java code, you would 
register the converter doing something like this:
         String dateFormat = "yyyy-MM-dd";
         ConvertUtils.register(new 
SimpleDateFormatDateConverter(dateFormat), 
java.util.Date.class);

Since this code was written, beanutils has made 
good progress away from using static methods for 
everything, which makes it easier to have 
Converters in different parts of your app that 
behave differently.  See 
http://jakarta.apache.org/commons/beanutils/api/index.html 
and more specifically 
http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/BeanUtilsBean.html#BeanUtilsBean(org.apache.commons.beanutils.ConvertUtilsBean,%20org.apache.commons.beanutils.PropertyUtilsBean) 
for the doc details.

Joe


At 12:13 PM -0300 11/5/04, Gabriel França Campolina wrote:
>Hi,
>
>I'd like know how I can use BeanUtils.copyProperties(form, model),
>when I my objects have name equals but types noequals, example, in my
>form I have a attribuite named "dateInsert" of type string, but in my
>model I have this attribuite of the type java.util.Date. How I
>specified to BeanUtils this type of conversion. I read some articles
>in internet saw that I can implements a inteface converter, but i
>don't know use. SomeBody can hope me, with link or example use this
>classe???
>
>Thanks folks,
>
>--------------------------------------------------------------------------
>Gabriel França Campolina
>Sun Certified Programmer for the Java 2 Plataform
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org


-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
"In fact, when I die, if I don't hear 'A Love 
Supreme,' I'll turn back; I'll know I'm in the 
wrong place."
    - Carlos Santana