1

Here is my Struts action

@Action("/trylogin")
@ParentPackage("json-default")
@Result(type = "json", params = { "includeProperties", "msg, productsList" })
public class Login extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private String utilisateur;
    private String motdepasse;
    private String msg;
    private ArrayList<Article> productsList = new ArrayList<Article>();

    @Autowired
    private Dao dao;

    public String execute() {

        if (dao.validCredentials(utilisateur, motdepasse)) {
            System.out.println("USER FOUND");
            productsList = dao.fetchProducts();
            msg = "success";
        } else {
            System.out.println("ERREUR");
            msg = "error";
        }
        return ActionSupport.SUCCESS;
    }

public ArrayList<Article> getProductsList() {
    return productsList;
}

public String getMsg() {
    return msg;
}

Here is my ajax post :

$.post({
    url: "trylogin",                        
    data: {                                 
        utilisateur: name,
        motdepasse: password
    }       
}).done(function(data) {                
    console.log(data.productsList.length);
}).fail(function( jqXHR, textStatus ) {     
    console.log("Fail");
})

I'd like to fetch my productsList. In my Action the list is loaded properly. But once I console.log(data.productsList) it's empty.

How should I do to get the productsList from my struts action to my javascript ?

My productsList is a list of objects that has various attributes like name/id/color...

Roman C
  • 49,761
  • 33
  • 66
  • 176
Aod Ren
  • 681
  • 1
  • 8
  • 17
  • what does `console.log(data)` show? – charlietfl Feb 29 '16 at 22:36
  • Hi @charlietfl console.log(data) returns Object { msg: "success", productsList: Array[0] } ; but a System.out.print(productsList) right after my productsList = dao.fetchProducts() returns a list with 3 elements inside it – Aod Ren Feb 29 '16 at 22:56

2 Answers2

1

You need to list each attribute of Article in the whitelist of the allowed properties.

Let's say Article has id, name and color attributes, the configuration would be:

@Result(type = "json", params = { "includeProperties", 
                                  "msg, 
                                   productsList\\[\\d+\\]\\.id,
                                   productsList\\[\\d+\\]\\.name,
                                   productsList\\[\\d+\\]\\.color"})

This is why I prefer to use a root object instead of includeProperties, even if in your case you'd need to figure out a way to deal with msg (that could be probably be composed client side based on the result of the List, though).

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
1

The parameter includeProperties is a list of regex expressions. When you use a parameter productsList the json serializer only finds this property, but when goes further and parse it's elements none of them are included. If you use this parameter with the result only properties that match the expressions are included in the json output.

You need to configure includeProperties in the json result. For example

@Result(type="json", params = {"includeProperties", "msg,
  productsList\\[\\d+\\]\\.id,productsList\\[\\d+\\]\\.name,productsList\\[\\d+\\]\\.color"}) 

Note, that special characters are double escaped.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    Thanks alot Roman as usual. I've noticed that doing : + "productsList\\[\\d+\\]\\," + "productsList.idarticle," + "productsList.nomarticle" + "productsList.couleurarticle," + "productsList.taillearticle," Also work. For some reason i don't need to add \\[\\d+\\] for every attributes – Aod Ren Mar 01 '16 at 12:45