sahana
sahana

Reputation: 621

getting the values of array from javascript to java as list

var catids = new Array();

I have a catids array where i store the checked checkbox values like the below one.

cat = $("input[name=catChkBox]:checked").map(function () {
  return $(this).data('name');
  }).get().join(",");

the cat variable forms something like this 1,2,3..

I want to send this "cat" to a java method and print those values.

I pass the values to java through a dwr call like this

  DataHandler.getTasks( categories, {callback:function(data){
  }, errorHandler:function(){
  },async:false
   });

I have configured dwr for pojo. should I configure anything for parameters?

I tried the below code but I didn't get anything.

public List<Facade> getTasks(String myIds){
 String[] ids = catids .split(",");
 System.out.println("-------size of cat id------------" + myIds.length);
 for (int i=0; i<myIds.length;i++)
 System.out.println(myIds[i]);

//finally it will return a pojo which i l be receiving it in data of dwr call.

-------size of cat id------------ is 1 myIds[i] prints nothing

I need it as an integer back. What mistake am I doing ?

Upvotes: 0

Views: 10303

Answers (3)

pepuch
pepuch

Reputation: 6516

I will do it in this way.

  1. JavaScript creates json object like this {"categoryIds": [1,2,3,4,5]}
  2. Java converter convert json to java POJO object using for example Gson or Jackson library.
  3. After convert you can work with java POJO object which have list of categories.

If you use this solution your code will be more clear and you will be able to share more objects between JavaScript and Java using the same clear solution.

Example (pseudo code)

CategorList class

public class CategoryList {
    private ArrayList<Category> categoryList;
    // getters and setters
}

Converter

public class CategoryListConverter {
    public CategoryList convert(String json) {
        Gson g = new Gson();
        CategoryList cl = g.fromJson(json, CategoryList.class);

        return cl;
    }
}

Upvotes: 2

Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

Send this as a form parameter from webpage. Then get this from HttpServletRequest request object in java.

request.getParameter('categoryId');

Upvotes: 0

aymankoo
aymankoo

Reputation: 671

I tried the code it workd fine

   getTasks("1,2,3");

check what the value of categoriesIds is sent to getTask

Upvotes: 0

Related Questions