You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Peder Larsen <la...@gmail.com> on 2006/12/21 18:05:17 UTC

How to use IoC in struts2?

Hi, i've got one very basic question that i cant find any documentation for
nor any examples, maybe i've missed it but..

What i want to is to inject a UserInfo object into my action. In webwork2
you used components.xml for this. E.g,
    <component>
        <scope>session</scope>
        <class>foo.acme.UserInfo</class>
        <enabler>foo.acme.UserInfoAware</enabler>
    </component>

to tell the IoC framework (is it xwork?) to inject the UserInfo into any
Action implementing UserInfoAware.

Now, I've managed to inject it in struts2 as well, but i cant find a way to
inject ONE UserInfo pr session, i can only specify singleton="true" or
"false" in "applicationContext.xml".

I paste my struts.xml and applicationContext.xml here so a more enlightened
soul may give me some clue,
struts.xml,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="unrestrictedActions" extends="struts-default" >
        <interceptors>
          <interceptor name="login" class="
foo.acme.interceptor.LoginInterceptor"/>
          <interceptor-stack name="secureStack">
            <interceptor-ref name="login"/>
            <interceptor-ref name="defaultStack"/>
          </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="secureStack"/>

        <action name="index" class="indexAction">
            <result type="freemarker">/ftl/index.ftl</result>
        </action>
    </package>
</struts>
applicationContext.xml,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
    "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="userInfo" class="foo.acme.UserInfo" singleton="false"></bean>

    <bean id="indexAction" class="foo.acme.actions.IndexAction"
singleton="false">
        <property name="userInfo"><!-- THIS INJECTS THE USERINFO BUT IT
INJECTS A NEW EVERY REQUEST, WANT 1 PR SESSION -->
            <ref local="userInfo" />
        </property>
    </bean>
</beans>

Maybe theres an altogether better way of setting up the IoC in struts2? Im
all ears.

Best regards, Peder Jensen.

Re: How to use IoC in struts2?

Posted by Musachy Barroso <mb...@wfscorp.com>.
Here is a tutorial on how to use Webwork(works the same on Struts 2) 
with Spring:

http://www.opensymphony.com/webwork/wikidocs/Spring.html

just set:

<constant name="struts.objectFactory" value="spring" />

in your struts.xml instead of "webwork.objectFactory ".

regards
musachy

