2

I'm trying to get a String value from action class using $.getJSON method, but I'm getting the result as undefined. Here are the code snippets which I have tried with

Script:

$(function() {
        $("#newPostionFormID").submit(
                function() {
                    var formInput = $(this).serialize();
                    $.getJSON('../employeeShifts/addnewposition.action',
                            formInput, function(jsonResponse) {
                                console.log(jsonResponse.positionAdded);
                            });
                    return false;
                });
    });

strus.xml:

<package name="employeeShifts" namespace="/employeeShifts"
                extends="struts-default,json-default">
     <action name="addnewposition" 
        class="com.vgt.intranet.employeeshifts.action.EmployeeShiftAction"
                    method="addNewPosition">
                    <result type="json">
                        <!-- <param name="excludeNullProperties">true</param>
                        <param name="noCache">true</param>
                        <param name="root">positionAdded</param> -->
                    </result>
        </action>
   </package>

action class:

public class EmployeeShiftAction extends ActionSupport {
private String position;
private String positionAdded;

public String addNewPosition() {
    log.info("inside addNewPosition");
    position = this.getPosition();
    log.info("Position: " + position);
    positionAdded = position;
    log.info("positionAdded: " + positionAdded);
    return Action.SUCCESS;
}

public String getPosition() {
    return position;
}

public void setPosition(String position) {
    this.position = position;
}

public String getPositionAdded() {
    return positionAdded;
}

public void setPositionAdded(String positionAdded) {
    this.positionAdded = positionAdded;
}

Console Log:

undefined

I don't know where I went wrong? any help appreciated.

Roman C
  • 49,761
  • 33
  • 66
  • 176
TSKSwamy
  • 225
  • 2
  • 21
  • @AleksandrM, If I print only `jsonResponse` it is showing `Object { empID=1000, com.opensymphony.xwork2.util.OgnlValueStack.MAP_IDENTIFIER_KEY=""}` – TSKSwamy May 26 '15 at 09:52
  • Is your action method executed? What values do you assign to properties? – Aleksandr M May 26 '15 at 11:57
  • @AleksandrM Action method executed and problem solved by putting this tag `positionAdded` . (**PS:** This worked after clean and build the tomcat-server). Thanks alot for your response. – TSKSwamy May 26 '15 at 12:05
  • @AleksandrM, It is solved by adding the `root` tag now, but without adding that why it is showing the Object value something else which I have not added in my Action method? Is there any chance of getting another `json` result type action values in same struts file? – TSKSwamy May 26 '15 at 12:28
  • Well it shouldn't. Maybe you had some cache issues (if clean worked)? Can you try with the latest S2 version. – Aleksandr M May 26 '15 at 12:31
  • It is just working for that string only which we have given as `root`. If I want to get a list in addition it is not working. (I'm trying with latest one, meanwhile just curious to clear this one) – TSKSwamy May 26 '15 at 12:45
  • @AleksandrM Tried with latest S2, problem not resolved. Without specifying that `root` tag it is giving an object which is not relevant to this action. But if we put that tag we are limiting to only one string value. What may be causes the struts controller to give this strange result? Is it what the cache issue which you said? If it is how can I resolve this issue? – TSKSwamy May 26 '15 at 13:10

4 Answers4

1

In the console log you print jsonResponse.positionAdded, but you don't have a property positionAdded in the JSON returned by the action. To access this property you should return JSON result

<result type="json">
    <!-- <param name="excludeNullProperties">true</param> -->
    <param name="noCache">true</param>
</result> 
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • thanks for you response. I tried in the way you said but it is showing the same result. Did I get you correctly or missing something? – TSKSwamy May 26 '15 at 09:09
  • I don't know you might were missing a json plugin or it's dependency, are you using maven? – Roman C May 26 '15 at 16:58
  • No, manually I'm adding jars in the lib folder and added those jars to build path. Included json plugin also. – TSKSwamy May 26 '15 at 17:10
  • Yes sure! I have added all the required libs and it is working for string if I include `root` tag `positionAdded`. But without this the it is showing some anonymous object as result. – TSKSwamy May 26 '15 at 17:21
  • I don't know may be you pushed something to the value stack before the result is executed. You can use another root: `#action` to make sure the root is action. – Roman C May 26 '15 at 19:30
  • if I add that tag it is throwing an Exception as `Exception in thread "http-bio-8090-exec-10" java.lang.OutOfMemoryError: Java heap space` – TSKSwamy May 27 '15 at 04:13
  • `action` alone causes `java heap space exception`. It is working if we add the `includeProperties` tag also. Myself posted an answer with your hints. Thank you – TSKSwamy May 27 '15 at 05:02
1

You can define multiple objects as out parameters using inlcude params.. By default some times , a JSON call will automatically calls all the funtions/methods in that controller which have their method names starting with 'get'.. So make sure this doesn't happen.

<result type="json">
<param name="excludeNullProperties">true</param>
     <param name="ignoreHierarchy">false</param>
     <param name="includeProperties">list1.*,list2.*,string</param>
</result>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
shiva kumar
  • 121
  • 5
  • `includeProperties` is working if we add the `action` tag also. Myself posted an answer with your hints. Thank you – TSKSwamy May 27 '15 at 05:03
0

You should code in your action class for json.You should return back json data to client side.In my situation i have used GSON that it is a java library that can be used to convert Java Objects into their JSON representation.GSON(https://code.google.com/p/google-gson/) Have a look here.You can not retrieve json data in your client side until you have return back json data from your action class.Try to convert positionAdded to JSON data from your action class.

I would suggest for your client side,this script:

 jQuery.ajax(
                        {
                            type: 'POST',
                            url:'../employeeShifts/addnewposition.action',,
                            dataType: 'json',
                            success:function(data){
                                var dat=data.positionAdded
                                //alert(dat);
                                var obj = jQuery.parseJSON(dat);

                                            alert(obj);

                },
                            error:function(data1,data2,data3){
                                //alert("error");

                                alert(data3);
                            }



                    });
Fation Hadri
  • 160
  • 2
  • 2
  • 20
  • 1
    OP uses json plugin instead of converting object to json by hand. – Aleksandr M May 26 '15 at 12:09
  • @AleksandrM, ok,but he sholud code for json in action class,json have it representation and he can't send to client side only the property of type string,and he asks for getting this value to client side. – Fation Hadri May 26 '15 at 12:17
  • 2
    Yes he can because of the S2 json plugin. – Aleksandr M May 26 '15 at 12:17
  • I did not know this and sorry about that,i used GSON and works well for me.Which of this two ways(GSON and JSON plugin) it is better? – Fation Hadri May 26 '15 at 12:21
  • 1
    Don't be sorry. Take a look at json plugin: https://struts.apache.org/docs/json-plugin.html. – Aleksandr M May 26 '15 at 12:27
  • This plugin i have used for sending data to server side and for getting back to client side i have used GSON because my json plugin it is not working for me.And for autocomplete app i used this json plugin.I thought it i something new.Thank you very much BTW :) – Fation Hadri May 26 '15 at 12:33
  • 2
    Be sure to understand [how the json-plugin works](http://stackoverflow.com/a/17149414/1654265) @FationHadri – Andrea Ligios May 27 '15 at 08:05
0

Thanks you for all. Here I'm posting the final working struts configuration as an answer with your responses.

<result type="json">
    <param name="root">action</param>
    <param name="includeProperties">positionAdded</param>
    <param name="excludeNullProperties">true</param>
    <param name="noCache">true</param>
</result>
TSKSwamy
  • 225
  • 2
  • 21
  • I don't see where did you get `subDepartmentMap` in the action class? You should edit your question and add missing code. Incomplete answers impossible to answer right and it doesn't help anyone who will read it. – Roman C May 27 '15 at 08:31
  • I actually need only a string as I posted in my question. I just want to try if it is working or not for more objects. For that reason only I added `subDepartmentMap`. Okay I will edit my answer with what I have asked in my question. – TSKSwamy May 27 '15 at 09:06