java - Receiving unpredictable parameters in Struts2 interceptor -
i aiming write interceptor add headers in response. have following interceptor
public class cachinginterceptor extends abstractinterceptor{ @override public string intercept(actioninvocation ai) throws exception { httpservletresponse response = (httpservletresponse) getactioncontext(ai).get(strutsstatics.http_response); if(null != response) { response.setheader("cache-control","no-store,no-cache"); response.setheader("pragma","no-cache"); response.setheader("expires","-1"); } return ai.invoke(); } }
i need enhance in such way headers can defined in configuration file (struts.xml
)
.... <!-- define , add following interceptor in default interceptor stack --> <interceptor name="cachinginterceptor" class="demo.cachinginterceptor"> .... <action name="myaction" class="demo.myaction"> .... <param name="cache-control">no-store,no-cache</param> <param name="pragma">no-cache</param> <param name="expires">-1</param> .... </action>
now have define properties in interceptor class values headers
private string pragma; //with getter, setter private string expires; //with getter, setter
here have 2 problems.
1• cannot define property "cache-control" in java.
2• header names unpredictable, i.e. header can defined in configuration
<param name="other-header">some-value</param>
i have 2 questions:
- how can receive header in interceptor defined in struts2 configuration.
- is there better way thing?
with action configuration have defined several static parameters processed via staticparams
interceptor. interceptor should proceed first in stack. have retrieve them action context.
map<string, object> params = actioncontext.getcontext().getparameters(); response.setheader("cache-control", ((string[])params.get("cache-control"))[0]); response.setheader("pragma", ((string[])params.get("pragma"))[0]); response.setheader("expires", ((string[])params.get("expires"))[0]);
Comments
Post a Comment