Nuwan Chandrasoma wrote:
> Hi Peder,
>
> As for my knowledge Spring 2 support this scoped variables, but i dont 
> have an idea of how to use it with struts2
>
> Regards,
>
> Nuwan
>
> ----- Original Message ----- From: "Peder Larsen" 
> <la...@gmail.com>
> To: <us...@struts.apache.org>
> Sent: Thursday, December 21, 2006 5:05 PM
> Subject: How to use IoC in struts2?
>
>
>> Hi, i've got one very basic question that i cant find any 
>> documentation for
>> nor any examples, maybe i've missed it but..
>>
>> What i want to is to inject a UserInfo object into my action. In 
>> webwork2
>> you used components.xml for this. E.g,
>>    <component>
>>        <scope>session</scope>
>>        <class>foo.acme.UserInfo</class>
>>        <enabler>foo.acme.UserInfoAware</enabler>
>>    </component>
>>
>> to tell the IoC framework (is it xwork?) to inject the UserInfo into any
>> Action implementing UserInfoAware.
>>
>> Now, I've managed to inject it in struts2 as well, but i cant find a 
>> way to
>> inject ONE UserInfo pr session, i can only specify singleton="true" or
>> "false" in "applicationContext.xml".
>>
>> I paste my struts.xml and applicationContext.xml here so a more 
>> enlightened
>> soul may give me some clue,
>> struts.xml,
>> <?xml version="1.0" encoding="UTF-8"?>
>> <!DOCTYPE struts PUBLIC
>>    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
>>    "http://struts.apache.org/dtds/struts-2.0.dtd">
>> <struts>
>>    <package name="unrestrictedActions" extends="struts-default" >
>>        <interceptors>
>>          <interceptor name="login" class="
>> foo.acme.interceptor.LoginInterceptor"/>
>>          <interceptor-stack name="secureStack">
>>            <interceptor-ref name="login"/>
>>            <interceptor-ref name="defaultStack"/>
>>          </interceptor-stack>
>>        </interceptors>
>>        <default-interceptor-ref name="secureStack"/>
>>
>>        <action name="index" class="indexAction">
>>            <result type="freemarker">/ftl/index.ftl</result>
>>        </action>
>>    </package>
>> </struts>
>> applicationContext.xml,
>> <?xml version="1.0" encoding="UTF-8"?>
>> <!DOCTYPE beans PUBLIC
>>    "-//SPRING//DTD BEAN//EN"
>>    "http://www.springframework.org/dtd/spring-beans.dtd">
>> <beans>
>>    <bean id="userInfo" class="foo.acme.UserInfo" 
>> singleton="false"></bean>
>>
>>    <bean id="indexAction" class="foo.acme.actions.IndexAction"
>> singleton="false">
>>        <property name="userInfo"><!-- THIS INJECTS THE USERINFO BUT IT
>> INJECTS A NEW EVERY REQUEST, WANT 1 PR SESSION -->
>>            <ref local="userInfo" />
>>        </property>
>>    </bean>
>> </beans>
>>
>> Maybe theres an altogether better way of setting up the IoC in 
>> struts2? Im
>> all ears.
>>
>> Best regards, Peder Jensen.
>>
>
>
> ---------------------------------------------------------------------
> 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: How to use IoC in struts2?

Posted by Don Brown <do...@gmail.com>.
Yes, and honestly, I'd like to keep it that way.  User applications
shouldn't be using the internal XWork Guice container as their
application IoC container.  We make no guarantees we won't completely
change how it works from version to version.

Don

