You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Stephan Schreck <St...@eurodyn.com> on 2012/11/21 16:46:54 UTC

json data are not passed in Stuts action

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


Re: json data are not passed in Stuts action

Posted by Lukasz Lenart <lu...@apache.org>.
2012/11/23 Stephan Schreck <St...@eurodyn.com>:
> Finally i made it work, it seems that the problem was the missing annotation
> @Action(interceptorRefs = { @InterceptorRef("json") }) .

You can use @InterceptorRef("json")

> Now i have to understand why my real javascript does not work.
> I might continue in this thread.

You must define your own stack in struts.xml and refer to it setting
up the struts.convention.default.parent.package constant, eg.

<constant name="struts.convention.default.parent.package" value="my-stack"/>

By default the Convention plugin uses convention-default which
basically is struts-default.


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

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


Re: json data are not passed in Stuts action

Posted by Stephan Schreck <St...@eurodyn.com>.
Finally i made it work, it seems that the problem was the missing annotation
@Action(interceptorRefs = { @InterceptorRef("json") }) .

Now i have to understand why my real javascript does not work.
I might continue in this thread.

Thank you for your help.



On 11/23/2012 9:29 AM, Lukasz Lenart wrote:
> 2012/11/22 Stephan Schreck <St...@eurodyn.com>:
>> So the issue still remains, why does it not work when i use the annotations.
> In the first step would you configure a normal result (as jsp) and
> check if that works ? And if you specify @ParentPackage why you adding
> interceptor into struts.xml ? Could you show the whole struts.xml ?
>
>
> Regards



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


Re: json data are not passed in Stuts action

Posted by Lukasz Lenart <lu...@apache.org>.
2012/11/22 Stephan Schreck <St...@eurodyn.com>:
> So the issue still remains, why does it not work when i use the annotations.

In the first step would you configure a normal result (as jsp) and
check if that works ? And if you specify @ParentPackage why you adding
interceptor into struts.xml ? Could you show the whole struts.xml ?


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

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


Re: json data are not passed in Stuts action

Posted by Stephan Schreck <St...@eurodyn.com>.
Hello Martin,

i just tried with the struts.xml  this time instead of anotations and it 
worked just fine. The data from the script
passed inside the Action without a problem at all.
I left my script untouched, so the only thing that i did was removing 
the anotations from the Action and
inserting the package with the action in my struts.xml .
Furthermore i added inside my <package name="myPackage" 
extends="struts-default"> the
         <result-types>
             <result-type name="json" 
class="org.apache.struts2.json.JSONResult" />
         </result-types>

The rest of the code stayed untouched.


So the issue still remains, why does it not work when i use the 
annotations.

Thank you

On 11/22/2012 4:07 AM, Martin Gainty wrote:
> Stephan sounds as if the navigation may not be working
>   instead of annotations what happens if you use the *normal* struts.xml package and action declarations e.g
> <interceptors>           <interceptor-stack name="guest" >
>                         <interceptor-ref name="params">
>                                                <param name="excludeParams">dojo\..*,^struts\..*,_.*</param>                                  </interceptor-ref>
>             </interceptor-stack>            </interceptors>
>
> <struts>......
>      <package name="NamespaceName" namespace="/" extends="struts-default">        <action name="myAction">
>              <result name="success">/success.jsp</result>            <result name="error">/error.jsp</result>
>             <interceptor ref-name="guest"/>        </action>    </package>
> .....</struts>
> Martin
>
>   > CC: user@struts.apache.org
>> From: nikhilesh.sovani@gmail.com
>> Subject: Re: json data are not passed in Stuts action
>> Date: Thu, 22 Nov 2012 06:20:38 +0530
>> To: user@struts.apache.org
>>
>> Stephan,
>>
>> Are you sure your data is getting passed in request body as you are making an Ajax call ?
>>
>> On 21-Nov-2012, at 9:16 PM, Stephan Schreck <St...@eurodyn.com> wrote:
>>
>>> 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
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>   		 	   		



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


RE: json data are not passed in Stuts action

Posted by Martin Gainty <mg...@hotmail.com>.
Stephan sounds as if the navigation may not be working
 instead of annotations what happens if you use the *normal* struts.xml package and action declarations e.g
<interceptors>           <interceptor-stack name="guest" >
                       <interceptor-ref name="params">
                                              <param name="excludeParams">dojo\..*,^struts\..*,_.*</param>                                  </interceptor-ref>           
           </interceptor-stack>            </interceptors>

<struts>......
    <package name="NamespaceName" namespace="/" extends="struts-default">        <action name="myAction">
            <result name="success">/success.jsp</result>            <result name="error">/error.jsp</result>
           <interceptor ref-name="guest"/>        </action>    </package>
.....</struts>
Martin

 > CC: user@struts.apache.org
> From: nikhilesh.sovani@gmail.com
> Subject: Re: json data are not passed in Stuts action
> Date: Thu, 22 Nov 2012 06:20:38 +0530
> To: user@struts.apache.org
> 
> Stephan,
> 
> Are you sure your data is getting passed in request body as you are making an Ajax call ?
> 
> On 21-Nov-2012, at 9:16 PM, Stephan Schreck <St...@eurodyn.com> wrote:
> 
> > 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
> > 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
 		 	   		  

Re: json data are not passed in Stuts action

Posted by Stephan Schreck <St...@eurodyn.com>.
Hello,

well i am debugging inside the JSONInterceptor but i do not see any data 
in the request, unless i look on the wrong side for data.
That is my current problem also, i believe that the data is not passed 
although it should.
To avoid also problems, i want to note that  the source code below is 
based on the example found on this
page: http://anton-solovyev.livejournal.com/172056.html.

Let me know if i could provide some further information.


On 11/22/2012 2:50 AM, Nikhilesh Sovani wrote:
> Stephan,
>
> Are you sure your data is getting passed in request body as you are making an Ajax call ?
>
> On 21-Nov-2012, at 9:16 PM, Stephan Schreck <St...@eurodyn.com> wrote:
>
>> 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
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>



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


Re: json data are not passed in Stuts action

Posted by Nikhilesh Sovani <ni...@gmail.com>.
Stephan,

Are you sure your data is getting passed in request body as you are making an Ajax call ?

On 21-Nov-2012, at 9:16 PM, Stephan Schreck <St...@eurodyn.com> wrote:

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

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