[S2] Equivalent of Servlet init() method?

2007-02-22 Thread Roger Varley

Hi

I'm a struts2 newbie  I've just got the hello world app running. I'm
now trying to convert a simple servlet app to struts2. In the original
servlet I use a singleton factory class which I initialise/setup in
the servlets init() method and use it in the servlet service() method.
What is the equivalent means of achieving this on Struts2 so that I
can set up the factory once and simply use it in my action class.

In addition, other than the wiki I'd be grateful if anyone could
recommend other documentation/tutorial sources. Should I also be
reading up on WebWork /or Struts 1?

Regards
Roger

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



RE: [S2] Equivalent of Servlet init() method?

2007-02-22 Thread Wesley Wannemacher
I looked for something similar a while back and didn't find anything simple.
There are servlet context listeners (but that isn't really struts-2-ish).
What I did (and I know it's inelegant) is to create a static initializer
block. One main difference to remember between servlets and actions is that
servlets are initialized once and remain in memory. Actions seem to be on a
one-per-request lifecycle. So, if you have a factory that you want to use,
it has to be either static in your action, or static in another class.

-Wes 

 -Original Message-
 From: Roger Varley [mailto:[EMAIL PROTECTED] 
 Sent: Thursday, February 22, 2007 9:10 AM
 To: user@struts.apache.org
 Subject: [S2] Equivalent of Servlet init() method?
 
 Hi
 
 I'm a struts2 newbie  I've just got the hello world app running. I'm
 now trying to convert a simple servlet app to struts2. In the original
 servlet I use a singleton factory class which I initialise/setup in
 the servlets init() method and use it in the servlet service() method.
 What is the equivalent means of achieving this on Struts2 so that I
 can set up the factory once and simply use it in my action class.
 
 In addition, other than the wiki I'd be grateful if anyone could
 recommend other documentation/tutorial sources. Should I also be
 reading up on WebWork /or Struts 1?
 
 Regards
 Roger
 
 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 
 


smime.p7s
Description: S/MIME cryptographic signature


RE: [S2] Equivalent of Servlet init() method?

2007-02-22 Thread bob


I could be wrong, but perhaps you should use the Spring IoC container for this. 
 I can't point you to the correct documentation off hand, but I believe that 
this is the way to maintain a resource such as what you had previoulsy done 
with the Servlet's init method and the factory class.  

Maybe someone with more experience integrating spring and struts2 can chime in 
with some doc recommendations.  I'm a bit of a newbie myself unfortunatley.




___
Join Excite! - http://www.excite.com
The most personalized portal on the Web!



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



Re: [S2] Equivalent of Servlet init() method?

2007-02-22 Thread Joe Germuska

On 2/22/07, Wesley Wannemacher [EMAIL PROTECTED] wrote:


I looked for something similar a while back and didn't find anything
simple.
There are servlet context listeners (but that isn't really struts-2-ish).



I would strongly disagree that ServletContextListeners are not really
struts-2-ish.  They are probably the best way to do any one-time
initialization for an application, unless there's already some other code to
do what you need...

On 2/22/07, bob [EMAIL PROTECTED] wrote:


I could be wrong, but perhaps you should use the Spring IoC container for
this.  I can't point you to the correct documentation off hand, but I
believe that this is the way to maintain a resource such as what you had
previoulsy done with the Servlet's init method and the factory class.

Maybe someone with more experience integrating spring and struts2 can
chime in with some doc recommendations.  I'm a bit of a newbie myself
unfortunatley.



I don't think you have to use the Spring IoC container for this, but I think
it's probably a pretty good way to do it.  I am extremely fond of using
Spring to establish an application context (not to be confused with the
webapp application context) and letting most of my code be blissfully
ignorant of how things get set up.

This page is the place to start on using the Spring plugin with Struts 2:
http://struts.apache.org/2.x/docs/spring-plugin.html

In short, Spring can instantiate any beans you want, either as singletons or
as prototypes (that is, a new bean is instantiated each time it is called
for from the Spring app context).  In instantiating, it can also satisfy
dependencies automagically.

In the Struts2/Spring model, this is usually applied to action classes,
which would be instantiated by Spring when requested by Struts.  Spring
would just give you an Action instance with a reference to your factory
(although once you start digging in Spring, you might find that you can even
hide the Factory and keep your Actions super simple and flexible.)

Joe

--
Joe Germuska
[EMAIL PROTECTED] * http://blog.germuska.com

The truth is that we learned from João forever to be out of tune.
-- Caetano Veloso


Re: [S2] Equivalent of Servlet init() method?

2007-02-22 Thread Tom Schneider

I see a couple of options.

The first would be to create an interceptor for your singleton factory.  
In the init of the interceptor you would initialize this factory and 
then provide some mechanism to inject that factory into your actions.  
(possibly via a FactoryAware interface)


Another option would be to convert your factory to use spring.  Spring 
provides mechanisms to initialize beans:

http://static.springframework.org/spring/docs/2.0.x/reference/beans.html#beans-factory-lifecycle-initializingbean
Tom


bob wrote:
I could be wrong, but perhaps you should use the Spring IoC container for this.  I can't point you to the correct documentation off hand, but I believe that this is the way to maintain a resource such as what you had previoulsy done with the Servlet's init method and the factory class.  


Maybe someone with more experience integrating spring and struts2 can chime in 
with some doc recommendations.  I'm a bit of a newbie myself unfortunatley.




___
Join Excite! - http://www.excite.com
The most personalized portal on the Web!



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


  



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



Re: [S2] Equivalent of Servlet init() method?

2007-02-22 Thread Joe Germuska

On 2/22/07, Joe Germuska [EMAIL PROTECTED] wrote:


This page is the place to start on using the Spring plugin with Struts 2:
http://struts.apache.org/2.x/docs/spring-plugin.html

In short, Spring can instantiate any beans you want, either as singletons
or as prototypes (that is, a new bean is instantiated each time it is
called for from the Spring app context).  In instantiating, it can also
satisfy dependencies automagically.

In the Struts2/Spring model, this is usually applied to action classes,
which would be instantiated by Spring when requested by Struts.  Spring
would just give you an Action instance with a reference to your factory
(although once you start digging in Spring, you might find that you can even
hide the Factory and keep your Actions super simple and flexible.)



Oh, yes, I just remembered -- the page above has a Spring config file
example using the Spring 1.x DTD.  If you should instead use the
Spring 2.xDTD (which you could, because Struts depends upon Spring 2),
you must be
aware that the default instantiation model for Spring 2 is singleton, not
prototype, and that is at odds with the model behind WebWork, which
assumes that each action is instantiated once per request.  Thus, the
example which is in the page referenced above:

   bean id=bar class=com.my.BarClass singleton=false/

would become

   bean id=bar class=com.my.BarClass scope=prototype/

Took me a little bit to realize that that was the source of a few quirks
when I was first setting out with Struts2/Spring2.




--
Joe Germuska
[EMAIL PROTECTED] * http://blog.germuska.com

The truth is that we learned from João forever to be out of tune.
-- Caetano Veloso


RE: [S2] Equivalent of Servlet init() method?

2007-02-22 Thread Wesley Wannemacher
I guess you're right, it's not that they aren't struts-2-ish... But, one
significant advantage of a static initializer block vs. servlet context
listeners is easier unit testing. I found it much easier to write the unit
tests for my actions if the JVM is in charge of initialization duties. I
don't know much about Spring and shied away from servlet context listeners
because I didn't want the external dependencies. I'm not suggesting that my
way is the best way, but if you have a requirement to write unit tests for
all of your code, then it's easier to stay away from the Servlet API.

-Wes 

 -Original Message-
 From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On 
 Behalf Of Joe Germuska
 Sent: Thursday, February 22, 2007 11:58 AM
 To: Struts Users Mailing List; [EMAIL PROTECTED]
 Subject: Re: [S2] Equivalent of Servlet init() method?
 
 On 2/22/07, Wesley Wannemacher [EMAIL PROTECTED] wrote:
 
  I looked for something similar a while back and didn't find anything
  simple.
  There are servlet context listeners (but that isn't really 
 struts-2-ish).
 
 
 I would strongly disagree that ServletContextListeners are not really
 struts-2-ish.  They are probably the best way to do any one-time
 initialization for an application, unless there's already 
 some other code to
 do what you need...
 


smime.p7s
Description: S/MIME cryptographic signature


Re: [S2] Equivalent of Servlet init() method?

2007-02-22 Thread Joe Germuska

On 2/22/07, Wesley Wannemacher [EMAIL PROTECTED] wrote:


 I don't know much about Spring and shied away from servlet context
listeners
because I didn't want the external dependencies. I'm not suggesting that
my
way is the best way, but if you have a requirement to write unit tests for
all of your code, then it's easier to stay away from the Servlet API.



That is absolutely true.  Then again, static initializer blocks can bring
headaches of their own.  I have never been very comfortable with them.

I find that using Spring steers one to design code that is eminently unit
testable, to the point where one need not use Spring in testing; one can
simply satisfy a few simple dependencies as Spring would, and let the tests
run.  This is ultimately more a factor of designing for dependency injection
than a characteristic of Spring itself, but I like Spring so...

One of the great strengths of Struts 2 is that it does a much better job of
hiding the Servlet API, making for components which are much more plausibly
unit-testable.

Joe

--
Joe Germuska
[EMAIL PROTECTED] * http://blog.germuska.com

The truth is that we learned from João forever to be out of tune.
-- Caetano Veloso