On 12/21/06, Musachy Barroso <mb...@wfscorp.com> wrote:
> I forgot to mention that Guice (http://code.google.com/p/google-guice/)
> is used on Struts 2, but I can't find any documentation, except the
> javadocs.
>
> regards
> musachy
>
> Nuwan Chandrasoma wrote:
> > Hi Peder,
> >
> > As for my knowledge Spring 2 support this scoped variables, but i dont
> > have an idea of how to use it with struts2
> >
> > Regards,
> >
> > Nuwan
> >
> > ----- Original Message ----- From: "Peder Larsen"
> > <la...@gmail.com>
> > To: <us...@struts.apache.org>
> > Sent: Thursday, December 21, 2006 5:05 PM
> > Subject: How to use IoC in struts2?
> >
> >
> >> Hi, i've got one very basic question that i cant find any
> >> documentation for
> >> nor any examples, maybe i've missed it but..
> >>
> >> What i want to is to inject a UserInfo object into my action. In
> >> webwork2
> >> you used components.xml for this. E.g,
> >>    <component>
> >>        <scope>session</scope>
> >>        <class>foo.acme.UserInfo</class>
> >>        <enabler>foo.acme.UserInfoAware</enabler>
> >>    </component>
> >>
> >> to tell the IoC framework (is it xwork?) to inject the UserInfo into any
> >> Action implementing UserInfoAware.
> >>
> >> Now, I've managed to inject it in struts2 as well, but i cant find a
> >> way to
> >> inject ONE UserInfo pr session, i can only specify singleton="true" or
> >> "false" in "applicationContext.xml".
> >>
> >> I paste my struts.xml and applicationContext.xml here so a more
> >> enlightened
> >> soul may give me some clue,
> >> struts.xml,
> >> <?xml version="1.0" encoding="UTF-8"?>
> >> <!DOCTYPE struts PUBLIC
> >>    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
> >>    "http://struts.apache.org/dtds/struts-2.0.dtd">
> >> <struts>
> >>    <package name="unrestrictedActions" extends="struts-default" >
> >>        <interceptors>
> >>          <interceptor name="login" class="
> >> foo.acme.interceptor.LoginInterceptor"/>
> >>          <interceptor-stack name="secureStack">
> >>            <interceptor-ref name="login"/>
> >>            <interceptor-ref name="defaultStack"/>
> >>          </interceptor-stack>
> >>        </interceptors>
> >>        <default-interceptor-ref name="secureStack"/>
> >>
> >>        <action name="index" class="indexAction">
> >>            <result type="freemarker">/ftl/index.ftl</result>
> >>        </action>
> >>    </package>
> >> </struts>
> >> applicationContext.xml,
> >> <?xml version="1.0" encoding="UTF-8"?>
> >> <!DOCTYPE beans PUBLIC
> >>    "-//SPRING//DTD BEAN//EN"
> >>    "http://www.springframework.org/dtd/spring-beans.dtd">
> >> <beans>
> >>    <bean id="userInfo" class="foo.acme.UserInfo"
> >> singleton="false"></bean>
> >>
> >>    <bean id="indexAction" class="foo.acme.actions.IndexAction"
> >> singleton="false">
> >>        <property name="userInfo"><!-- THIS INJECTS THE USERINFO BUT IT
> >> INJECTS A NEW EVERY REQUEST, WANT 1 PR SESSION -->
> >>            <ref local="userInfo" />
> >>        </property>
> >>    </bean>
> >> </beans>
> >>
> >> Maybe theres an altogether better way of setting up the IoC in
> >> struts2? Im
> >> all ears.
> >>
> >> Best regards, Peder Jensen.
> >>
> >
> >
> > ---------------------------------------------------------------------
> > 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
>
>

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


Re: How to use IoC in struts2?

Posted by Musachy Barroso <mb...@wfscorp.com>.
I forgot to mention that Guice (http://code.google.com/p/google-guice/) 
is used on Struts 2, but I can't find any documentation, except the 
javadocs.

regards
musachy

Nuwan Chandrasoma wrote:
> Hi Peder,
>
> As for my knowledge Spring 2 support this scoped variables, but i dont 
> have an idea of how to use it with struts2
>
> Regards,
>
> Nuwan
>
> ----- Original Message ----- From: "Peder Larsen" 
> <la...@gmail.com>
> To: <us...@struts.apache.org>
> Sent: Thursday, December 21, 2006 5:05 PM
> Subject: How to use IoC in struts2?
>
>
>> Hi, i've got one very basic question that i cant find any 
>> documentation for
>> nor any examples, maybe i've missed it but..
>>
>> What i want to is to inject a UserInfo object into my action. In 
>> webwork2
>> you used components.xml for this. E.g,
>>    <component>
>>        <scope>session</scope>
>>        <class>foo.acme.UserInfo</class>
>>        <enabler>foo.acme.UserInfoAware</enabler>
>>    </component>
>>
>> to tell the IoC framework (is it xwork?) to inject the UserInfo into any
>> Action implementing UserInfoAware.
>>
>> Now, I've managed to inject it in struts2 as well, but i cant find a 
>> way to
>> inject ONE UserInfo pr session, i can only specify singleton="true" or
>> "false" in "applicationContext.xml".
>>
>> I paste my struts.xml and applicationContext.xml here so a more 
>> enlightened
>> soul may give me some clue,
>> struts.xml,
>> <?xml version="1.0" encoding="UTF-8"?>
>> <!DOCTYPE struts PUBLIC
>>    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
>>    "http://struts.apache.org/dtds/struts-2.0.dtd">
>> <struts>
>>    <package name="unrestrictedActions" extends="struts-default" >
>>        <interceptors>
>>          <interceptor name="login" class="
>> foo.acme.interceptor.LoginInterceptor"/>
>>          <interceptor-stack name="secureStack">
>>            <interceptor-ref name="login"/>
>>            <interceptor-ref name="defaultStack"/>
>>          </interceptor-stack>
>>        </interceptors>
>>        <default-interceptor-ref name="secureStack"/>
>>
>>        <action name="index" class="indexAction">
>>            <result type="freemarker">/ftl/index.ftl</result>
>>        </action>
>>    </package>
>> </struts>
>> applicationContext.xml,
>> <?xml version="1.0" encoding="UTF-8"?>
>> <!DOCTYPE beans PUBLIC
>>    "-//SPRING//DTD BEAN//EN"
>>    "http://www.springframework.org/dtd/spring-beans.dtd">
>> <beans>
>>    <bean id="userInfo" class="foo.acme.UserInfo" 
>> singleton="false"></bean>
>>
>>    <bean id="indexAction" class="foo.acme.actions.IndexAction"
>> singleton="false">
>>        <property name="userInfo"><!-- THIS INJECTS THE USERINFO BUT IT
>> INJECTS A NEW EVERY REQUEST, WANT 1 PR SESSION -->
>>            <ref local="userInfo" />
>>        </property>
>>    </bean>
>> </beans>
>>
>> Maybe theres an altogether better way of setting up the IoC in 
>> struts2? Im
>> all ears.
>>
>> Best regards, Peder Jensen.
>>
>
>
> ---------------------------------------------------------------------
> 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: How to use IoC in struts2?

Posted by Nuwan Chandrasoma <my...@gmail.com>.
Hi Peder,

As for my knowledge Spring 2 support this scoped variables, but i dont have 
an idea of how to use it with struts2

Regards,

Nuwan

----- Original Message ----- 
From: "Peder Larsen" <la...@gmail.com>
To: <us...@struts.apache.org>
Sent: Thursday, December 21, 2006 5:05 PM
Subject: How to use IoC in struts2?


> Hi, i've got one very basic question that i cant find any documentation 
> for
> nor any examples, maybe i've missed it but..
>
> What i want to is to inject a UserInfo object into my action. In webwork2
> you used components.xml for this. E.g,
>    <component>
>        <scope>session</scope>
>        <class>foo.acme.UserInfo</class>
>        <enabler>foo.acme.UserInfoAware</enabler>
>    </component>
>
> to tell the IoC framework (is it xwork?) to inject the UserInfo into any
> Action implementing UserInfoAware.
>
> Now, I've managed to inject it in struts2 as well, but i cant find a way 
> to
> inject ONE UserInfo pr session, i can only specify singleton="true" or
> "false" in "applicationContext.xml".
>
> I paste my struts.xml and applicationContext.xml here so a more 
> enlightened
> soul may give me some clue,
> struts.xml,
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE struts PUBLIC
>    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
>    "http://struts.apache.org/dtds/struts-2.0.dtd">
> <struts>
>    <package name="unrestrictedActions" extends="struts-default" >
>        <interceptors>
>          <interceptor name="login" class="
> foo.acme.interceptor.LoginInterceptor"/>
>          <interceptor-stack name="secureStack">
>            <interceptor-ref name="login"/>
>            <interceptor-ref name="defaultStack"/>
>          </interceptor-stack>
>        </interceptors>
>        <default-interceptor-ref name="secureStack"/>
>
>        <action name="index" class="indexAction">
>            <result type="freemarker">/ftl/index.ftl</result>
>        </action>
>    </package>
> </struts>
> applicationContext.xml,
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE beans PUBLIC
>    "-//SPRING//DTD BEAN//EN"
>    "http://www.springframework.org/dtd/spring-beans.dtd">
> <beans>
>    <bean id="userInfo" class="foo.acme.UserInfo" singleton="false"></bean>
>
>    <bean id="indexAction" class="foo.acme.actions.IndexAction"
> singleton="false">
>        <property name="userInfo"><!-- THIS INJECTS THE USERINFO BUT IT
> INJECTS A NEW EVERY REQUEST, WANT 1 PR SESSION -->
>            <ref local="userInfo" />
>        </property>
>    </bean>
> </beans>
>
> Maybe theres an altogether better way of setting up the IoC in struts2? Im
> all ears.
>
> Best regards, Peder Jensen.
> 


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


Re: logic:messagesPresent ???

Posted by Niall Pemberton <ni...@gmail.com>.
On 12/21/06, A. Lotfi <ma...@yahoo.com> wrote:
> Hi, I am still confused about messagesPresent , I don't know why we use it , for example I found this example :

You use it to render something if errors/messages are present. Look at
the second example here:

http://www.niallp.pwp.blueyonder.co.uk/HelpTagsErrorsAndMessages.html#section2

Niall

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


logic:messagesPresent ???

Posted by "A. Lotfi" <ma...@yahoo.com>.
Hi, I am still confused about messagesPresent , I don't know why we use it , for example I found this example :
   
   <logic:messagesPresent>
                        <script>errValue = true;</script>
                        <html:errors header="" footer=""/>
                        </logic:messagesPresent>
   
  I will appreciate if someone please can explain it to me.
  thanks

 __________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: How to use IoC in struts2?

Posted by Nuwan Chandrasoma <my...@gmail.com>.
Hi Peder,

Read this blog entry by ted, it was really helpful for me..

http://jroller.com/page/TedHusted?entry=struts_2_spring_love_fest

and also to get the bean scopes using spring 2 read the below link..

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

I hope this would be helpful to you.

Thanks,

Nuwan



----- Original Message ----- 
From: "Peder Larsen" <la...@gmail.com>
To: <us...@struts.apache.org>
Sent: Thursday, December 21, 2006 5:05 PM
Subject: How to use IoC in struts2?


> Hi, i've got one very basic question that i cant find any documentation 
> for
> nor any examples, maybe i've missed it but..
>
> What i want to is to inject a UserInfo object into my action. In webwork2
> you used components.xml for this. E.g,
>    <component>
>        <scope>session</scope>
>        <class>foo.acme.UserInfo</class>
>        <enabler>foo.acme.UserInfoAware</enabler>
>    </component>
>
> to tell the IoC framework (is it xwork?) to inject the UserInfo into any
> Action implementing UserInfoAware.
>
> Now, I've managed to inject it in struts2 as well, but i cant find a way 
> to
> inject ONE UserInfo pr session, i can only specify singleton="true" or
> "false" in "applicationContext.xml".
>
> I paste my struts.xml and applicationContext.xml here so a more 
> enlightened
> soul may give me some clue,
> struts.xml,
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE struts PUBLIC
>    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
>    "http://struts.apache.org/dtds/struts-2.0.dtd">
> <struts>
>    <package name="unrestrictedActions" extends="struts-default" >
>        <interceptors>
>          <interceptor name="login" class="
> foo.acme.interceptor.LoginInterceptor"/>
>          <interceptor-stack name="secureStack">
>            <interceptor-ref name="login"/>
>            <interceptor-ref name="defaultStack"/>
>          </interceptor-stack>
>        </interceptors>
>        <default-interceptor-ref name="secureStack"/>
>
>        <action name="index" class="indexAction">
>            <result type="freemarker">/ftl/index.ftl</result>
>        </action>
>    </package>
> </struts>
> applicationContext.xml,
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE beans PUBLIC
>    "-//SPRING//DTD BEAN//EN"
>    "http://www.springframework.org/dtd/spring-beans.dtd">
> <beans>
>    <bean id="userInfo" class="foo.acme.UserInfo" singleton="false"></bean>
>
>    <bean id="indexAction" class="foo.acme.actions.IndexAction"
> singleton="false">
>        <property name="userInfo"><!-- THIS INJECTS THE USERINFO BUT IT
> INJECTS A NEW EVERY REQUEST, WANT 1 PR SESSION -->
>            <ref local="userInfo" />
>        </property>
>    </bean>
> </beans>
>
> Maybe theres an altogether better way of setting up the IoC in struts2? Im
> all ears.
>
> Best regards, Peder Jensen.
> 


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