The native Struts2 solution would be to use an interceptor, or to
piggy back on an interceptor that already exists. Writing your own
interceptor is not difficult

 * http://struts.apache.org/2.x/docs/writing-interceptors.html

and I would suggest that everyone at least try writing one interceptor once :)

Another way to go is to piggy-back on an existing inceptor, like
SessionAware or ModelDriven. The interfaces describe a setter or a
getter. When that method is called, it becomes an event, so you can
call your own singleton or what-have-you to acquire the bean as save
it to the Action.

For example, here's a SessionAware setter that exposes a User object
stored in the session as a property.

      @SuppressWarnings("unchecked")
        public void setSession(Map value) {
        session = value;
        // setUser when session available
        Object object = value.get(USER_KEY);
        if (object==null) {
                User myUser = new MyMemoryUser();
                setUser(myUser);
                setTask(INSERT);
        } else {
                setUser((User) object);
                setTask(UPDATE);
        }               
    }

In turn, the ModelDriven getter can call getUser:

        public Object getModel() {
                return getUser();
        }

If SessonAware wasn't locating the user property, we could inject some
could here to acquire and cache the property. (Though doing it advance
is a much better approach!)

We could really use a good description of ModelDriven on the website.
For now, the best resource would be Ian's Getting Started or WebWork
in Action.


> I wish there were some more books out there on Struts2 ... I'd
> read them first before asking here.

Today, WebWork in Action is still the best Struts 2 book available!
But that will start to change as Kurniawan's book and Roughley's new
books ship this month and next. Anyone who is interested in Struts 2
books should pre-order one or both of these now, to encourage other
authors.

 * http://struts.apache.org/2.x/docs/home.html#Home-Books

-- HTH, Ted
<http://www.husted.com/ted/blog/>
Meet me at ApacheCon US 2007:
 * http://us.apachecon.com/us2007/program/talk/1883

On 10/2/07, Tom Holmes Jr. <[EMAIL PROTECTED]> wrote:
> I've been playing with Struts2+Spring2 beans+Hibernate and that all
> works very well.
>
> So, now I am trying something simpler ... so consider this a new
> web-project altogether with just Struts2 ... no Spring2 and no Hibernate.
> Basically, I want to call a pre-Action action that loads a bean into a
> session and then calls a JSP page ... simple right?
>
> So, I am presuming that in the struts.xml file I have an action like:
> <action name="start" method="execute" class="com.me.path.me.again.MyAction">
>     <result name="success">/mypage.jsp</result>
>     <result name="error">/myerror.jsp</result>
> </action>
>
> Is that all I need?   Is there anything else?   Can you send me some
> links for examples?
>
> God!   I wish there were some more books out there on Struts2 ... I'd
> read them first before asking here.
> And yes, I tried getting information from Google first.
>
> Thanks very much for all the help!
>                                                                      Tom

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

Reply via email to