将数组的值从javascript获取为java作为列表

时间:2021-08-07 09:50:53
var catids = new Array();

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

我有一个catids数组,我存储已选中的复选框值,如下所示。

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

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

cat变量形成类似于1,2,3的东西..

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

我想将这个“cat”发送到java方法并打印这些值。

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

我通过像这样的dwr调用将值传递给java

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

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

我为pojo配置了dwr。我应该为参数配置什么?

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.

//最后它将返回一个pojo,我将在dwr调用的数据中接收它。

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

-------猫咪的大小------------是1 myIds [i]什么都不打印

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

我需要它作为整数回来。我在做什么错?

3 个解决方案

#1


2  

I will do it in this way.

我会这样做的。

  1. JavaScript creates json object like this {"categoryIds": [1,2,3,4,5]}
  2. JavaScript创建像这样的json对象{“categoryIds”:[1,2,3,4,5]}

  3. Java converter convert json to java POJO object using for example Gson or Jackson library.
  4. Java转换器使用例如Gson或Jackson库将json转换为java POJO对象。

  5. After convert you can work with java POJO object which have list of categories.
  6. 转换后,您可以使用具有类别列表的java POJO对象。

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.

如果您使用此解决方案,您的代码将更加清晰,您将能够使用相同的清晰解决方案在JavaScript和Java之间共享更多对象。

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

#2


0  

I tried the code it workd fine

我尝试了它工作得很好的代码

   getTasks("1,2,3");

check what the value of categoriesIds is sent to getTask

检查将categoryIds的值发送到getTask的内容

#3


0  

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

从网页发送此表单参数。然后从java中的HttpServletRequest请求对象获取此信息。

request.getParameter('categoryId');

#1


2  

I will do it in this way.

我会这样做的。

  1. JavaScript creates json object like this {"categoryIds": [1,2,3,4,5]}
  2. JavaScript创建像这样的json对象{“categoryIds”:[1,2,3,4,5]}

  3. Java converter convert json to java POJO object using for example Gson or Jackson library.
  4. Java转换器使用例如Gson或Jackson库将json转换为java POJO对象。

  5. After convert you can work with java POJO object which have list of categories.
  6. 转换后,您可以使用具有类别列表的java POJO对象。

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.

如果您使用此解决方案,您的代码将更加清晰,您将能够使用相同的清晰解决方案在JavaScript和Java之间共享更多对象。

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

#2


0  

I tried the code it workd fine

我尝试了它工作得很好的代码

   getTasks("1,2,3");

check what the value of categoriesIds is sent to getTask

检查将categoryIds的值发送到getTask的内容

#3


0  

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

从网页发送此表单参数。然后从java中的HttpServletRequest请求对象获取此信息。

request.getParameter('categoryId');