最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

getting the values of array from javascript to java as list - Stack Overflow

programmeradmin3浏览0评论
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 ?

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 ?

Share Improve this question edited Apr 15, 2013 at 7:23 sahana asked Apr 15, 2013 at 6:32 sahanasahana 6215 gold badges12 silver badges33 bronze badges 2
  • 1 You only left out the most important part: HOW do you send this "cat" to java? – vinczemarton Commented Apr 15, 2013 at 6:36
  • Please enter some example usage od getTasks method - how categoriesIds looks before method call and how it should look? – pepuch Commented Apr 15, 2013 at 7:03
Add a ment  | 

3 Answers 3

Reset to default 2

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;
    }
}

I tried the code it workd fine

   getTasks("1,2,3");

check what the value of categoriesIds is sent to getTask

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

request.getParameter('categoryId');
发布评论

评论列表(0)

  1. 暂无评论