当前位置: 首页 > 知识库问答 >
问题:

从Jquery向Struts1方法发送数组

公羊宇定
2023-03-14
var article = new Object();
    article.title = "abc";
    article.url = "abc";
    article.categories = [1,2,3];
    article.tags = [1,2,3];


    console.log('hi');
    $.ajax({
        type: 'POST',
        url: URL,
       contentType:"application/json",
        data:  JSON.stringify(article),
        dataType: 'json',
        success: function(result) {
            console.log(result);

        },
        error: function(e){
            alert('Error in Processing');
        }

    });

和我的java代码作为

 BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
                String json = "";
                if(br != null){
                    json = br.readLine();
                }



                // 2. initiate jackson mapper
                ObjectMapper mapper = new ObjectMapper();
                //mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

                // 3. Convert received JSON to Article
                Article article = mapper.readValue(json, Article.class);

现在我的Arcticle类是

public class Article {

    private String title;
    private String url;
    private List<String> categories;
    private List<String> tags;

//getters and setters
}

现在我在排队时例外

String json = "";
                if(br != null){
                    json = br.readLine();
                }
{"title":"abc","url":"abc","categories":"[1, 2, 3]","tags":"[1, 2, 3]"}
{"title":"abc","url":"abc","categories":[1, 2, 3],"tags":[1, 2, 3]}

system.out.println(user.getRouteFirst());

Gson Gson=新Gson();

typeToken>token=new typeToken>(){};List personList=gson.fromjson(user.getRouteFirst(),token.getType());

private String routeFirst;
private String routeSecond;

}

jsp jquery代码作为

var article = new Object();
article.routeFirst = newRoute1;
article.routeSecond = newRoute2;


$.ajax({
    type: 'POST',
    url: '../..//admin/cabsharing/findIntersesctionPoint.do',
    data : "point="+JSON.stringify(article),
    dataType: 'json',
    success: function(result) {
        console.log("success");
    },
    error: function(e){
        console.log("error");
    }

});

共有1个答案

管杜吟
2023-03-14

我的JSP将是,

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Calculator</title>
</head>
<script
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">


$( document ).ready(function() {
    //alert("DOM is ready");
});


function sendJsonData() {

    var article = new Object();
    article.title = "abc";
    article.url = "abc";
    article.categories = [1,2,3];
    article.tags = [1,2,3];

    //alert("JSON string :"+ JSON.stringify(article));

    $.ajax({
        type: 'POST',
        url: "JsonServlet",
        //contentType:"application/json",
        //data:  {point:point},
        data : "point="+encodeURIComponent(JSON.stringify(article)),
        dataType: 'json',
        success: function(result) {

        },
        error: function(e){
        //alert('Error in Processing');
        }

    });
}


</script>

<body>

    <button id="jsonButton" onclick="sendJsonData()">send jdon Data</button>

</body>
</html>

我的servlet将是,

public class JsonServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String point = request.getParameter("point");
        System.out.println("Point : " + point );

        if(point != null){

            ObjectMapper mapper = new ObjectMapper();

            try {
                // read from string, convert it to Article class object
                Article user = mapper.readValue(point, Article.class);

                // Conver the Article class object in to the JSON string
                System.out.println("Output Json String is :::::::::::> "+mapper.writeValueAsString(user));


            } catch (Exception e) {  
                e.printStackTrace();     
            } 
        }           
    }
}

这是我在控制台中获得的输出

Point : {"title":"abc","url":"abc","categories":[1,2,3],"tags":[1,2,3]}
Json String is :::::::::::> {"title":"abc","url":"abc","categories":["1","2","3"],"tags":["1","2","3"]}
 类似资料:
  • 问题内容: 这是我的方案: 我必须调用一个方法。假设参数为:Parameter1,Parameter2,..,..,Parameter N,但是要发送给该方法的参数在每种情况下都可能会更改。 情况1: 仅发送参数 1 情况2: 发送参数的组合 情况3: 发送所有参数 用Java实现此目标的最佳方法是什么? 问题答案: 解决方案取决于问题的答案-所有参数是否都将是同一类型,如果是,则每个参数将被视为

  • 图形QL服务器已设置。当值被硬编码时,它可以正常工作。这是我的调用请求: 这给出了错误: 没有对齐错误。我这样做是为了增强可读性,但是,当对参数进行硬编码时,上述变异可以正常工作。我发现很难找到错误的原因。我已经尝试了两种JSON。stringify(dateArray)和仅dateArray。 或者有没有更好的方法从 react-redux 应用程序查询 GraphQL?

  • 问题内容: 这个问题是密切相关的这一个(已回答)。 在整个项目中,我使用JSF命令按钮在屏幕之间导航,其中action属性将指向返回字符串的函数。字符串是新屏幕的名称,例如 将返回一个字符串,例如,它将引导用户 出于移动目的,我不得不使用ajax调用来使clickable,例如 这有效,我已经测试过了,并且返回了正确的结果(在返回之前测试并记录了它) 但是,没有重定向发生。Ajax调用尚未完成。如

  • 这个问题与这个问题密切相关(已回答)。 在整个项目中,我使用JSF命令按钮在屏幕之间导航,其中action属性将指向返回字符串的函数。字符串是新屏幕的名称,例如。 将返回一个字符串,例如,它将用户引导到 出于移动目的,我必须使用ajax调用使panelGrid可点击,例如。 这个工作原理,我已经测试过了,并返回了正确的结果(在返回之前记录它进行了测试) 但是,没有重定向发生。ajax调用还没有做到

  • 我们有一个返回产品信息的网络服务。服务期望在发布数据中有一个JSON数组...该 来自简单HTML测试网页的示例脚本如下所示(该脚本按预期工作): 用PHP编写的web服务正确地接收到这一信息。转储(打印)web服务从客户端接收的数据将导致以下数组: 现在问题来了......我试图从Google Sheet Script调用webservice,如下所示: 从Google Sheets脚本调用PH

  • 我需要将一些数据从 Angular 传递到 Node.js 服务器。这是我的角度帖子列表组件:从“@angular/核心”导入 { 组件,OnInit };从 “@angular/common/http” 导入 { HttpClient }; 我的node.js服务器: 我想安慰一下。记录数据,但什么都没有发生。