2

我有以下 POJO

public class Category {
    private Integer id;
    private String title;
    private String description;

    //many more attributes below
}

public class User {
        private Integer id;
        private String name;
        private String address;

           //many more attributes below
    }

public class MyAction extends ActionSupport {

       //list of objects
    List<Category> categories;

       //Complex Map
    private Map<Category, List<User>> categorizedUsers;

       //getters and setters

    @Override
    public String execute() {

        //populate "categories" and "categorizedUsers" with some business logic
        return SUCCESS;  
}
}

我为此操作进行了 AJAX 调用,并期望 JSON 格式的数据(即“类别”和“分类用户”)。Struts2 为我们提供了 JSON 拦截器,我可以在其中专门过滤要序列化的参数。

以下是struts xml文件中的配置

<action name="mywidget" class="com.struts.action.MyAction">
     <result type="json">
        <param name="includeProperties">
           ^categories\[\d+\]\.id, 
           ^categories\[\d+\]\.title,
        </param>
    </result>
</action>

两个 POJO 都包含很多属性,但是使用"includeproperties",我能够过滤掉Category列表中每个属性的 id 和 title。

但是对于映射键和值,我无法应用任何此类正则表达式模式来过滤掉所需的属性。(假设对于 map key Category,我需要idand title,而对于每个 Value User,我只需要过滤掉id, name)。请建议适当的正则表达式模式应用于 Map<Category, List<User>>.

4

0 回答 0