问题
In order to take control of the parameters which an action accept you must:
Make your action implement ParameterNameAware
like:
public class sample implements ParameterNameAware(){
public boolean acceptableParameterName(String parameterName) {
if (("amount".equals(parameterName) ||
"sourceAccount".equals(parameterName) ||
"destinationAccount".equals(parameterName))
return true;
else
return false;
}
}
This method is called for the excluded properties of param
properties.
So, you need to configure params
interceptor to exclude all parameters, so the acceptableParameters
get the chance to be called by params
interceptor.
<interceptor-ref name="params">
<param name="excludeParams">\w+((\.\w+)|(\[\d+\])|(\(\d+\))|(\['\w+'\])|(\('\w+'\)))*</param>
</interceptor-ref>
Am I correct?! This seems some how strange for me (Excluding all parameters and then adding them in each action).
回答1:
The Struts2 params
interceptor behavior against accepting or declining parameters could be extended via the ParameterNameAware
action. All parameters are handled by this interceptor if they are accepted or ignored if they are excluded. The extension point checks for accepted parameters. If they are accepted by the interceptor then they are passed, nevertheless additional checks is performed by calling acceptableParameterName
but it useless if parameter is accepted by the interceptor. On the other hand if parameter is excluded by the interceptor then calling this method makes a power. Coexisting both methods looks strange because they are designed to be mutually exclusive.
来源:https://stackoverflow.com/questions/20254142/struts-2-acceptable-parameters-names