You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Andy Law <an...@roslin.ed.ac.uk> on 2010/05/12 10:57:38 UTC

Some Spring/Struts questions

All,

Apologies if this would be better asked in a Spring forum. If so, I would
appreciate guidance as to which one/where.

I have a Struts2 application with around 30 actions. Most inherit from a
common base class. We have configured the system to start to use Spring to
inject certain things into certain places. This seems to work well and we
are all currently happy bunnies.

A new requirement means that we need to have available across pretty much
every action a separate, independent bean that carries information that may
(or may not) need to be displayed on the resulting web-pages. I am wondering
what the best way is to make this bean available to each JSP.

I'm happy with injecting certain beans into certain actions, but I'm not
clear what I should be injecting *this* bean into. Ideally it should just
pop into existence and be available to the JSPs without the Actions even
being aware that it is there. It's not part of the business logic and is
purely informational. If I have to make it available through the Actions
then I would *like* to be able to inject it into the common base class with
a single configuration and have that work automatically for all sub-classes
(and this would be a useful trick for some other places in the application)
but that does not seem to be possible unless I'm missing a trick?

How should I configure this, please?

Later,

Andy
-- 
View this message in context: http://old.nabble.com/Some-Spring-Struts-questions-tp28533505p28533505.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


RE: Some Spring/Struts questions

Posted by James Cook <Ja...@wecomm.com>.
Cool, cheers Wes

-----Original Message-----
From: Wes Wannemacher [mailto:wesw@wantii.com] 
Sent: 12 May 2010 15:29
To: Struts Users Mailing List
Subject: Re: Some Spring/Struts questions

Yep... Here's a quick example struts.xml -

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
        "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>

    <constant name="struts.convention.default.parent.package" value="/view"/>

    <package name="/view" namespace="/view" extends="app-base">

        <result-types>
            <result-type name="json"
class="org.apache.struts2.json.JSONResult"/>
        </result-types>

        <interceptors>
            <interceptor name="json"
class="org.apache.struts2.json.JSONInterceptor"/>
            <interceptor name="securityInterceptor"
class="securityInterceptor"/>

            <!-- what follows is a mod of the stock
paramsPrepareParams, but jsonValidation is added -->
            <interceptor-stack name="paramsPrepareParamsJsonStack">
                <interceptor-ref name="exception"/>
                <interceptor-ref name="servletConfig"/>
                <interceptor-ref name="checkbox"/>
                <interceptor-ref name="params">
                    <param name="excludeParams">dojo\..*,^struts\..*</param>
                </interceptor-ref>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="actionMappingParams"/>
                <interceptor-ref name="params">
                    <param name="excludeParams">dojo\..*,^struts\..*</param>
                </interceptor-ref>
                <interceptor-ref name="conversionError"/>
                <interceptor-ref name="validation">
                    <param name="excludeMethods">input,back,cancel</param>
                </interceptor-ref>
                <interceptor-ref name="jsonValidation"/>
                <interceptor-ref name="workflow">
                    <param name="excludeMethods">input,back,cancel</param>
                </interceptor-ref>
            </interceptor-stack>

            <interceptor-stack name="appStack">
                <interceptor-ref name="securityInterceptor"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="appStack"/>

        <global-results>
            <result name="loginRequired" type="redirect">
                <param name="location">/login.jsp</param>
                <param name="parse">false</param>
            </result>
        </global-results>

    </package>

</struts>

