Json 使用 Struts2-Json 插件时的序列化问题

Json serialization issue when using Struts2-Json plugin

我有一个具有 3 个属性的操作 class。我正在使用 Struts2-Json 插件来序列化操作 class。我能够按照我的期望序列化 String selectedCompany

问题

People ArrayList<Person> 属性 被序列化为空值。我好像没找到我哪里弄错了。

Json_Response:

动作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>

List 发布为数组,因此您需要定义要包含的数组。喜欢

people\[\d+\]\..*,list\[\d+\]\..* 

如果 List 是对象的类型(而不是简单的 String),您可以将结果缩小到选定的属性,例如:

employee\[\d+\]\.lName,employee\[\d+\]\.fName,

如果 List 对象有内部对象,你可以这样做:

//The employee\[\d+\]\.address.addressline1 is not enough !!
//May be one can suggest better idea here :)
employee\[\d+\]\.address,employee\[\d+\]\.address.addressline1