You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Akash Shah (Jira)" <ji...@apache.org> on 2020/11/16 18:05:00 UTC

[jira] [Comment Edited] (WW-5096) Struts2 StaticParametersInterceptor's addParametersToContext method is not working as expected.

    [ https://issues.apache.org/jira/browse/WW-5096?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17232967#comment-17232967 ] 

Akash Shah edited comment on WW-5096 at 11/16/20, 6:04 PM:
-----------------------------------------------------------

I encountered this bug while I was upgrading my application from struts 2.3.35 to 2.5.22. Please refer below details for scenario.

In our application , I have one JSP page which allows user to search student details by different search criteria like student_name, student_rollno etc..

I have below code in struts.xml file for action mapping to achieve this functionality. 
{code:java}
<action name="studentSearch" class="fn.college.action.StudentSearchAction" method="getStudentInfo">
 <result>/WEB-INF/pages/studentResultPage.jsp</result>
 <result name="index">/WEB-INF/pages/index.jsp</result>
 <result name="preRegisteredAccountSearchPage" type="redirectAction">preRegisteredAccountSearchPage</result>
 <result name="studentviewpage">/WEB-INF/pages/studentViewPage.jsp</result>
 <result name="studentresultpage">/WEB-INF/pages/studentResultPage.jsp</result>
 <result name="studentDetail" type="redirectAction">${redirectActionUrl}</result>
</action>

<action name="studentDetail" class="fn.college.action.StudentDetailAction">
 <interceptor-ref name="abcStack" />
 <param name="rollno">${rollno}</param>
 <result>/WEB-INF/pages/studentViewPage.jsp</result>
 <result name="showPreRegisteredAccount" type="redirectAction">showPreRegisteredAccount?studentId=${studentId}</result>
 <result name="error">/WEB-INF/pages/index.jsp</result>
</action>

<interceptor-stack name="abcStack">
<interceptor-ref name="servletConfig" />
<interceptor-ref name="i18n" />
<interceptor-ref name="prepare" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="params" >
<param name="excludeParams">(?i)^session\..*,^struts\..*</param>
</interceptor-ref>
</interceptor-stack>
{code}
So when user search student details by student name then "getStudentInfo" of "StudentSearchAction" will be invoked and after processing business logic have below code to build url and set it redirect action url property. 
{code:java}
url = new StringBuilder("studentDetail?rollno=12345");
setRedirectActionUrl(url.toString());
return "studentDetail";
{code}
so as per above action mapping, StudentDetailAction will be invoked because of above redirection url and in that execute method , when tried to access code "student.getRollno()" it gave value "${rollno}" instead it should give 12345 value. 

Please note that same code was working fine in struts 2.3.35 but it's not working in struts 2.5.22 version. So After debugging, I found that in StaticParametersInterceptor's "addParametersToContext" method new params value is "rollno=${rollno}" and previousParams value is "rollno=12345". since, we have not given overwrite parameter value hence it should be by default false and unfortunately addParametersToContext behaves the same irrespective of overwrite parameter.


was (Author: akash.shah):
I encountered this bug while I was upgrading my application from struts 2.3.35 to 2.5.22. Please refer below details for scenario.

In our application , I have one JSP page which allows user to search student details by different search criteria like student_name, student_rollno etc..

I have below code in struts.xml file for action mapping to achieve this functionality. 

 
{code:java}
<action name="studentSearch" class="fn.college.action.StudentSearchAction" method="getStudentInfo">
 <result>/WEB-INF/pages/studentResultPage.jsp</result>
 <result name="index">/WEB-INF/pages/index.jsp</result>
 <result name="preRegisteredAccountSearchPage" type="redirectAction">preRegisteredAccountSearchPage</result>
 <result name="studentviewpage">/WEB-INF/pages/studentViewPage.jsp</result>
 <result name="studentresultpage">/WEB-INF/pages/studentResultPage.jsp</result>
 <result name="studentDetail" type="redirectAction">${redirectActionUrl}</result>
</action>

<action name="studentDetail" class="fn.college.action.StudentDetailAction">
 <interceptor-ref name="abcStack" />
 <param name="rollno">${rollno}</param>
 <result>/WEB-INF/pages/studentViewPage.jsp</result>
 <result name="showPreRegisteredAccount" type="redirectAction">showPreRegisteredAccount?studentId=${studentId}</result>
 <result name="error">/WEB-INF/pages/index.jsp</result>
</action>

<interceptor-stack name="abcStack">
<interceptor-ref name="servletConfig" />
<interceptor-ref name="i18n" />
<interceptor-ref name="prepare" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="params" >
<param name="excludeParams">(?i)^session\..*,^struts\..*</param>
</interceptor-ref>
</interceptor-stack>
{code}
So when user search student details by student name then "getStudentInfo" of "StudentSearchAction" will be invoked and after processing business logic have below code to build url and set it redirect action url property.

 
{code:java}
url = new StringBuilder("studentDetail?rollno=12345");
setRedirectActionUrl(url.toString());
return "studentDetail";
{code}
so as per above action mapping, StudentDetailAction will be invoked because of above redirection url and in that execute method , when tried to access code "student.getRollno()" it gave value "${rollno}" instead it should give 12345 value. 

Please note that same code was working fine in struts 2.3.35 but it's not working in struts 2.5.22 version. So After debugging, I found that in StaticParametersInterceptor's "addParametersToContext" method new params value is "rollno=${rollno}" and previousParams value is "rollno=12345". since, we have not given overwrite parameter value hence it should be by default false and unfortunately addParametersToContext behaves the same irrespective of overwrite parameter.

> Struts2 StaticParametersInterceptor's addParametersToContext method is not working as expected.
> -----------------------------------------------------------------------------------------------
>
>                 Key: WW-5096
>                 URL: https://issues.apache.org/jira/browse/WW-5096
>             Project: Struts 2
>          Issue Type: Bug
>            Reporter: Akash Shah
>            Priority: Minor
>             Fix For: 2.5.26, 2.6
>
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> In our current project we are upgrading struts 2.3.35 to 2.5.22 and we encounter some minor bug in below code of addParametersToContext method of StaticParametersInterceptor.
> in method, code is like below.
>  
> {code:java}
> if (overwrite) {
>             if (previousParams != null) {
>                 combinedParams = combinedParams.withParent(previousParams);
>             }
>             if (newParams != null) {
>                 combinedParams = combinedParams.withExtraParams(newParams);
>             }
>         } else {
>             if (newParams != null) {
>                 combinedParams = combinedParams.withExtraParams(newParams);
>             }
>             if (previousParams != null) {
>                 combinedParams = combinedParams.withParent(previousParams);
>             }
>         }
> {code}
>  
> But here overwrite functionality will not work since only if condition order was changed in else condition of overwrite.
> instead of this, as per me, code would be like below
> {code:java}
> if (overwrite) {
>             if (previousParams != null) {
>                 combinedParams = combinedParams.withParent(previousParams);
>             }
>             if (newParams != null) {
>                 combinedParams = combinedParams.withExtraParams(newParams);
>             }
>         } else {
>             if (newParams != null) {  
>              HttpParameters newHttpParameters = HttpParameters.create(newParams).build();
>                 combinedParams = combinedParams.withParent(newParams);
>             }
>             if (previousParams != null) {
>                 combinedParams = combinedParams.withExtraParams(previousParams);
>             }
>         }
> {code}
>  
> And that's how ac.setParameters(combinedParams.build()); will work because in build method, requestParameterMap will override parent parameters.
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)