Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4775

Action class Attributes(value stack) is not getting populated through Ajax url request parms

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Not A Problem
    • Affects Version/s: 2.5.10
    • Fix Version/s: 2.5.12
    • Component/s: Value Stack
    • Labels:
      None

      Description

      Passing request attributes through Ajax - url is not populating the Action Form Attributes and coming up EMPTY . Instead they are available via request object currently.
      It was working correctly in struts 2.3.16 BUT it is NOT working with Struts 2.5.10 and posing big concern to upgrade to newer version which we want due to security risk.
      To better explain the issue, please check the below snippets. It is big part of our applications code and have a big impacts if we need to move to newer version.
      Below is sniipet of Ajax Method call to Action name:

      function callAjaxGet(value){
      $.ajax( {
      url : "strustSampleActionNameGet",
      type: "GET",
      data : 
      { param : "2"} 
      ,
      success : function(response)
      {alert('success');} 
      ,
      error : function(e)
      {alert('Message on error');} 
      });
      

      ---Action class snippet-----

      public class strustSampleActionClass extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware{
      private String param1;
      
      
      //getter and setter exist
      public string getParam1(){
      ...
      }
      
      public void setParam1(){
      ...
      }
      
      public String execute() throws exception
      { //getParam1 is null // but we can get it through request String parma1req= getRequest().getParameter("param1"); }
      

        Activity

        Hide
        lukaszlenart Lukasz Lenart added a comment - - edited

        Can you update the code snippet? Because right now I don't know if this is a not-working-hand-made example or an actual code from your project (which even won't compile).

        Show
        lukaszlenart Lukasz Lenart added a comment - - edited Can you update the code snippet? Because right now I don't know if this is a not-working-hand-made example or an actual code from your project (which even won't compile).
        Hide
        nitinperi nitin added a comment - - edited

        Thanks Lukasz for quick reply. I updated the the snippets below, please let me know if further information is need on this.

        Struts

        <action name="strustSampleActionNameGet" class="struts.action.strutsSampleActionClass">
              	<result name="success">/views/successPage.jsp</result> 
              	 <result name="error">/views/ErrorPage.jsp</result>     
               </action>  
        

        Call from jsp

        onclick="callAjaxGet();"
        

        Js using jquery

        function callAjaxGet(){ 
         $.ajax( { 
         url : "strustSampleActionNameGet", 
         type: "GET", 
         data : 
         { param : "2"} 
         , 
         success : function(response) 
         {alert('success');} 
         , 
         error : function(e) 
         {alert('Message on error');} 
         }); 
        

        Action

        public class strutsSampleActionClass extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware{ 
         private String param1; 
        
         //getter and setter exist 
         public string getParam1(){ 
        return param1; 
         } 
        
         public void setParam1(String param1){ 
        this.param1 = param1;
         } 
        
         public String execute() throws Exception{
        try { 
        // getParam1(); is returning null 
        String param1ValueStack = getParam1(); 
        
        //but we can get it through request Object
        String parma1req= getRequest().getParameter("param1");
        }
        catch(Exception e) {
                               return "error";
                }
              return "success";
        
        Show
        nitinperi nitin added a comment - - edited Thanks Lukasz for quick reply. I updated the the snippets below, please let me know if further information is need on this. Struts <action name= "strustSampleActionNameGet" class= "struts.action.strutsSampleActionClass" > <result name= "success" > /views/successPage.jsp </result> <result name= "error" > /views/ErrorPage.jsp </result> </action> Call from jsp onclick="callAjaxGet();" Js using jquery function callAjaxGet(){ $.ajax( { url : "strustSampleActionNameGet", type: "GET", data : { param : "2"} , success : function (response) {alert('success');} , error : function (e) {alert('Message on error');} }); Action public class strutsSampleActionClass extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware{ private String param1; //getter and setter exist public string getParam1(){ return param1; } public void setParam1( String param1){ this .param1 = param1; } public String execute() throws Exception{ try { // getParam1(); is returning null String param1ValueStack = getParam1(); //but we can get it through request Object String parma1req= getRequest().getParameter( "param1" ); } catch (Exception e) { return "error" ; } return "success" ;
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        There is a mismatch between param name in JavaScript - param - and param name in the Java code - param1

        param != param1
        
        Show
        lukaszlenart Lukasz Lenart added a comment - There is a mismatch between param name in JavaScript - param - and param name in the Java code - param1 param != param1
        Hide
        nitinperi nitin added a comment -

        please consider that as typing error, that would be same name param

        Show
        nitinperi nitin added a comment - please consider that as typing error, that would be same name param
        Hide
        nitinperi nitin added a comment - - edited

        resending the Action class with corrected param

        public class strutsSampleActionClass extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware{
        private String param;

        //getter and setter exist
        public string getParam()

        { return param; }

        public void setParam(String param)

        { this.param = param; }

        public String execute() throws Exception{
        try {
        // getParam(); is returning null
        String param1ValueStack = getParam();

        //but we can get it through request Object
        String parmareq= getRequest().getParameter("param");
        }
        catch(Exception e)

        { return "error"; }

        return "success";
        }

        Show
        nitinperi nitin added a comment - - edited resending the Action class with corrected param public class strutsSampleActionClass extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware{ private String param; //getter and setter exist public string getParam() { return param; } public void setParam(String param) { this.param = param; } public String execute() throws Exception{ try { // getParam(); is returning null String param1ValueStack = getParam(); //but we can get it through request Object String parmareq= getRequest().getParameter("param"); } catch(Exception e) { return "error"; } return "success"; }
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        so then it will work, you can pass the param via request, e.g /strustSampleActionNameGet?param1=test to test the action.

        Show
        lukaszlenart Lukasz Lenart added a comment - so then it will work, you can pass the param via request, e.g /strustSampleActionNameGet?param1=test to test the action.
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Please use edit functionality, posting the same code snippet multiple times won't help. And again there is a name mismatch - setParam1

        Show
        lukaszlenart Lukasz Lenart added a comment - Please use edit functionality, posting the same code snippet multiple times won't help. And again there is a name mismatch - setParam1
        Hide
        nitinperi nitin added a comment -

        tried the above , It still returning it empty for getParam(). edited the above snippet rename setParam1 to setParam

        Snippet in js
        function callAjaxGet(){
        $.ajax( {
        url : "strustSampleActionNameGet?param=2",
        type: "GET",
        success : function(response)

        {alert('success');}

        ,
        error : function(e)

        {alert('Message on error');}

        });

        Show
        nitinperi nitin added a comment - tried the above , It still returning it empty for getParam(). edited the above snippet rename setParam1 to setParam Snippet in js function callAjaxGet(){ $.ajax( { url : "strustSampleActionNameGet?param=2", type: "GET", success : function(response) {alert('success');} , error : function(e) {alert('Message on error');} });
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Don't use JS and AJAX, just open the action in a browser.

        Show
        lukaszlenart Lukasz Lenart added a comment - Don't use JS and AJAX, just open the action in a browser.
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Can you post the real param name with setter/getter? I assume it isn't param

        Show
        lukaszlenart Lukasz Lenart added a comment - Can you post the real param name with setter/getter? I assume it isn't param
        Hide
        nitinperi nitin added a comment -

        Hi Lukasz, tested the url as you requested and found the cause that is creating issue. Issue is in the way we define the name of an attribute in action class that causing this problem of value not getting populated. Issue happens when first letter of an attribute name is in small letter and immediate second letter is capital (for e.g. fName). To Explain this please check the example below.

        //In Action Class

        private String lName = null;

        //in above example Attribute lName first character is small letter 'l' followed by capital letter 'N'.

        when attribute name is like above . the getter and setter methods generated would be:

        getter name = getlName();
        setter name = setlName(String );

        notice that letter 'l' is not getting capital.

        When this is done in new Struts the param lName is passed in a URL the lName is not getting populated in action class.

        Please let me know if you have any question(s)

        Show
        nitinperi nitin added a comment - Hi Lukasz, tested the url as you requested and found the cause that is creating issue. Issue is in the way we define the name of an attribute in action class that causing this problem of value not getting populated. Issue happens when first letter of an attribute name is in small letter and immediate second letter is capital (for e.g. fName). To Explain this please check the example below. //In Action Class private String lName = null; //in above example Attribute lName first character is small letter 'l' followed by capital letter 'N'. when attribute name is like above . the getter and setter methods generated would be: getter name = getlName(); setter name = setlName(String ); notice that letter 'l' is not getting capital. When this is done in new Struts the param lName is passed in a URL the lName is not getting populated in action class. Please let me know if you have any question(s)
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        It is related to a bug in OGNL that was fixed long time ago, see WW-3909 - basically it will work if you generate proper setter/getter, according to the JavaBean specifications:

        private String pName;
        
        public void setpName(String pName) {
            this.pName = pName;
        }
        
        public String getpName() {
             return pName;
        }
        

        it is also mentioned in a migration guide

        Show
        lukaszlenart Lukasz Lenart added a comment - It is related to a bug in OGNL that was fixed long time ago, see WW-3909 - basically it will work if you generate proper setter/getter, according to the JavaBean specifications: private String pName; public void setpName( String pName) { this .pName = pName; } public String getpName() { return pName; } it is also mentioned in a migration guide

          People

          • Assignee:
            Unassigned
            Reporter:
            nitinperi nitin
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development