You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Burton Rhodes <bu...@gmail.com> on 2017/10/01 03:16:07 UTC

Re: How to put Spring object in Struts application scope on web startup?

Nice - I'll take a closer look.  That seems like a decent solution


On Sat, Sep 30, 2017 at 4:22 AM, Yasser Zamani <ya...@live.com>
wrote:

> Hello Burton,
>
> I tested following and seems works but I did not tested if objects are
> equal by reference (you may get different objects e.g. when scope is
> prototype):
>
> ```java
> public class MYStrutsPrepareFilter implements Filter {
>
>
>         public void doFilter(ServletRequest req, ServletResponse res,
> FilterChain chain)
>                         throws IOException, ServletException {
>
>                 ActionContext actionContext = ActionContext.getContext();
>                 if(null != actionContext) {
>                         ValueStack stack = actionContext.getValueStack();
>                         StrutsSpringObjectFactory objectFactory =
> (StrutsSpringObjectFactory)
> actionContext.getInstance(ObjectFactory.class);
>                         try {
>                                 stack.setValue("#application['
> MYSpringObject']",
> objectFactory.buildBean("myBeanName",null));
>                         } catch (Exception e) {
>                                 e.printStackTrace();
>                         }
>                 }
>                 chain.doFilter(req, res);
>         }
> }
> ```
>
> ```xml
>      <filter>
>          <filter-name>struts2prepare</filter-name>
>          <filter-class>...StrutsPrepareFilter</filter-class>
>      </filter>
>      <filter>
>          <filter-name>MYStrutsPrepareFilter</filter-name>
>          <filter-class>MYStrutsPrepareFilter</filter-class>
>      </filter>
>      <filter>
>          <filter-name>struts2execute</filter-name>
>          <filter-class>...StrutsExecuteFilter</filter-class>
>      </filter>
>      <filter-mapping>
>          <filter-name>struts2prepare</filter-name>
>          <url-pattern>/*</url-pattern>
>      </filter-mapping>
>      <filter-mapping>
>          <filter-name>MYStrutsPrepareFilter</filter-name>
>          <url-pattern>/*</url-pattern>
>      </filter-mapping>
>      <filter-mapping>
>          <filter-name>struts2execute</filter-name>
>          <url-pattern>/*</url-pattern>
>      </filter-mapping>
> ```
>
> ```jsp
> <s:property value="#application.MYSpringObject.myProperty" />
> ```
>
> Hope this helps!
> Yasser.
>
> On 9/30/2017 1:29 AM, Burton Rhodes wrote:
> > I have all of these settings in place, but your comments spurred the idea
> > to have a "BaseAction" class where I autowire my object
> > (MyApplicationSettings.java) and then extend all of my Actions from this
> > class.
> >
> > Just for my info though... My original line of thinking was to be able to
> > access the object with the following:
> >
> > Map application = (Map) ActionContext.getContext().get("application");
> > application.get("myApplicationSettings");
> > ---or---
> > <s:property value="#application.myApplicatoinSettings" />
> >
> > I just couldn't figure out a way to put the object in the struts
> > application scope within a listener during startup.  Does that make
> sense?
> >
> > On Fri, Sep 29, 2017 at 11:50 AM, Adam Brin <ab...@digitalantiquity.org>
> > wrote:
> >
> >> it’s a mixture of things:
> >>
> >> * Struts.xml should be setup to know about spring:
> >>      <constant name="struts.objectFactory"
> >>          value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
> >>      <constant name="struts.objectFactory.
> spring.autoWire.alwaysRespect"
> >> value="true" />
> >>      <constant name="struts.objectFactory.spring.autoWire"
> value="name" />
> >>
> >> * register the appropriate application listeners
> >>
> >> * add the @Scope variables  to controllers
> >>
> >> --
> >> _________________________________________________________
> >> Adam Brin
> >> Director of Technology, Digital Antiquity
> >> 480.965.1278
> >>
> >>> On Sep 29, 2017, at 9:03 AM, Burton Rhodes <bu...@gmail.com>
> >> wrote:
> >>>
> >>> How do you put a Spring object into the Struts application scope Map on
> >>> startup of a web application?  I have 2 listeners that I thought would
> be
> >>> the usual suspects, but have not had success yet.
> >>>
> >>> I have a ServletContextListener class but on the
> >>> contextInitialized(ServletContextEvent event) method, my spring bean
> has
> >>> not been created yet.
> >>>
> >>> Secondly, I have a Spring "ApplicationListener", but in the
> >>> onApplicationEvent(ApplicationEvent event) method,
> >>> "ServletActionContext.getContext()" is null.  How is this
> traditionally
> >>> accomplished?
> >>>
> >>> Thanks!
> >>> Burton
> >>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

Re: How to put Spring object in Struts application scope on web startup?

Posted by Lukasz Lenart <lu...@apache.org>.
Do you have any other idea how it's supposed to be solved? An
interface with a struts.xml entry to bridge Spring bean with a Struts
scope?


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