On Wed, May 12, 2010 at 10:03 AM, James Cook <Ja...@wecomm.com> wrote:
> Ah, awesome. Thank you for clearing that one up and your package is defined in the struts.xml in the root of the source?
>
> -----Original Message-----
> From: Wes Wannemacher [mailto:wesw@wantii.com]
> Sent: 12 May 2010 14:51
> To: Struts Users Mailing List
> Subject: Re: Some Spring/Struts questions
>
> James,
>
> What I normally do is create a "package" and set the defaults that I
> want stuff to use (interceptors / interceptor stack / etc.). Then, I
> just tell the convention plugin to use the package I configured....
>
> The list of properties that the plugin uses is here -
> http://struts.apache.org/2.x/docs/convention-plugin.html#ConventionPlugin-Configurationreference
>
> I think the setting is -
>
> struts.convention.default.parent.package
>
> -Wes
>
> On Wed, May 12, 2010 at 9:39 AM, James Cook <Ja...@wecomm.com> wrote:
>> That would be my favoured route.
>>
>> Wes, side question. Using the convention plugin can you modify the stack
>> in the xml file so you don't have to annotate the action itself? Or do
>> you still have to add the annotation with the interceptor put in?
>>
>> James
>>
>> -----Original Message-----
>> From: Wes Wannemacher [mailto:wesw@wantii.com]
>> Sent: 12 May 2010 14:33
>> To: Struts Users Mailing List
>> Subject: Re: Some Spring/Struts questions
>>
>> I've seen a few responses, but I think the best way would be to write
>> an interceptor that places the bean into the session. Give it a known
>> name in the session, then you can use OGNL to access it... Here is a
>> quick example of the interceptor -
>>
>> public class CustomInterceptor extends AbstractInterceptor {
>>
>>    private SomeBeanGeneratingService someBeanGeneratingService;
>>
>>    public String intercept(ActionInvocation invocation) throws
>> Exception {
>>       invocation.getSession().put("knownBeanName",
>> someBeanGeneratingService.getBean());
>>       return invocation.invoke();
>>    }
>>
>>    public void setSomeBeanGeneratingService(SomeBeanGeneratingService
>> someBeanGeneratingService) {
>>        this.someBeanGeneratingService = someBeanGeneratingService;
>>    }
>> }
>>
>> The benefit is using an interceptor is two-fold... One is that it's
>> pretty easy to unit test (compared to a Servlet Filter). Two is that
>> it can be spring-configured and spring-injected.
>>
>> With this interceptor putting the bean into the session, you can
>> access the bean's values through OGNL. Here is an example s:property
>> tag retrieving some property -
>>
>> <s:property value="%{#session.knownBeanName.someProperty}" />
>>
>> Then, configure the interceptor and dependencies and put this
>> interceptor in your stack.
>>
>> -Wes
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
>
>
> --
> Wes Wannemacher
>
> Head Engineer, WanTii, Inc.
> Need Training? Struts, Spring, Maven, Tomcat...
> Ask me for a quote!
>
> ---------------------------------------------------------------------
> 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
>
>



-- 
Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

---------------------------------------------------------------------
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: Some Spring/Struts questions

Posted by Wes Wannemacher <we...@wantii.com>.
Yep... Here's a quick example struts.xml -

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
        "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>

    <constant name="struts.convention.default.parent.package" value="/view"/>

    <package name="/view" namespace="/view" extends="app-base">

        <result-types>
            <result-type name="json"
class="org.apache.struts2.json.JSONResult"/>
        </result-types>

        <interceptors>
            <interceptor name="json"
class="org.apache.struts2.json.JSONInterceptor"/>
            <interceptor name="securityInterceptor"
class="securityInterceptor"/>

            <!-- what follows is a mod of the stock
paramsPrepareParams, but jsonValidation is added -->
            <interceptor-stack name="paramsPrepareParamsJsonStack">
                <interceptor-ref name="exception"/>
                <interceptor-ref name="servletConfig"/>
                <interceptor-ref name="checkbox"/>
                <interceptor-ref name="params">
                    <param name="excludeParams">dojo\..*,^struts\..*</param>
                </interceptor-ref>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="actionMappingParams"/>
                <interceptor-ref name="params">
                    <param name="excludeParams">dojo\..*,^struts\..*</param>
                </interceptor-ref>
                <interceptor-ref name="conversionError"/>
                <interceptor-ref name="validation">
                    <param name="excludeMethods">input,back,cancel</param>
                </interceptor-ref>
                <interceptor-ref name="jsonValidation"/>
                <interceptor-ref name="workflow">
                    <param name="excludeMethods">input,back,cancel</param>
                </interceptor-ref>
            </interceptor-stack>

            <interceptor-stack name="appStack">
                <interceptor-ref name="securityInterceptor"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="appStack"/>

        <global-results>
            <result name="loginRequired" type="redirect">
                <param name="location">/login.jsp</param>
                <param name="parse">false</param>
            </result>
        </global-results>

    </package>

</struts>

