Reputation: 335
I have an action class with 3 properties. I am using Struts2-Json plugin to serialize the action class. I am able to serialize the String selectedCompany as per my expectations
The problem
The People ArrayList<Person>
property is serialized with empty values. I don't seem to find where I have made a mistake.
Json_Response:
Action Class:
package json;
import java.util.ArrayList;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import dao.DataAccess;
public class Json extends ActionSupport implements Preparable {
private static final long serialVersionUID = -8415223624346993447L;
private ArrayList<String> list;
private String selectedCompany = "Buhin Engineers";
private ArrayList<Person> people;
public ArrayList<String> getList() {
return list;
}
public void setList(ArrayList<String> list) {
this.list = list;
}
public String getSelectedCompany() {
return selectedCompany;
}
public void setSelectedCompany(String selectedCompany) {
this.selectedCompany = selectedCompany;
}
public ArrayList<Person> getPeople() {
return people;
}
public void setPeople(ArrayList<Person> people) {
this.people = people;
}
public String execute(){
list = new ArrayList<String>();
list.add("Yamaha");
list.add("Hero Honda");
return SUCCESS;
}
@Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
populatePeople();
}
private void populatePeople() {
// TODO Auto-generated method stub
DataAccess da = new DataAccess();
setPeople(da.retrievePeople());
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="json-default">
<action name="Json" class="json.Json">
<result type="json">
<param name="includeProperties">selectedCompany,people,list</param>
</result>
</action>
</package>
</struts>
Upvotes: 2
Views: 750
Reputation: 45475
The List
are published as array so you need to define the array to be included. like
people\[\d+\]\..*,list\[\d+\]\..*
If the List
is the type of objects (instead of simple String
) you can narrow your result to selected properties like:
employee\[\d+\]\.lName,employee\[\d+\]\.fName,
If the List
objects has inner objects you can do as:
//The employee\[\d+\]\.address.addressline1 is not enough !!
//May be one can suggest better idea here :)
employee\[\d+\]\.address,employee\[\d+\]\.address.addressline1
Upvotes: 2