7

I am using struts2-json-plugin-2.2.3.jar. and trying to serialize filterList property of class like this:

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'm not able to get this property of FilterType class.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jeetu Verma
  • 199
  • 1
  • 5
  • 19

4 Answers4

7

Assuming the fields in your FilterType are named id and desc

Try

<param name="includeProperties">
filterList\[\d+\]\.id,
filterList\[\d+\]\.desc
</param>
Quincy
  • 4,393
  • 3
  • 26
  • 40
  • 1
    No problem, although it works I do think it's cumbersome to list out every properties...:/ – Quincy Jan 21 '13 at 08:56
3

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

Its a problem that I had faced using struts2-json-plugin. Even though the plugin-doc show a working examples for includeProperties parameter, it never worked for me and never did after so many trials and googling. So i had to use excludeProperties to remove non-required contents from being serialized, instead of specifying what I want to serialize.

DarkHorse
  • 2,740
  • 19
  • 28
  • ohh... is it so? This is very bad i think, so i have to use excludeProperties to remove non-required contents from being serialized, okay thanx...i will check it and get back to you if it will work.Thank you again...buddy – Jeetu Verma Jan 21 '13 at 07:29
  • i tried with json plugin 2.3.8, includeproperties works just fine. if it doesn't works in your project you can trace the source code of json plugin( org.apache.struts2.json.JSONWriter) it's quite easy. – SalutonMondo Mar 03 '15 at 08:41
0

In case prefer the annotation 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"
SalutonMondo
  • 629
  • 9
  • 17
0

You may try this:

<param name="includeProperties">
    filterList.*
</param>
sharon
  • 123
  • 1
  • 1
  • 11