On Wed, May 12, 2010 at 10:03 AM, James Cook <Ja...@wecomm.com> wrote:
> Ah, awesome. Thank you for clearing that one up and your package is defined in the struts.xml in the root of the source?
>
> -----Original Message-----
> From: Wes Wannemacher [mailto:wesw@wantii.com]
> Sent: 12 May 2010 14:51
> To: Struts Users Mailing List
> Subject: Re: Some Spring/Struts questions
>
> James,
>
> What I normally do is create a "package" and set the defaults that I
> want stuff to use (interceptors / interceptor stack / etc.). Then, I
> just tell the convention plugin to use the package I configured....
>
> The list of properties that the plugin uses is here -
> http://struts.apache.org/2.x/docs/convention-plugin.html#ConventionPlugin-Configurationreference
>
> I think the setting is -
>
> struts.convention.default.parent.package
>
> -Wes
>
> On Wed, May 12, 2010 at 9:39 AM, James Cook <Ja...@wecomm.com> wrote:
>> That would be my favoured route.
>>
>> Wes, side question. Using the convention plugin can you modify the stack
>> in the xml file so you don't have to annotate the action itself? Or do
>> you still have to add the annotation with the interceptor put in?
>>
>> James
>>
>> -----Original Message-----
>> From: Wes Wannemacher [mailto:wesw@wantii.com]
>> Sent: 12 May 2010 14:33
>> To: Struts Users Mailing List
>> Subject: Re: Some Spring/Struts questions
>>
>> I've seen a few responses, but I think the best way would be to write
>> an interceptor that places the bean into the session. Give it a known
>> name in the session, then you can use OGNL to access it... Here is a
>> quick example of the interceptor -
>>
>> public class CustomInterceptor extends AbstractInterceptor {
>>
>>    private SomeBeanGeneratingService someBeanGeneratingService;
>>
>>    public String intercept(ActionInvocation invocation) throws
>> Exception {
>>       invocation.getSession().put("knownBeanName",
>> someBeanGeneratingService.getBean());
>>       return invocation.invoke();
>>    }
>>
>>    public void setSomeBeanGeneratingService(SomeBeanGeneratingService
>> someBeanGeneratingService) {
>>        this.someBeanGeneratingService = someBeanGeneratingService;
>>    }
>> }
>>
>> The benefit is using an interceptor is two-fold... One is that it's
>> pretty easy to unit test (compared to a Servlet Filter). Two is that
>> it can be spring-configured and spring-injected.
>>
>> With this interceptor putting the bean into the session, you can
>> access the bean's values through OGNL. Here is an example s:property
>> tag retrieving some property -
>>
>> <s:property value="%{#session.knownBeanName.someProperty}" />
>>
>> Then, configure the interceptor and dependencies and put this
>> interceptor in your stack.
>>
>> -Wes
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
>
>
> --
> Wes Wannemacher
>
> Head Engineer, WanTii, Inc.
> Need Training? Struts, Spring, Maven, Tomcat...
> Ask me for a quote!
>
> ---------------------------------------------------------------------
> 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
>
>



-- 
Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

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


RE: Some Spring/Struts questions

Posted by James Cook <Ja...@wecomm.com>.
Ah, awesome. Thank you for clearing that one up and your package is defined in the struts.xml in the root of the source?

-----Original Message-----
From: Wes Wannemacher [mailto:wesw@wantii.com] 
Sent: 12 May 2010 14:51
To: Struts Users Mailing List
Subject: Re: Some Spring/Struts questions

James,

What I normally do is create a "package" and set the defaults that I
want stuff to use (interceptors / interceptor stack / etc.). Then, I
just tell the convention plugin to use the package I configured....

The list of properties that the plugin uses is here -
http://struts.apache.org/2.x/docs/convention-plugin.html#ConventionPlugin-Configurationreference

I think the setting is -

struts.convention.default.parent.package	

-Wes

