简体   繁体   中英

Why can't a Struts 2 interceptor modify the 'result' page generated by subsequent interceptors?

I was reading Struts2 in Action , and it said that if an interceptor B fires after interceptor A , and B determines that control should not be allowed to go to Action (as there might be validation errors as done by DefaultWorkFLowInterceptor ). It then returns "input"..and the page is rendered to the user. The control then comes back to interceptor A , but A can't modify the result now, as the page has already been sent to the client.

But, since the interceptor B returns just a string, the interceptor A can simply return another string in its place, and the result changes. DefaultWorkFLowInterceptor returns just a string, it doesn't actually write anything to the response stream, so when control goes back to its preceding interceptors, why can't they change the input ?

Even if you change the result string, its too late since the result has already been rendered to the client (UI).

Interceptor calling occurs in reverse order so that any post processing work can be done like cleaning up any resources or writing any critical information.

If you are interested to change the result you can use PreResultListener .A PreResultListener can affect an action invocation between the interceptor/action phase and the result phase. Typical uses include switching to a different Result or somehow modifying the Result or Action objects before the Result executes.

For details refer to the document

I was under the impression that an interceptor calls another interceptor (and therefore, the string returned by an interceptor is received by its preceding one, which it can modify). Actually, an interceptor calls the invoke method of the ActionInvocation class which, in turn calls the next interceptor. This also means that the result string returned by an interceptor will be first received by ActionInvocation , which can actually render the response page to the client before passing control to the preceding interceptors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM