struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephan Schreck <Stephan.Schr...@eurodyn.com>
Subject json data are not passed in Stuts action
Date Wed, 21 Nov 2012 15:46:54 GMT
Hello,
i have a problem with my struts 2 application. I try to send some json 
data in my action from the following
script.

  <script>
                 $(document).ready(function () {

                var data = {data: {jsonKey1 : "one", jsonKey2: "two", 
jsonKey3: [1, 2, 3, 4, 5], jsonKey4: {a: "A", b: 2, c: "3"}, jsonKey5: 
2, jsonKey6: "2"}};
                     jQuery.ajax(
                     {
                         type: "POST",
                         url: "http://localhost:8080/myAction",
                         data: jQuery.toJSON(data),
                         dataType: "json",
                         async: false ,
                         contentType: "application/json; charset=utf-8",
                         success: function(){window.alert("Done");}
                     });
                 });
             </script>



The action is called without any problem. What i am trying to achieve is 
to send the data to my action and set the Map data. This is something
that does not work.  In my struts.xml i have included the 
JSONInterceptor in my stack, as seen below:

...
...
                 <interceptor-ref name="scopedModelDriven" />
                 <interceptor-ref name="modelDriven" />
                 <interceptor-ref name="fileUpload" />
                 <interceptor-ref name="json" />
                 <interceptor-ref name="staticParams" />
                 <interceptor-ref name="actionMappingParams" />

                 <interceptor-ref name="params">
                     <param 
name="excludeParams">dojo\..*,^struts\..*,_.*</param>
                 </interceptor-ref>
...
...



Here the action that is called while the script is running.

@Injectable
@Result(name = "success", type = "json")
@ParentPackage("json-default")
public class MyAction extends BaseAction {


     Map data = new HashMap();

     public String execute() throws Exception {

         return SUCCESS;
     }


     public Map getData() {
         return data;
     }

     public void setData(Map data) {
         this.data = data;
     }
}



I have added some brakepoints in  the JSONInterceptor. In there there is 
a variable String contentType = request.getHeader("content-type");
The variable contentType seems to be always null. So maybe that is where 
the problem starts from. The data sent are not
recognized as json data maybe.

All in all and in a few lines, i am trying to send the data from the 
script to my struts Action but it seems that i am missing something


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message