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...

2
  • what does console.log(data) show? Commented Feb 29, 2016 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 Commented Feb 29, 2016 at 22:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.