Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Info

The example code for this tutorial, exclude_parameters, is available for checkout at https://svngithub.com/apache.org/repos/asf/struts/sandbox/trunk/struts2examples/Image Removedstruts-examples.

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
html
html
titleStruts 2 Form Tags


<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
HTMLplainHTML
plain
titleLog Messages


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
XMLxmlXML
xml
titleSetup Interceptor Stack To Exclude submit Parameter



<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" />

...