Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning JSON Object from Struts2 Action Giving Empty Data

Tags:

java

json

struts2

In Action:

//Declaration
    JSONObject jObj1 = null;

    public JSONObject getjObj1() {
            return jObj1;
   }
   public void setjObj1(JSONObject jObj1) {
            this.jObj1 = jObj1;
   }

 //In Action method   
String jsong="{\"cid\":232}";
jObj1 = new JSONObject(jsong);

return Action.SUCCESS

Struts Cfg File :

<action name="jsonAction" class="jsonAction" method="getJson">
            <result type="json" name="success">
                <param name="root">jObj1</param>                
            </result> 
        </action>

I am getting empty result when I see in JSP console

Where I went wrong? Thanks.

like image 357
sasi Avatar asked Apr 28 '26 02:04

sasi


1 Answers

You are overcomplicating it :)

The Struts2-JSON-Plugin will serialize in JSON your Action's objects for you, so you don't need (and it's an error) to encode them by yourself.

Then, keeping your code and config as it is, just change the Action to:

//Declaration
Map<String,Integer> jObj1 = null;

/* Getter and Setter */

//In Action method   
jObj1 = new HashMap<String,Integer>();
jObj1.put("cid",232);
return Action.SUCCESS
like image 165
Andrea Ligios Avatar answered Apr 29 '26 15:04

Andrea Ligios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!