Info |
---|
The example code for this tutorial, Exclude_Params_Struts2_Mvnexclude_parameters, is available on Google Code - httpat https://codegithub.google.com/papache/struts2struts-examples/downloads/list. After downloading and unzipping the file, you'll have a folder named Exclude_Params_Struts2_Mvn. In that folder will be a README.txt file with instructions on now to build and run the example application. |
Introduction
When Struts development mode is set to true (also see Debugging Struts) the framework writes many informative messages to the log file. These messages include ones that indicate whether or not a specific parameter will be handled by the parameter interceptor and made available to the Action class. These log messages can be helpful in clearly identifying parameters that you do not want the parameter interceptor to process for security or other reasons. This article discusses how to exclude parameters from being handled by the parameter interceptor.
...
Consider this code which creates a form:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<s:form action="save" method="post">
<s:textfield key="personBean.firstName" />
<s:textfield key="personBean.lastName" />
<s:textfield key="personBean.email" />
<s:textfield key="personBean.phoneNumber" />
<s:select key="personBean.sport" list="sports" />
<s:radio key="personBean.gender" list="genders" />
<s:select key="personBean.residency" list="states" listKey="stateAbbr" listValue="stateName" />
<s:checkbox key="personBean.over21" />
<s:checkboxlist key="personBean.carModels" list="carModelsAvailable" />
<s:submit key="submit" />
</s:form>
|
The s:submit tag will create a submit button with a name of submit. Since the Action class probably doesn't have a setSubmit(String name) method you will see the following log messages (only if Struts development mode is set to true):
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
Dec 31, 2012 3:43:53 PM
com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: Parameter [submit] is not on the excludeParams list of patterns and will be appended to action!
Dec 31, 2012 3:43:53 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
SEVERE: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'submit' on 'class org.apache.struts.edit.action.EditAction: Error setting expression 'submit' with value ['Save Changes', ]
|
...
To exclude specific parameters from being processed by the Struts 2 framework you need to add those parameter names to the list of excluded parameters. One way to do this is by adding those parameter names to the collection of excludedParams for the Parameters interceptor. You can do this by modifying the Parameters interceptor in setting up the stack of interceptors used by your Struts 2 application. For example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<interceptors>
<interceptor-stack name="appDefault">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
<param name="params.excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*,submit</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="appDefault" />
|
...