aspose file tools*
The moose likes Struts and the fly likes parameter and scope interceptor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "parameter and scope interceptor" Watch "parameter and scope interceptor" New topic
Author

parameter and scope interceptor

Paul Paulsen
Greenhorn

Joined: Jul 27, 2010
Posts: 7
Hi,
I have a short question about the order of this two interceptor.

Why I'm asking:
I want to save a value in the users session, but he should be able to change it. I'm using parameter and scope interceptors (from the default stack)
My problem is, that the value from the session is set after the value was set from the parameters. So its always returning to the default value.
When I change the order of this two interceptor, it works just fine.

The question:
Why is the scope interceptor after the parameter interceptor (in the default stack), is there any good reason?



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Without knowing how you're implementing this, it's difficult to provide any real feedback. Normally if the user is changing application state it would happen during a form submission and be processed by an action, not an interceptor.
Paul Paulsen
Greenhorn

Joined: Jul 27, 2010
Posts: 7
Its a simple implementation:
One action with the getter/setter for the value, a jsp with a form and a textbox to set the value.

the user submits the form,
the parameter interceptor grabs the data from the request and set its using the setter, the session interceptor takes the data from the session and replaces it.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

I know what the interceptors do.

I don't recall the scope interceptor being in *any* of the pre-defined stacks, only scopedModelDriven--what default stack are you talking about? What version of S2 are you using?
Paul Paulsen
Greenhorn

Joined: Jul 27, 2010
Posts: 7
Hmm, you are right, no scope in defaultstack. but who is setting the value?
In my example I added the
@InterceptorRef(value = "defaultStack", params = { "scope.session", "myValue" })
annotation to my action.

The stacktrace says the call comes from the ScopeInterceptor class.
Is struts automatically adding the scope interceptor when i add this parameter? (nice feature if he does )
This would explain why its in the end
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Doubt it; do you have any config files?
Paul Paulsen
Greenhorn

Joined: Jul 27, 2010
Posts: 7
just the struts.properties file, but just containing
"struts.ui.theme=simple"
thats all

Im using struts 2.1.8.1
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Then how are you changing the order of the interceptors?
Paul Paulsen
Greenhorn

Joined: Jul 27, 2010
Posts: 7
I just changed the annotation to

This way its works (first setting from session, than from parameters)

When i just do

it seems struts adds the scope interceptor automatically (at the end of the stack). But i can't find any documentation about this behavior.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I really don't believe it would magically add the scope interceptor, although it's possible I'm wrong. Turn up the logging level to DEBUG and post the stack trace; it'll show up if it's in the stack.

Besides: if you add the "scope" interceptor manually in front of the "defaultStack", any "scope" interceptor in the "defaultStack" would still execute as it did before, thus recreating the original behavior, no?
Paul Paulsen
Greenhorn

Joined: Jul 27, 2010
Posts: 7
here's the debug msg when using
@InterceptorRefs(value = {
@InterceptorRef(value = "defaultStack", params = { "scope.session", "myValue" })
})

...
2010-07-28 13:02:22,500 DEBUG com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.debug:57 - Setting static parameters {}
2010-07-28 13:02:22,500 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:57 - Setting params NONE
2010-07-28 13:02:22,500 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:57 - Setting params myValue => [ test ]
...
2010-07-28 13:02:22,524 DEBUG org.apache.struts2.interceptor.ScopeInterceptor.debug:57 - scope interceptor before
2010-07-28 13:02:22,524 DEBUG org.apache.struts2.interceptor.ScopeInterceptor.debug:57 - session scoped variable set myValue = [default]
...

Maybe he just checks the params I set, finds that there is no target for "scope.session", and adds the scope interceptor.
And when I add the Scope interceptor by hand, there is no need for struts to add it, because the params will have a target.

Struts is just a smart little feller
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's possible, but I find it unlikely.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: parameter and scope interceptor