You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Eric Lentz <er...@outfastsource.com> on 2012/11/30 14:42:37 UTC

Best way to add to ValueStack in an Interceptor

Inside of an interceptor, I'd like to add a value onto the the ValueStack.
What is the best way to do this?

I tried this:
invocation.getStack().set("myValueStackName", theValueIwantForThisName);

But that pushes that value to the top of the stack, pushing my action class
down which becomes problematic for values getting assigned to the action
class. For example, a JSP field with name="fooId" and an action class with
a field of Long fooId. When I have the above code, fooId doesn't get
assigned. When I don't, it works as you would expect.

I guess my interceptor could be later in the stack of interceptors, after
the ParametersInterceptor, but that seems fragile. Some other developer
could come along and move it and now there are bugs. Plus, I'd prefer this
particular interceptor be at or near the beginning of the list of
interceptors.

So, how do I assign, but leave the action class at the top of the stack? I
could pop the action, add my value and then add the action back on, but is
there a better way?

- Eric

Re: Best way to add to ValueStack in an Interceptor

Posted by Maurizio Cucchiara <mc...@apache.org>.
Did you try to put the value in the context?

invocation.getStack().getContext().put("myValueStackName",
> theValueIwantForThisName);


Twitter     :http://www.twitter.com/m_cucchiara
G+          :https://plus.google.com/107903711540963855921
Linkedin    :http://www.linkedin.com/in/mauriziocucchiara
VisualizeMe: http://vizualize.me/maurizio.cucchiara?r=maurizio.cucchiara

Maurizio Cucchiara


On 30 November 2012 14:42, Eric Lentz <er...@outfastsource.com> wrote:

> Inside of an interceptor, I'd like to add a value onto the the ValueStack.
> What is the best way to do this?
>
> I tried this:
> invocation.getStack().set("myValueStackName", theValueIwantForThisName);
>
> But that pushes that value to the top of the stack, pushing my action class
> down which becomes problematic for values getting assigned to the action
> class. For example, a JSP field with name="fooId" and an action class with
> a field of Long fooId. When I have the above code, fooId doesn't get
> assigned. When I don't, it works as you would expect.
>
> I guess my interceptor could be later in the stack of interceptors, after
> the ParametersInterceptor, but that seems fragile. Some other developer
> could come along and move it and now there are bugs. Plus, I'd prefer this
> particular interceptor be at or near the beginning of the list of
> interceptors.
>
> So, how do I assign, but leave the action class at the top of the stack? I
> could pop the action, add my value and then add the action back on, but is
> there a better way?
>
> - Eric
>

Re: Best way to add to ValueStack in an Interceptor

Posted by Eric Lentz <er...@outfastsource.com>.
Good call Chris! I didn't realize that. The context solution Maurizio
offered now works for me. Pros/cons to both
approaches. The context approach could be confusing to the uninitiated on
the UI side. Pop/push could be confusing or mysterious at the interceptor
level (again, for the casual Struts coder). The context solution will be
the quickest for me (already done), so I guess that's the winner of the
coin toss.

Thanks again everyone!


On Fri, Nov 30, 2012 at 2:15 PM, Chris Pratt <th...@gmail.com>wrote:

