IncludeProperties in Json struts2 plugin not working

I am using struts2-json-plugin-2.2.3.jar . and tries to serialize the filterList class filterList as follows:

struts.xml code

 <action name="jsonUserFilterListAction" class="com.my.fitnessb.actions.UserArticlesAction" method="filterList"> <result name="success" type="json"> <param name="includeProperties">filterList</param> </result> </action> 

Action class

 public class UserArticlesAction extends ActionSupport implements SessionAware, ServletRequestAware { private List<FilterType> filterList; HttpServletRequest request; public String filterList() { filterList = new ArrayList<FilterType>(); filterList.add(new FilterType(10, "Latest Articles")); filterList.add(new FilterType(1, "Trending Articles")); filterList.add(new FilterType(2, "Top Rated Articles")); filterList.add(new FilterType(3, "Most Viewd Atricles")); filterList.add(new FilterType(4, "All Atricles")); return SUCCESS; } //setter & getter of filterList } 

but I cannot get this property of the FilterType class.

+6
source share
3 answers

Assuming the fields in your FilterType are called id and desc

Try

 <param name="includeProperties"> filterList\[\d+\]\.id, filterList\[\d+\]\.desc </param> 
+6
source

The Struts2-json plugin will serialize your all action attributes in the action class.

This is the problem I encountered using struts2-json-plugin. Despite the fact that plugin-doc shows working examples for the includeProperties parameter, it never worked for me and never did it after so many tests and searches on the Internet. So I had to use excludeProperties to remove optional content from serialized, instead of specifying what I want to serialize.

+3
source

In case of preferred flavor

 @ParentPackage("json-default") @Namespace("/") @ResultPath(value = "/") @Results({ @Result(name="firstDir",type="json" ,params = {"includeProperties","fileList\\[\\d+\\]"} ) }) fileList = new ArrayList<String>(); for (File img : folder.listFiles()) { fileList.add(img.getName()); } return "firstDir" 
0
source

All Articles