You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Alberto A. Flores" <aa...@gmail.com> on 2008/06/03 17:35:46 UTC

Constructor with arguments

Does anyone know how to make Struts2 ObjectFactory instantiate actions 
with constructor-arguments.

I currently have to rely on Spring to handle that case, but would be 
nice to do it without it.

-- 

Alberto A. Flores
http://www.linkedin.com/in/aflores



Re: Constructor with arguments

Posted by Laurie Harper <la...@holoweb.net>.
You don't need an XML file per class, just an entry per class in one XML 
file -- and if you use Spring 2.5's @Ccomponent/@Resource annotation 
support, you don't even need that. If you have Spring deployed anyway, 
it's definitely the best tool for the job here.

L.

Alberto A. Flores wrote:
> Thanks Brian,
> 
> Yes, I'm using Spring already. The way I fixed it was by having Spring 
> instantiate the class (I wasn't doing that before). Problem was that now 
> I need to maintain an additional XML file (for each class) which is what 
> I was trying to avoid.
> 
> Spring will do for me for now.
> 
> Relph,Brian wrote:
>>  
>> The struts2 object factory uses xwork's object factory to create the 
>> actions:
>>
>> public Object buildAction(String actionName, String namespace, 
>> ActionConfig config, Map extraContext) throws Exception {
>>         return buildBean(config.getClassName(), extraContext);
>>     }
>>
>> Which, as you can guess, can only build object with public,no-arg 
>> constructors.
>>
>>     public Object buildBean(Class clazz, Map extraContext) throws 
>> Exception {
>>         return clazz.newInstance();
>>     }
>>
>> There is a SpringObjectFactory available in xwork, which was extended 
>> in the spring plugin as the StrutsSpringObjectFactory.  These allow 
>> you to use spring to configure your actions, which if you don't mind 
>> autowiring-by-name, does not require any special work on your part.
>>
>> When you say you rely on spring to build your actions, are you already 
>> using the spring plugin?  I find it works well, and since I use spring 
>> for so many other things, I have no need to get rid of it as a 
>> dependency.  If you don't want to use spring, you can create your own 
>> object factory, and override either the buildAction or buildBean 
>> methods to handle other constructors.  It seems like you will still 
>> need to find your constructor argument objects from somewhere though, 
>> so wouldn't you need some DI framework to handle that anyway?
>>
>> I think there is work being done to adopt Guice as the default DI 
>> framework, you could look into that.
>>
>> Brian Relph
>>
>> -----Original Message-----
>> From: Alberto A. Flores [mailto:aaflores@gmail.com] Sent: Tuesday, 
>> June 03, 2008 10:36 AM
>> To: Struts Users Mailing List
>> Subject: Constructor with arguments
>>
>> Does anyone know how to make Struts2 ObjectFactory instantiate actions 
>> with constructor-arguments.
>>
>> I currently have to rely on Spring to handle that case, but would be 
>> nice to do it without it.
>>
> 
> 
> ------------------------------------------------------------------------
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org


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


Re: Constructor with arguments

Posted by "Alberto A. Flores" <aa...@gmail.com>.
Thanks Brian,

Yes, I'm using Spring already. The way I fixed it was by having Spring 
instantiate the class (I wasn't doing that before). Problem was that now 
I need to maintain an additional XML file (for each class) which is what 
I was trying to avoid.

Spring will do for me for now.

Relph,Brian wrote:
>  
> The struts2 object factory uses xwork's object factory to create the actions:
> 
> public Object buildAction(String actionName, String namespace, ActionConfig config, Map extraContext) throws Exception {
>         return buildBean(config.getClassName(), extraContext);
>     }
> 
> Which, as you can guess, can only build object with public,no-arg constructors.
> 
>     public Object buildBean(Class clazz, Map extraContext) throws Exception {
>         return clazz.newInstance();
>     }
> 
> There is a SpringObjectFactory available in xwork, which was extended in the spring plugin as the StrutsSpringObjectFactory.  These allow you to use spring to configure your actions, which if you don't mind autowiring-by-name, does not require any special work on your part.
> 
> When you say you rely on spring to build your actions, are you already using the spring plugin?  I find it works well, and since I use spring for so many other things, I have no need to get rid of it as a dependency.  If you don't want to use spring, you can create your own object factory, and override either the buildAction or buildBean methods to handle other constructors.  It seems like you will still need to find your constructor argument objects from somewhere though, so wouldn't you need some DI framework to handle that anyway?
> 
> I think there is work being done to adopt Guice as the default DI framework, you could look into that.
> 
> Brian Relph
> 
> -----Original Message-----
> From: Alberto A. Flores [mailto:aaflores@gmail.com] 
> Sent: Tuesday, June 03, 2008 10:36 AM
> To: Struts Users Mailing List
> Subject: Constructor with arguments
> 
> Does anyone know how to make Struts2 ObjectFactory instantiate actions with constructor-arguments.
> 
> I currently have to rely on Spring to handle that case, but would be nice to do it without it.
> 

-- 

Alberto A. Flores
http://www.linkedin.com/in/aflores



RE: Constructor with arguments

Posted by "Relph,Brian" <Br...@Cerner.com>.
 
The struts2 object factory uses xwork's object factory to create the actions:

public Object buildAction(String actionName, String namespace, ActionConfig config, Map extraContext) throws Exception {
        return buildBean(config.getClassName(), extraContext);
    }

Which, as you can guess, can only build object with public,no-arg constructors.

    public Object buildBean(Class clazz, Map extraContext) throws Exception {
        return clazz.newInstance();
    }

There is a SpringObjectFactory available in xwork, which was extended in the spring plugin as the StrutsSpringObjectFactory.  These allow you to use spring to configure your actions, which if you don't mind autowiring-by-name, does not require any special work on your part.

When you say you rely on spring to build your actions, are you already using the spring plugin?  I find it works well, and since I use spring for so many other things, I have no need to get rid of it as a dependency.  If you don't want to use spring, you can create your own object factory, and override either the buildAction or buildBean methods to handle other constructors.  It seems like you will still need to find your constructor argument objects from somewhere though, so wouldn't you need some DI framework to handle that anyway?

I think there is work being done to adopt Guice as the default DI framework, you could look into that.

Brian Relph

-----Original Message-----
From: Alberto A. Flores [mailto:aaflores@gmail.com] 
Sent: Tuesday, June 03, 2008 10:36 AM
To: Struts Users Mailing List
Subject: Constructor with arguments

Does anyone know how to make Struts2 ObjectFactory instantiate actions with constructor-arguments.

I currently have to rely on Spring to handle that case, but would be nice to do it without it.

-- 

Alberto A. Flores
http://www.linkedin.com/in/aflores


----------------------------------------------------------------------
CONFIDENTIALITY NOTICE This message and any included attachments are from Cerner Corporation and are intended only for the addressee. The information contained in this message is confidential and may constitute inside or non-public information under international, federal, or state securities laws. Unauthorized forwarding, printing, copying, distribution, or use of such information is strictly prohibited and may be unlawful. If you are not the addressee, please promptly delete this message and notify the sender of the delivery error by e-mail or you may call Cerner's corporate offices in Kansas City, Missouri, U.S.A at (+1) (816)221-1024.

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