You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "M. Bitner" <mo...@gmail.com> on 2007/06/14 07:11:22 UTC

[S2] Getting DAO from Spring in Struts Action

Good day,

I'm working on a Struts 2 app that uses Spring to manage the DAO
layer. I have the ContextLoaderListener and applicationContext
configured in web.xml. When the app starts I can see in the console
that Spring is picking up the beans defined in applicationContext and
instantiating them. How do I get them into my actions to use them for
data access? Are they part of the session? Thank you very much.

Melissa

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


Re: [S2] Getting DAO from Spring in Struts Action

Posted by "David Durham, Jr." <da...@gmail.com>.
On 6/15/07, M. Bitner <mo...@gmail.com> wrote:
> That got it working - thank you all so much for your help.

Turns out I was wrong, though, and it's the "class" attribute that has
to match up.  Been awhile since I looked at that.  Good that you
solved your problem, anyway.


-Dave



>
> On 6/15/07, David Durham, Jr. <da...@gmail.com> wrote:
> > On 6/14/07, M. Bitner <mo...@gmail.com> wrote:
> > >         <bean id="AddStoryCardAction"
> >
> > >         <action name="AddStoryCard"
> >
> > Your action name has to correspond to your bean id, so:
> >
> > <action name="AddStoryCardAction" ...
> >
> > or
> >
> > <bean id="AddStoryCard" ...

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


Re: [S2] Getting DAO from Spring in Struts Action

Posted by "M. Bitner" <mo...@gmail.com>.
That got it working - thank you all so much for your help.

On 6/15/07, David Durham, Jr. <da...@gmail.com> wrote:
> On 6/14/07, M. Bitner <mo...@gmail.com> wrote:
> >         <bean id="AddStoryCardAction"
>
> >         <action name="AddStoryCard"
>
> Your action name has to correspond to your bean id, so:
>
> <action name="AddStoryCardAction" ...
>
> or
>
> <bean id="AddStoryCard" ...
>
>
> HTH,
>
> Dave
>
> ---------------------------------------------------------------------
> 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: [S2] Getting DAO from Spring in Struts Action

Posted by "David Durham, Jr." <da...@gmail.com>.
On 6/14/07, M. Bitner <mo...@gmail.com> wrote:
>         <bean id="AddStoryCardAction"

>         <action name="AddStoryCard"

Your action name has to correspond to your bean id, so:

<action name="AddStoryCardAction" ...

or

<bean id="AddStoryCard" ...


HTH,

Dave

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


Re: [S2] Getting DAO from Spring in Struts Action

Posted by "M. Bitner" <mo...@gmail.com>.
I really appreciate the input - I've been struggling with this for a
while. It's still not quite working though, and I'm sure the problem
is that there's something fundamental about the process that I don't
quite understand yet.

My applicationContext.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans autowire-type="byName">

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
		<property name="driverClassName" value="org.postgresql.Driver"/>
		<property name="url" value="jdbc:postgresql:hydra"/>
		<property name="username" value="username"/>
		<property name="password" value="password"/>
	</bean>
	
	<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation" value="WEB-INF/SqlMapConfig.xml"/>
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- dao implementations -->
	<!-- snip -->
	
	<bean id="pointsDAOImpl" class="com.hbms.hydra.data.postgres.PointsDAOImpl">
		<property name="sqlMapClient" ref="sqlMapClient" />
	</bean>
	
        <!-- actions -->
	<bean id="AddStoryCardAction" class="com.hbms.hydra.struts.AddStoryCardAction">
		<property name="pointsDAOImpl" ref="pointsDAOImpl" />
	</bean>

</beans>

My action has a getter and setter for the pointsDAOImpl and a prepare
method that's using the DAO implementation:

	public void prepare() throws Exception {
		pointList = HydraService.getAllPoints(getPointsDAOImpl());
	}

I also have the action set up in the struts.xml file.

    <package name="default" extends="struts-default">
        <action name="AddStoryCard"
class="com.hbms.hydra.struts.AddStoryCardAction">
            <result name="success">/storyCards.jsp</result>
        </action>
    </package>

When I start up Tomcat I see this in the logs:

23298 [http-8080-Processor25] DEBUG
org.springframework.beans.CachedIntrospectionResults  - Found bean
property 'pointsDAOImpl' of type
[com.hbms.hydra.data.postgres.PointsDAOImpl]

and then a bit later:

23299 [http-8080-Processor25] DEBUG
org.springframework.beans.factory.support.DefaultListableBeanFactory
- Not autowiring property 'pointsDAOImpl' of bean
'com.hbms.hydra.struts.AddStoryCardAction' by name: no matching bean
found

The front page loads but when I click on the link that calls the
action I get a NPE for the pointsDAOImpl in the prepare method.

Does anyone know what I'm doing wrong? Thank you very much.

On 6/14/07, David Durham, Jr. <da...@gmail.com> wrote:
> On 6/14/07, Wesslan <fo...@wesslan.se> wrote:
> > Maybe http://struts.apache.org/2.x/docs/spring-plugin.html can be of some
> > help.
>
> You have to give your spring actions (those you want dependency
> injection for) the same value for the "name" attribute as the value
> you give for your spring bean's "id" attribute.  This is spelled out
> in the referenced document, but I missed that point the first time
> through.
>
> HTH,
> Dave
>
> ---------------------------------------------------------------------
> 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: [S2] Getting DAO from Spring in Struts Action

Posted by "David Durham, Jr." <da...@gmail.com>.
On 6/14/07, Wesslan <fo...@wesslan.se> wrote:
> Maybe http://struts.apache.org/2.x/docs/spring-plugin.html can be of some
> help.

You have to give your spring actions (those you want dependency
injection for) the same value for the "name" attribute as the value
you give for your spring bean's "id" attribute.  This is spelled out
in the referenced document, but I missed that point the first time
through.

HTH,
Dave

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


RE: [S2] Getting DAO from Spring in Struts Action

Posted by Wesslan <fo...@wesslan.se>.
Maybe http://struts.apache.org/2.x/docs/spring-plugin.html can be of some
help.

Cheers,
Peter 

-----Original Message-----
From: Dave Newton [mailto:newton.dave@yahoo.com] 
Sent: den 14 juni 2007 13:59
To: Struts Users Mailing List
Subject: Re: [S2] Getting DAO from Spring in Struts Action

--- "M. Bitner" <mo...@gmail.com> wrote:
> [...] Spring is picking up the beans defined in applicationContext and 
> instantiating them. How do I get them into my actions to use them for 
> data
access?

Two main ways:

1) Wire them up by hand (define your actions as beans in a Spring context
file, wire everything by hand)

2) Use "by-name" auto-wiring, tell S2 to use Spring as its object factory,
and provide getters/setters in your action with the same name as the bean.

d.



 
____________________________________________________________________________
________
Bored stiff? Loosen up... 
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front

---------------------------------------------------------------------
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: [S2] Getting DAO from Spring in Struts Action

Posted by Dave Newton <ne...@yahoo.com>.
--- "M. Bitner" <mo...@gmail.com> wrote:
> [...] Spring is picking up the beans defined in
> applicationContext and instantiating them. How do I 
> get them into my actions to use them for data
access?

Two main ways:

1) Wire them up by hand (define your actions as beans
in a Spring context file, wire everything by hand)

2) Use "by-name" auto-wiring, tell S2 to use Spring as
its object factory, and provide getters/setters in
your action with the same name as the bean.

d.



 
____________________________________________________________________________________
Bored stiff? Loosen up... 
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front

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