Parameters without mutators and accessories (setters / getters) along with interceptor parameters in Struts 2

In the next class of actions, I use a parameter hook.

@Namespace("/admin_side") @ResultPath("/WEB-INF/content") @ParentPackage(value = "struts-default") @InterceptorRefs(@InterceptorRef(value="store", params={"operationMode", "AUTOMATIC"})) public final class TestAction extends ActionSupport implements Serializable, ValidationAware, Preparable { private static final long serialVersionUID = 1L; private String param1; private String param2; //Getters and setters. public TestAction() {} @Action(value = "TestMessage", results = { @Result(name=ActionSupport.SUCCESS, type="redirectAction", params={"namespace", "/admin_side", "actionName", "Test"}), @Result(name = ActionSupport.INPUT, location = "Test.jsp")}, interceptorRefs={ @InterceptorRef(value="paramsPrepareParamsStack", params={"params.acceptParamNames", "param1, param2", "params.excludeParams", "extraParam", "validation.validateAnnotatedMethodOnly", "true"}) }) public String insert() { // Do something. Add or update a row to the database (one at a time). addActionMessage("Action message"); addActionError("Error message"); return ActionSupport.SUCCESS; } @Action(value = "Test", results = { @Result(name = ActionSupport.SUCCESS, location = "Test.jsp"), @Result(name = ActionSupport.INPUT, location = "Test.jsp")}, interceptorRefs = { @InterceptorRef(value = "paramsPrepareParamsStack", params = {"params.acceptParamNames", "param1, param2", "params.excludeParams", "extraParam", "validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})}) public String load() throws Exception { // This method is just required to return an initial view on page load. return ActionSupport.SUCCESS; } @Override public void prepare() throws Exception {} } 

Below is <s:form> :

 <s:form namespace="/admin_side" action="Test" validate="true" id="dataForm" name="dataForm"> <s:if test="hasActionMessages()"> <s:actionmessage theme="jquery"/> </s:if> <s:if test="hasActionErrors()"> <s:actionerror theme="jquery"/> </s:if> <s:hidden name="param1"/> <s:hidden name="param2"/> <s:hidden name="extraParam"/> <s:submit value="Submit" action="TestMessage"/> </s:form> 

Here the hidden field of the extraParam form extraParam not declared and therefore does not have a setter and getter in the action class.

In this case, the following message appears on the server terminal:

SEVERE: developer notification (set struts.devMode to false to disable this message): An unexpected exception caught setting "extraParam" to 'class actions.TestAction: error setting expression' extraParam 'with value [' ',]

params.excludeParams does not exclude the extraParam parameter, as in the action class.

Is it possible to somehow prevent such exceptions when using interceptor parameters. Such messages are unnecessarily added to the action messages and are displayed via <s:actionmessage/> if they are used when they should not be displayed at all.


If this paramsPrepareParamsStack replaced with defaultStack in the action class, then such messages are not displayed. He simply warns the following.

A WARNING. The [extraParam] parameter is in the excludeParams list. patterns!

Please do not just say, set struts.devMode to false to disable such messages.

+7
jsp struts2 struts2-interceptors interceptor
source share
3 answers

I already said in a comment that interceptor parameters are not inherited by the parent packet interceptor configurations if you define your own set of parameters that override the default settings. See Intercepting Interceptor Parameters .

There are also some methods used to get two different maps of interceptor parameters; see Getting Interceptor Parameters in Struts2 .

The convention plugin creates XWork configuration packages inherited from some parent package. See My Answer for Struts 2 Convention Platform Define Multiple Parent Packages .

So all you have to do is to override the default parameter set by the parent configuration if you want to add your own parameters to the set. The interceptor tag or the interceptor-stack tag, and you must do this for each interceptor-ref tag.

The agreement plugin uses the @InterceprorRef annotation for the same purpose, but with a caveat, which, if applied to a class, applies to every action of this class. Therefore, be careful when using this annotation at the class level. You override the parameters of the interceptor stack, so for each parameter name you should use a prefix followed by the interceptor name dot indicated on the stack, but this only works if you have unique interceptor-ref names on the stack.

If you have two params interceptor links in paramsPrepareParamsStack , you cannot override the second params interceptor-ref unless you created your own interceptor stack and did not specify parameter overrides for each interceptor link.

+2
source share

Let's take a look at paramsPrepareParamsStack :

  <interceptor-stack name="paramsPrepareParamsStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="i18n"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="multiselect"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param> </interceptor-ref> <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> <interceptor-ref name="chain"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack> 

There are 2 params interceptors. When you set the excludeParams parameter to your action class, this is probably set for the first params interceptor parameter - it remains the default for the second interceptor. Now, when the second params interceptor is called (with excludeParams by default), this exception is thrown.

You can try duplicating the setting of the excludeParams parameter to set it also for the second interceptor:

  @InterceptorRef(value = "paramsPrepareParamsStack", params = {"params.acceptParamNames", "param1, param2", "params.excludeParams", "extraParam", "params.excludeParams", "extraParam", "validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})}) 
+1
source share

I do not say for sure

sample code as follows:

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

Perhaps this does not work, follow this link:

+1
source share

All Articles