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?