1

I am learning Struts2 and have the following question on submitting a form with Jquery serialize. I have an action class where I have an object called Policy and the policy class has set of fields as shown below. With jquery ajax I want to set a json string in my Action class and would like to deserialize it to an object.

How much ever I try, I am not able to set the string I defined in my action class. Below is the code

Class CassPolicy{
  String policyNumber;
  String name; 
  //getsets for members

}

Action:

Class PolicyAction{
   String cassPolicyString;
   CassPolicy cassPolicy = new CassPolicy();
   //getsets for members

   String save(){
//In save method  I want to convert the policyString to policy object
//policyString always returns null
   } 
}

JSP:

$.ajax({
 url:PolicyAction.action,
 type:'post',
 data:$("#policyForm").serialize(),
 async:true,
 success:function(data){
 }

});

<s:form id="policyForm">
    <s:textfield name="cassPolicy.policyNumber" label="policyNumber"></s:textfield>
    <s:textfield name="cassPolicy.name" label="name"></s:textfield> 
</s:form>

I even tried in ajax something like data:{cassPolicyString:$("#policyForm).serialize()}

Can someone help me point to the right direction or what is the right way to achieve my task?

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
user2192298
  • 35
  • 1
  • 9
  • Your form field names don't match the action property names, nor is it clear if there are actually getters/setters for the properties. Are you using the JSON plugin? What's going out on the wire? – Dave Newton Apr 22 '13 at 13:13
  • Thanks Dave for replying.. I do have getters and setters in my object and my Action class.. I am using Struts2-Json plugin and have json-default in my struts.xml... I see the values in my json request from browser as cassPolicy.policyNumber=12345&cassPolicy.name=Test – user2192298 Apr 22 '13 at 13:24
  • Your form field names don't match the action property names. – Dave Newton Apr 22 '13 at 13:57
  • Change `cassPolicy.policyNumber` to `policy.number` and `cassPolicy.name` to `policy.name`. That's it... – Andrea Ligios Apr 22 '13 at 13:58
  • In parameters mapping, fantasy is not allowed :D – Andrea Ligios Apr 22 '13 at 13:58
  • Thanks Andrea and Dave..I changed the code to above and still my values are not getting set. – user2192298 Apr 22 '13 at 14:44
  • Please don't hide the code, if they could see the getters/setters they would know if they were correct or not. – Quaternion Apr 22 '13 at 19:31

1 Answers1

0

I figured it out finally.. The issue is form serialize does not give a json format. I used the code in below link to serialize my form as json object and added a interceptor ref element to my action in struts.xml which finally assigned my form values to cassPolicy object.

Convert form data to JavaScript object with jQuery

Community
  • 1
  • 1
user2192298
  • 35
  • 1
  • 9
  • Hey can you please paste your interceptor code, how are really converting json data to CassPolicy Obj. I am also struggling for same requirement from long time. Tx In Advance. – Ganesh K Jul 18 '13 at 14:23