On Wed, May 12, 2010 at 9:39 AM, James Cook <Ja...@wecomm.com> wrote:
> That would be my favoured route.
>
> Wes, side question. Using the convention plugin can you modify the stack
> in the xml file so you don't have to annotate the action itself? Or do
> you still have to add the annotation with the interceptor put in?
>
> James
>
> -----Original Message-----
> From: Wes Wannemacher [mailto:wesw@wantii.com]
> Sent: 12 May 2010 14:33
> To: Struts Users Mailing List
> Subject: Re: Some Spring/Struts questions
>
> I've seen a few responses, but I think the best way would be to write
> an interceptor that places the bean into the session. Give it a known
> name in the session, then you can use OGNL to access it... Here is a
> quick example of the interceptor -
>
> public class CustomInterceptor extends AbstractInterceptor {
>
>    private SomeBeanGeneratingService someBeanGeneratingService;
>
>    public String intercept(ActionInvocation invocation) throws
> Exception {
>       invocation.getSession().put("knownBeanName",
> someBeanGeneratingService.getBean());
>       return invocation.invoke();
>    }
>
>    public void setSomeBeanGeneratingService(SomeBeanGeneratingService
> someBeanGeneratingService) {
>        this.someBeanGeneratingService = someBeanGeneratingService;
>    }
> }
>
> The benefit is using an interceptor is two-fold... One is that it's
> pretty easy to unit test (compared to a Servlet Filter). Two is that
> it can be spring-configured and spring-injected.
>
> With this interceptor putting the bean into the session, you can
> access the bean's values through OGNL. Here is an example s:property
> tag retrieving some property -
>
> <s:property value="%{#session.knownBeanName.someProperty}" />
>
> Then, configure the interceptor and dependencies and put this
> interceptor in your stack.
>
> -Wes
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

---------------------------------------------------------------------
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: Some Spring/Struts questions

Posted by Wes Wannemacher <we...@wantii.com>.
James,

What I normally do is create a "package" and set the defaults that I
want stuff to use (interceptors / interceptor stack / etc.). Then, I
just tell the convention plugin to use the package I configured....

The list of properties that the plugin uses is here -
http://struts.apache.org/2.x/docs/convention-plugin.html#ConventionPlugin-Configurationreference

I think the setting is -

struts.convention.default.parent.package	

-Wes

On Wed, May 12, 2010 at 9:39 AM, James Cook <Ja...@wecomm.com> wrote:
> That would be my favoured route.
>
> Wes, side question. Using the convention plugin can you modify the stack
> in the xml file so you don't have to annotate the action itself? Or do
> you still have to add the annotation with the interceptor put in?
>
> James
>
> -----Original Message-----
> From: Wes Wannemacher [mailto:wesw@wantii.com]
> Sent: 12 May 2010 14:33
> To: Struts Users Mailing List
> Subject: Re: Some Spring/Struts questions
>
> I've seen a few responses, but I think the best way would be to write
> an interceptor that places the bean into the session. Give it a known
> name in the session, then you can use OGNL to access it... Here is a
> quick example of the interceptor -
>
> public class CustomInterceptor extends AbstractInterceptor {
>
>    private SomeBeanGeneratingService someBeanGeneratingService;
>
>    public String intercept(ActionInvocation invocation) throws
> Exception {
>       invocation.getSession().put("knownBeanName",
> someBeanGeneratingService.getBean());
>       return invocation.invoke();
>    }
>
>    public void setSomeBeanGeneratingService(SomeBeanGeneratingService
> someBeanGeneratingService) {
>        this.someBeanGeneratingService = someBeanGeneratingService;
>    }
> }
>
> The benefit is using an interceptor is two-fold... One is that it's
> pretty easy to unit test (compared to a Servlet Filter). Two is that
> it can be spring-configured and spring-injected.
>
> With this interceptor putting the bean into the session, you can
> access the bean's values through OGNL. Here is an example s:property
> tag retrieving some property -
>
> <s:property value="%{#session.knownBeanName.someProperty}" />
>
> Then, configure the interceptor and dependencies and put this
> interceptor in your stack.
>
> -Wes
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

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


RE: Some Spring/Struts questions

Posted by James Cook <Ja...@wecomm.com>.
That would be my favoured route.

Wes, side question. Using the convention plugin can you modify the stack
in the xml file so you don't have to annotate the action itself? Or do
you still have to add the annotation with the interceptor put in?

James

-----Original Message-----
From: Wes Wannemacher [mailto:wesw@wantii.com] 
Sent: 12 May 2010 14:33
To: Struts Users Mailing List
Subject: Re: Some Spring/Struts questions

I've seen a few responses, but I think the best way would be to write
an interceptor that places the bean into the session. Give it a known
name in the session, then you can use OGNL to access it... Here is a
quick example of the interceptor -

public class CustomInterceptor extends AbstractInterceptor {