2017-10-01 5:16 GMT+02:00 Burton Rhodes <bu...@gmail.com>:
> Nice - I'll take a closer look.  That seems like a decent solution
>
>
> On Sat, Sep 30, 2017 at 4:22 AM, Yasser Zamani <ya...@live.com>
> wrote:
>
>> Hello Burton,
>>
>> I tested following and seems works but I did not tested if objects are
>> equal by reference (you may get different objects e.g. when scope is
>> prototype):
>>
>> ```java
>> public class MYStrutsPrepareFilter implements Filter {
>>
>>
>>         public void doFilter(ServletRequest req, ServletResponse res,
>> FilterChain chain)
>>                         throws IOException, ServletException {
>>
>>                 ActionContext actionContext = ActionContext.getContext();
>>                 if(null != actionContext) {
>>                         ValueStack stack = actionContext.getValueStack();
>>                         StrutsSpringObjectFactory objectFactory =
>> (StrutsSpringObjectFactory)
>> actionContext.getInstance(ObjectFactory.class);
>>                         try {
>>                                 stack.setValue("#application['
>> MYSpringObject']",
>> objectFactory.buildBean("myBeanName",null));
>>                         } catch (Exception e) {
>>                                 e.printStackTrace();
>>                         }
>>                 }
>>                 chain.doFilter(req, res);
>>         }
>> }
>> ```
>>
>> ```xml
>>      <filter>
>>          <filter-name>struts2prepare</filter-name>
>>          <filter-class>...StrutsPrepareFilter</filter-class>
>>      </filter>
>>      <filter>
>>          <filter-name>MYStrutsPrepareFilter</filter-name>
>>          <filter-class>MYStrutsPrepareFilter</filter-class>
>>      </filter>
>>      <filter>
>>          <filter-name>struts2execute</filter-name>
>>          <filter-class>...StrutsExecuteFilter</filter-class>
>>      </filter>
>>      <filter-mapping>
>>          <filter-name>struts2prepare</filter-name>
>>          <url-pattern>/*</url-pattern>
>>      </filter-mapping>
>>      <filter-mapping>
>>          <filter-name>MYStrutsPrepareFilter</filter-name>
>>          <url-pattern>/*</url-pattern>
>>      </filter-mapping>
>>      <filter-mapping>
>>          <filter-name>struts2execute</filter-name>
>>          <url-pattern>/*</url-pattern>
>>      </filter-mapping>
>> ```
>>
>> ```jsp
>> <s:property value="#application.MYSpringObject.myProperty" />
>> ```
>>
>> Hope this helps!
>> Yasser.
>>
>> On 9/30/2017 1:29 AM, Burton Rhodes wrote:
>> > I have all of these settings in place, but your comments spurred the idea
>> > to have a "BaseAction" class where I autowire my object
>> > (MyApplicationSettings.java) and then extend all of my Actions from this
>> > class.
>> >
>> > Just for my info though... My original line of thinking was to be able to
>> > access the object with the following:
>> >
>> > Map application = (Map) ActionContext.getContext().get("application");
>> > application.get("myApplicationSettings");
>> > ---or---
>> > <s:property value="#application.myApplicatoinSettings" />
>> >
>> > I just couldn't figure out a way to put the object in the struts
>> > application scope within a listener during startup.  Does that make
>> sense?
>> >
>> > On Fri, Sep 29, 2017 at 11:50 AM, Adam Brin <ab...@digitalantiquity.org>
>> > wrote:
>> >
>> >> it’s a mixture of things:
>> >>
>> >> * Struts.xml should be setup to know about spring:
>> >>      <constant name="struts.objectFactory"
>> >>          value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
>> >>      <constant name="struts.objectFactory.
>> spring.autoWire.alwaysRespect"
>> >> value="true" />
>> >>      <constant name="struts.objectFactory.spring.autoWire"
>> value="name" />
>> >>
>> >> * register the appropriate application listeners
>> >>
>> >> * add the @Scope variables  to controllers
>> >>
>> >> --
>> >> _________________________________________________________
>> >> Adam Brin
>> >> Director of Technology, Digital Antiquity
>> >> 480.965.1278
>> >>
>> >>> On Sep 29, 2017, at 9:03 AM, Burton Rhodes <bu...@gmail.com>
>> >> wrote:
>> >>>
>> >>> How do you put a Spring object into the Struts application scope Map on
>> >>> startup of a web application?  I have 2 listeners that I thought would
>> be
>> >>> the usual suspects, but have not had success yet.
>> >>>
>> >>> I have a ServletContextListener class but on the
>> >>> contextInitialized(ServletContextEvent event) method, my spring bean
>> has
>> >>> not been created yet.
>> >>>
>> >>> Secondly, I have a Spring "ApplicationListener", but in the
>> >>> onApplicationEvent(ApplicationEvent event) method,
>> >>> "ServletActionContext.getContext()" is null.  How is this
>> traditionally
>> >>> accomplished?
>> >>>
>> >>> Thanks!
>> >>> Burton
>> >>
>> >>
>> >
>>
>> ---------------------------------------------------------------------
>> 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