> Remember, when you use Maurizio's way, you have to reference the values as
> context, not stack variables. So instead of using %{myvar} you have to use
> %{#myvar).
>   (*Chris*)
>
>
> On Fri, Nov 30, 2012 at 10:42 AM, Eric Lentz <ericlentz@outfastsource.com
> >wrote:
>
> > Maurizio, tried your way and no joy. Looks like I'll go with Łukasz' way.
> >
> > Thanks everyone for your consideration and help!
> >
> > On Fri, Nov 30, 2012 at 1:31 PM, Lukasz Lenart <lukaszlenart@apache.org
> > >wrote:
> >
> > > 2012/11/30 Eric Lentz <er...@outfastsource.com>:
> > > > Inside of an interceptor, I'd like to add a value onto the the
> > > ValueStack.
> > > > What is the best way to do this?
> > > >
> > > > I tried this:
> > > > invocation.getStack().set("myValueStackName",
> > theValueIwantForThisName);
> > > >
> > > > But that pushes that value to the top of the stack, pushing my action
> > > class
> > > > down which becomes problematic for values getting assigned to the
> > action
> > > > class. For example, a JSP field with name="fooId" and an action class
> > > with
> > > > a field of Long fooId. When I have the above code, fooId doesn't get
> > > > assigned. When I don't, it works as you would expect.
> > >
> > > use stack.pop() and stack.push()
> > >
> > > > I guess my interceptor could be later in the stack of interceptors,
> > after
> > > > the ParametersInterceptor, but that seems fragile. Some other
> developer
> > > > could come along and move it and now there are bugs. Plus, I'd prefer
> > > this
> > > > particular interceptor be at or near the beginning of the list of
> > > > interceptors.
> > > >
> > > > So, how do I assign, but leave the action class at the top of the
> > stack?
> > > I
> > > > could pop the action, add my value and then add the action back on,
> but
> > > is
> > > > there a better way?
> > >
> > > No, this is the preferred way. And please keep in mind the stack will
> > > be searched from top to down for given value name.
> > >
> > >
> > > Regards
> > > --
> > > Łukasz
> > > + 48 606 323 122 http://www.lenart.org.pl/
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> > >
> >
>

Re: Best way to add to ValueStack in an Interceptor

Posted by Chris Pratt <th...@gmail.com>.
Remember, when you use Maurizio's way, you have to reference the values as
context, not stack variables. So instead of using %{myvar} you have to use
%{#myvar).
  (*Chris*)


On Fri, Nov 30, 2012 at 10:42 AM, Eric Lentz <er...@outfastsource.com>wrote:

> Maurizio, tried your way and no joy. Looks like I'll go with Łukasz' way.
>
> Thanks everyone for your consideration and help!
>
> On Fri, Nov 30, 2012 at 1:31 PM, Lukasz Lenart <lukaszlenart@apache.org
> >wrote:
>
> > 2012/11/30 Eric Lentz <er...@outfastsource.com>:
> > > Inside of an interceptor, I'd like to add a value onto the the
> > ValueStack.
> > > What is the best way to do this?
> > >
> > > I tried this:
> > > invocation.getStack().set("myValueStackName",
> theValueIwantForThisName);
> > >
> > > But that pushes that value to the top of the stack, pushing my action
> > class
> > > down which becomes problematic for values getting assigned to the
> action
> > > class. For example, a JSP field with name="fooId" and an action class
> > with
> > > a field of Long fooId. When I have the above code, fooId doesn't get
> > > assigned. When I don't, it works as you would expect.
> >
> > use stack.pop() and stack.push()
> >
> > > I guess my interceptor could be later in the stack of interceptors,
> after
> > > the ParametersInterceptor, but that seems fragile. Some other developer
> > > could come along and move it and now there are bugs. Plus, I'd prefer
> > this
> > > particular interceptor be at or near the beginning of the list of
> > > interceptors.
> > >
> > > So, how do I assign, but leave the action class at the top of the
> stack?
> > I
> > > could pop the action, add my value and then add the action back on, but
> > is
> > > there a better way?
> >
> > No, this is the preferred way. And please keep in mind the stack will
> > be searched from top to down for given value name.
> >
> >
> > Regards
> > --
> > Łukasz
> > + 48 606 323 122 http://www.lenart.org.pl/
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>

Re: Best way to add to ValueStack in an Interceptor

Posted by Eric Lentz <er...@outfastsource.com>.
Maurizio, tried your way and no joy. Looks like I'll go with Łukasz' way.

Thanks everyone for your consideration and help!

On Fri, Nov 30, 2012 at 1:31 PM, Lukasz Lenart <lu...@apache.org>wrote:

> 2012/11/30 Eric Lentz <er...@outfastsource.com>:
> > Inside of an interceptor, I'd like to add a value onto the the
> ValueStack.
> > What is the best way to do this?
> >
> > I tried this:
> > invocation.getStack().set("myValueStackName", theValueIwantForThisName);
> >
> > But that pushes that value to the top of the stack, pushing my action
> class
> > down which becomes problematic for values getting assigned to the action
> > class. For example, a JSP field with name="fooId" and an action class
> with
> > a field of Long fooId. When I have the above code, fooId doesn't get
> > assigned. When I don't, it works as you would expect.
>
> use stack.pop() and stack.push()
>
> > I guess my interceptor could be later in the stack of interceptors, after
> > the ParametersInterceptor, but that seems fragile. Some other developer
> > could come along and move it and now there are bugs. Plus, I'd prefer
> this
> > particular interceptor be at or near the beginning of the list of
> > interceptors.
> >
> > So, how do I assign, but leave the action class at the top of the stack?
> I
> > could pop the action, add my value and then add the action back on, but
> is
> > there a better way?
>
> No, this is the preferred way. And please keep in mind the stack will
> be searched from top to down for given value name.
>
>
> Regards
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Best way to add to ValueStack in an Interceptor

Posted by Lukasz Lenart <lu...@apache.org>.
2012/11/30 Eric Lentz <er...@outfastsource.com>:
> Inside of an interceptor, I'd like to add a value onto the the ValueStack.
> What is the best way to do this?
>
> I tried this:
> invocation.getStack().set("myValueStackName", theValueIwantForThisName);
>
> But that pushes that value to the top of the stack, pushing my action class
> down which becomes problematic for values getting assigned to the action
> class. For example, a JSP field with name="fooId" and an action class with
> a field of Long fooId. When I have the above code, fooId doesn't get
> assigned. When I don't, it works as you would expect.

use stack.pop() and stack.push()

> I guess my interceptor could be later in the stack of interceptors, after
> the ParametersInterceptor, but that seems fragile. Some other developer
> could come along and move it and now there are bugs. Plus, I'd prefer this
> particular interceptor be at or near the beginning of the list of
> interceptors.
>
> So, how do I assign, but leave the action class at the top of the stack? I
> could pop the action, add my value and then add the action back on, but is
> there a better way?

No, this is the preferred way. And please keep in mind the stack will
be searched from top to down for given value name.


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

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