0

The getter menthods are successfully called. But the client side always receives null values.

struts2 version 2.3.15
Struts2 JSON plugin version 2.3.15

Action Class

public class UpdateInfo implements Action{

private String uname;
private String uemail;
private String uphone;                     
private JSONObject status;

@Override
public String execute() throws Exception {
    DbConnector connector = new DbConnector();
    HttpServletRequest request = ServletActionContext.getRequest();
    JsonFormatter jf = new JsonFormatter();
    HashMap<String,String> updates = jf.createUpdateRecords(request); 
    this.setStatus(connector.updateUserInfo(updates));
    return SUCCESS;
}

public void setUname(String name){
    this.uname = name;
}

public void setUemail(String email){
    this.uemail = email;
}

public void setUphone(String phone){
    this.uphone = phone;
}

public void setStatus(JSONObject jb){
    this.status = jb;
}

public String getUname(){
    return this.uname;
}

public String getUemail(){
    return this.uemail;
}

public String getUphone(){
    return this.uphone;
}

public JSONObject getStatus(){
    return this.status;
}
}

Here updateUserInfo() returns a JSON object

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!-- Configuration for the default package. -->
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default,json-default">
    <result-types>
        <result-type name="tiles" 
        class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>

   ......................

    <action name="updateinfo" class="com.portal.controls.UpdateInfo" method="execute">
         <result name="success" type="json"></result>
    </action>

</package>
</struts>

Form to submit

<form method="post" id="uform">
    <input type="text" id="uname" name="name" placeholder="Name"><br>
    <input type="text" id="uemail" name="email" placeholder="Email"><br>
    <input type="text" id="uphone" name="phone" placeholder="Phone"><br>
    <input type="submit" id="ibutton" value="Update">
</form>

Client Side Call

$.post("updateinfo",$('#uform').serialize(),function(data){
            if(data.status === "1"){
                alert("updated");
            }
            else{
                alert("Update failed");
            }
            },"json");
        });

The response received

{"status":{},"uemail":null,"uname":null,"uphone":null}

The values received are always null. I checked whether the getter methods are called before the JSON response and it's working. The class members hold the correct values when the getter methods are called. Only when the response is received, they are null. Any idea what's wrong with the code?

Aravind Bharathy
  • 1,540
  • 3
  • 15
  • 32

1 Answers1

2

I am not sure why you expect this to work because a JSONObject is returned by one of your methods. When you use the JSONPlugin, all fields with a getXXX() method signature will be marshalled to JSON. Which is what you are seeing in the output you posted. However those fields are not populated and that's why you get a null value for each.

You will have to unwrap the JSONObject and populate the uname, email and phone fields with that data.

ramp
  • 1,256
  • 8
  • 14
  • Won't calling setXXX() methods do that? – Aravind Bharathy Mar 05 '15 at 07:24
  • I am updating one of them by calling setStatus(). Why isn't the change reflected in the response? – Aravind Bharathy Mar 05 '15 at 07:30
  • @arvind, the type of status field is a JSONObject. I suspect you will have to set it to a more 'base' datatype - String, Number, Boolean, List or Map. – ramp Mar 05 '15 at 10:39
  • @ramp You are right. Setting to a base type works fine. So does the JSON plugin provide support only for base types? – Aravind Bharathy Mar 05 '15 at 10:58
  • @ramp Got it. JSONObject is NonSerializable(Transient). It can be serialized only as a JSON not as a Java Object. And struts JSON plugin doesn't accept transient objects – Aravind Bharathy Mar 05 '15 at 11:19
  • 1
    That, and also the fact that the struts2-json plugin serialize Java objects into JSON objects itself, to prevent you from manually doing the serialization. You must not provide an object that is already JSONized to it. – Andrea Ligios Mar 05 '15 at 13:09