    private SomeBeanGeneratingService someBeanGeneratingService;

    public String intercept(ActionInvocation invocation) throws
Exception {
       invocation.getSession().put("knownBeanName",
someBeanGeneratingService.getBean());
       return invocation.invoke();
    }

    public void setSomeBeanGeneratingService(SomeBeanGeneratingService
someBeanGeneratingService) {
        this.someBeanGeneratingService = someBeanGeneratingService;
    }
}

The benefit is using an interceptor is two-fold... One is that it's
pretty easy to unit test (compared to a Servlet Filter). Two is that
it can be spring-configured and spring-injected.

With this interceptor putting the bean into the session, you can
access the bean's values through OGNL. Here is an example s:property
tag retrieving some property -

<s:property value="%{#session.knownBeanName.someProperty}" />

Then, configure the interceptor and dependencies and put this
interceptor in your stack.

-Wes


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


Re: Some Spring/Struts questions

Posted by Dale Newfield <da...@newfield.org>.
On 5/13/10 7:10 AM, Andy Law wrote:
> Following up on that thought process, if I use an interceptor then is there
> any difference/tradeoff/efficiency gain or loss from stuffing the object in
> question onto the Value Stack instead of dropping it into the request or the
> session? I guess that "explicitness" is what you gain from the
> request/session route?

You want to be able to directly address it rather than having to search 
through the value stack for it (and make other things search this bean 
in case of unintended conflicts), so while I wouldn't put it "on" the 
value stack, if you put it in the name-based hashmap that lives along 
side the stack you're good.  (Meaning use the ValueStack set() method 
instead of push().)  (And no, I don't have a reason to suggest OGNL over 
normal Request scope, or vice-versa.)

-Dale

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


Re: Some Spring/Struts questions

Posted by Andy Law <an...@roslin.ed.ac.uk>.


Wes Wannemacher wrote:
> 
> On Wed, May 12, 2010 at 10:42 AM, Dale Newfield <da...@newfield.org> wrote:
>>
>> That all sounds reasonable, except the part about putting it in the
>> session
>> instead of the request.  If there's no compelling reason to store
>> something
>> in the session, I think that it should be avoided.  If you're using an
>> interceptor that's going to be doing this injection for each request
>> anyway,
>> you might as well store it in request scope.
>>
>> If you're storing it in the session, and the bean doesn't change much
>> (and
>> can't get stale), the interceptor might as well at least check to see if
>> it's already present and act as a no-op in that case.
>>
> 
> 
> Good point, Dale... I didn't read the OP close enough and was being lazy.
> 
> -Wes
> 
> 

Thanks for all the replies so far folks.

Following up on that thought process, if I use an interceptor then is there
any difference/tradeoff/efficiency gain or loss from stuffing the object in
question onto the Value Stack instead of dropping it into the request or the
session? I guess that "explicitness" is what you gain from the
request/session route?

Later,

Andy

-- 
View this message in context: http://old.nabble.com/Some-Spring-Struts-questions-tp28533505p28546199.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Some Spring/Struts questions

Posted by Wes Wannemacher <we...@wantii.com>.
On Wed, May 12, 2010 at 10:42 AM, Dale Newfield <da...@newfield.org> wrote:
>
> That all sounds reasonable, except the part about putting it in the session
> instead of the request.  If there's no compelling reason to store something
> in the session, I think that it should be avoided.  If you're using an
> interceptor that's going to be doing this injection for each request anyway,
> you might as well store it in request scope.
>
> If you're storing it in the session, and the bean doesn't change much (and
> can't get stale), the interceptor might as well at least check to see if
> it's already present and act as a no-op in that case.
>


Good point, Dale... I didn't read the OP close enough and was being lazy.

-Wes

-- 
Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

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


Re: Some Spring/Struts questions

Posted by Dale Newfield <da...@newfield.org>.
On 5/12/10 9:32 AM, Wes Wannemacher wrote:
> I've seen a few responses, but I think the best way would be to write
> an interceptor that places the bean into the session. Give it a known
> name in the session, then you can use OGNL to access it...

That all sounds reasonable, except the part about putting it in the 
session instead of the request.  If there's no compelling reason to 
store something in the session, I think that it should be avoided.  If 
you're using an interceptor that's going to be doing this injection for 
each request anyway, you might as well store it in request scope.

If you're storing it in the session, and the bean doesn't change much 
(and can't get stale), the interceptor might as well at least check to 
see if it's already present and act as a no-op in that case.

-Dale

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


Re: Some Spring/Struts questions

Posted by Wes Wannemacher <we...@wantii.com>.
I've seen a few responses, but I think the best way would be to write
an interceptor that places the bean into the session. Give it a known
name in the session, then you can use OGNL to access it... Here is a
quick example of the interceptor -

public class CustomInterceptor extends AbstractInterceptor {

    private SomeBeanGeneratingService someBeanGeneratingService;

    public String intercept(ActionInvocation invocation) throws Exception {
       invocation.getSession().put("knownBeanName",
someBeanGeneratingService.getBean());
       return invocation.invoke();
    }

    public void setSomeBeanGeneratingService(SomeBeanGeneratingService
someBeanGeneratingService) {
        this.someBeanGeneratingService = someBeanGeneratingService;
    }
}

The benefit is using an interceptor is two-fold... One is that it's
pretty easy to unit test (compared to a Servlet Filter). Two is that
it can be spring-configured and spring-injected.

With this interceptor putting the bean into the session, you can
access the bean's values through OGNL. Here is an example s:property
tag retrieving some property -

<s:property value="%{#session.knownBeanName.someProperty}" />

Then, configure the interceptor and dependencies and put this
interceptor in your stack.

-Wes

On Wed, May 12, 2010 at 4:57 AM, Andy Law <an...@roslin.ed.ac.uk> wrote:
>
> All,
>
> Apologies if this would be better asked in a Spring forum. If so, I would
> appreciate guidance as to which one/where.
>
> I have a Struts2 application with around 30 actions. Most inherit from a
> common base class. We have configured the system to start to use Spring to
> inject certain things into certain places. This seems to work well and we
> are all currently happy bunnies.
>
> A new requirement means that we need to have available across pretty much
> every action a separate, independent bean that carries information that may
> (or may not) need to be displayed on the resulting web-pages. I am wondering
> what the best way is to make this bean available to each JSP.
>
> I'm happy with injecting certain beans into certain actions, but I'm not
> clear what I should be injecting *this* bean into. Ideally it should just
> pop into existence and be available to the JSPs without the Actions even
> being aware that it is there. It's not part of the business logic and is
> purely informational. If I have to make it available through the Actions
> then I would *like* to be able to inject it into the common base class with
> a single configuration and have that work automatically for all sub-classes
> (and this would be a useful trick for some other places in the application)
> but that does not seem to be possible unless I'm missing a trick?
>
> How should I configure this, please?
>
> Later,
>
> Andy
> --
> View this message in context: http://old.nabble.com/Some-Spring-Struts-questions-tp28533505p28533505.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

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


Re: Some Spring/Struts questions

Posted by Andy Law <an...@roslin.ed.ac.uk>.


dcabasson wrote:
> 
> That's actually possible. We are doing that type of things by using the 
> @Autowired annotation, which means that spring needs to find a suitable 
> bean to inject there. If you put that annotation on your base class, the 
> instance will get injected in all sub-classes. But in that case, it's 
> better for the actions to be unaware of that, so I would using the 
> static approach. Otherwise, you could use an interceptor to push that 
> object on to the stack.
> 

But that introduces a dependency on Spring into my Action classes, no? And
puts *some* of the configuration into the code and some inside of the Spring
config file.

I think that I'd rather avoid those outcomes if at all possible.

I think I'm leaning back towards Interceptors :o{

Later,

Andy
-- 
View this message in context: http://old.nabble.com/Some-Spring-Struts-questions-tp28533505p28535809.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Some Spring/Struts questions

Posted by Denis Cabasson <de...@gmail.com>.
> A new requirement means that we need to have available across pretty much
> every action a separate, independent bean that carries information that may
> (or may not) need to be displayed on the resulting web-pages. I am wondering
> what the best way is to make this bean available to each JSP.
>    
We have a case which is pretty much like that. We have decided to use a 
static property in a configuration class to hold this instance. OGNL 
works pretty nicely with static members, using 
@com.MyClass@myBean.property syntax. We are injecting this bean onto the 
static member using the MethodInvokingFactoryBean (see 
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/MethodInvokingFactoryBean.html).
> I'm happy with injecting certain beans into certain actions, but I'm not
> clear what I should be injecting *this* bean into. Ideally it should just
> pop into existence and be available to the JSPs without the Actions even
> being aware that it is there. It's not part of the business logic and is
> purely informational. If I have to make it available through the Actions
> then I would *like* to be able to inject it into the common base class with
> a single configuration and have that work automatically for all sub-classes
> (and this would be a useful trick for some other places in the application)
> but that does not seem to be possible unless I'm missing a trick?
>
>    
That's actually possible. We are doing that type of things by using the 
@Autowired annotation, which means that spring needs to find a suitable 
bean to inject there. If you put that annotation on your base class, the 
instance will get injected in all sub-classes. But in that case, it's 
better for the actions to be unaware of that, so I would using the 
static approach. Otherwise, you could use an interceptor to push that 
object on to the stack.

Denis.


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


RE: Some Spring/Struts questions

Posted by Andy Law <an...@roslin.ed.ac.uk>.
Martin,



mgainty wrote:
> 
> 
> jsp:useBean for quite some time
> <jsp:useBean id="query" scope="application"
> class="enterprise.criteriaQuery.ejb.StatelessSessionBean" />
> 
> in your jsp if you have a method called getCriteria the jsp could call the
> bean method directly via
> 
> <%=query.getCriteria()%>
> 
> 


OK. This looks like it could well be a good route to take except that I
can't work out how to make the jsp:useBean tag actually specify the copy of
the object that it wants to come from Spring. There is bean configuration
going on inside of the Spring configuration and so having instantiated a
singleton inside of the Spring config, I then want to tell the jsp to access
that instance. Th class specification that I'm working to is also an
Interface and I don't want to hard-code the specific implementation name
inside of the jsps as I need to configure that in Spring so I can plug in
alternative implementations in different instances of the application.

Is any of that possible?

Later,

Andy
-- 
View this message in context: http://old.nabble.com/Some-Spring-Struts-questions-tp28533505p28535778.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


RE: Some Spring/Struts questions

Posted by Martin Gainty <mg...@hotmail.com>.
jsp:useBean for quite some time
<jsp:useBean id="query" scope="application" class="enterprise.criteriaQuery.ejb.StatelessSessionBean" />

 

in your jsp if you have a method called getCriteria the jsp could call the bean method directly via

 

<%=query.getCriteria()%>

 

HTH
Martin Gainty 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.



 

> Date: Wed, 12 May 2010 04:43:13 -0700
> From: andy.law@roslin.ed.ac.uk
> To: user@struts.apache.org
> Subject: RE: Some Spring/Struts questions
> 
> 
> 
> 
> James Cook-13 wrote:
> > 
> > In the past I have done this for accessing beans in a servlet.
> > 
> > <bean
> > class="org.springframework.web.context.support.ServletContextAttributeEx
> > porter">
> > <property name="attributes">
> > <map>
> > <entry key="organisationService"
> > value-ref="myOrganisationServiceBeanRef"/>
> > </map>
> > </property>
> > </bean>
> > 
> > OrganisationService organisationService = (OrganisationService)
> > getServletContext().getAttribute("organisationService");
> > 
> 
> 
> OK. Thanks. I'll try that.
> 
> Is it possible though to use Spring to push objects onto the Struts OGNL
> stack? That would be neater?
> 
> Later,
> 
> Andy
> -- 
> View this message in context: http://old.nabble.com/Some-Spring-Struts-questions-tp28533505p28534868.html
> Sent from the Struts - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
 		 	   		  
_________________________________________________________________
The New Busy is not the old busy. Search, chat and e-mail from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3

RE: Some Spring/Struts questions

Posted by Andy Law <an...@roslin.ed.ac.uk>.


James Cook-13 wrote:
> 
> In the past I have done this for accessing beans in a servlet.
> 
> <bean
> class="org.springframework.web.context.support.ServletContextAttributeEx
> porter">
> 	<property name="attributes">
> 		<map>
>                 <entry key="organisationService"
> value-ref="myOrganisationServiceBeanRef"/>
>              </map>
>       </property>
> </bean>
> 
> OrganisationService organisationService = (OrganisationService)
> getServletContext().getAttribute("organisationService");
> 


OK. Thanks. I'll try that.

Is it possible though to use Spring to push objects onto the Struts OGNL
stack? That would be neater?

Later,

Andy
-- 
View this message in context: http://old.nabble.com/Some-Spring-Struts-questions-tp28533505p28534868.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


RE: Some Spring/Struts questions

Posted by James Cook <Ja...@wecomm.com>.
In the past I have done this for accessing beans in a servlet.

<bean
class="org.springframework.web.context.support.ServletContextAttributeEx
porter">
	<property name="attributes">
		<map>
                <entry key="organisationService"
value-ref="myOrganisationServiceBeanRef"/>
             </map>
      </property>
</bean>

OrganisationService organisationService = (OrganisationService)
getServletContext().getAttribute("organisationService");



-----Original Message-----
From: Andy Law [mailto:andy.law@roslin.ed.ac.uk] 
Sent: 12 May 2010 10:23
To: user@struts.apache.org
Subject: RE: Some Spring/Struts questions



James Cook-13 wrote:
> 
> You could, add the bean to the servlet context, and access it via a
> scriptlet in the jsp. Thus bypassing your actions all together.
> 

What does the Spring configuration look like for that course of action?


James Cook-13 wrote:
> 
> Or.. Create filter/Inteceptor and inject into them?
> 

OK. We're already doing that for some of our stuff and that kind of
handles
our "inheritance" problem (same injection into multiple subclass types
with
single configuration). It feels a bit "hacky" in this context and I'd
also
have to make all the actions "aware" of the object being added. The
other
objects that we handle in this manner are genuinely integral to the
Actions
(data access objects and permissions filters for example).

Later,

Andy

-- 
View this message in context:
http://old.nabble.com/Some-Spring-Struts-questions-tp28533505p28533688.h
tml
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
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: Some Spring/Struts questions

Posted by Andy Law <an...@roslin.ed.ac.uk>.

James Cook-13 wrote:
> 
> You could, add the bean to the servlet context, and access it via a
> scriptlet in the jsp. Thus bypassing your actions all together.
> 

What does the Spring configuration look like for that course of action?


James Cook-13 wrote:
> 
> Or.. Create filter/Inteceptor and inject into them?
> 

OK. We're already doing that for some of our stuff and that kind of handles
our "inheritance" problem (same injection into multiple subclass types with
single configuration). It feels a bit "hacky" in this context and I'd also
have to make all the actions "aware" of the object being added. The other
objects that we handle in this manner are genuinely integral to the Actions
(data access objects and permissions filters for example).

Later,

Andy

-- 
View this message in context: http://old.nabble.com/Some-Spring-Struts-questions-tp28533505p28533688.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


RE: Some Spring/Struts questions

Posted by James Cook <Ja...@wecomm.com>.
You could, add the bean to the servlet context, and access it via a
scriptlet in the jsp. Thus bypassing your actions all together.

Or.. Create filter/Inteceptor and inject into them?

-----Original Message-----
From: Andy Law [mailto:andy.law@roslin.ed.ac.uk] 
Sent: 12 May 2010 09:58
To: user@struts.apache.org
Subject: Some Spring/Struts questions


All,

Apologies if this would be better asked in a Spring forum. If so, I
would
appreciate guidance as to which one/where.

I have a Struts2 application with around 30 actions. Most inherit from a
common base class. We have configured the system to start to use Spring
to
inject certain things into certain places. This seems to work well and
we
are all currently happy bunnies.

A new requirement means that we need to have available across pretty
much
every action a separate, independent bean that carries information that
may
(or may not) need to be displayed on the resulting web-pages. I am
wondering
what the best way is to make this bean available to each JSP.

I'm happy with injecting certain beans into certain actions, but I'm not
clear what I should be injecting *this* bean into. Ideally it should
just
pop into existence and be available to the JSPs without the Actions even
being aware that it is there. It's not part of the business logic and is
purely informational. If I have to make it available through the Actions
then I would *like* to be able to inject it into the common base class
with
a single configuration and have that work automatically for all
sub-classes
(and this would be a useful trick for some other places in the
application)
but that does not seem to be possible unless I'm missing a trick?

How should I configure this, please?

Later,

Andy
-- 
View this message in context:
http://old.nabble.com/Some-Spring-Struts-questions-tp28533505p28533505.h
tml
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
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