显示列表列表从spring控制器发送到jsp [英] display arraylist send from spring controller to jsp

查看:97
本文介绍了显示列表列表从spring控制器发送到jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题模型如下

public class Question{
    String question;
    String optA;
    String optB;
    String optC;
    String optD;
    //getter and setter
}

问题列表如下

questionList = new ArrayList<Question>();
        questionList.add(new Question("Which of these method is used to find out that a thread is still running or not?"," checkRun()"," Run()","Alive()","isAlive()"));  
        questionList.add(new Question(" Which of these method waits for the thread to treminate?"," Run()","Alive()","sleep()","join()"));  

在Controller中,我通过了以下列表

In Controller I have passed list as follows

model.addAttribute("data", questionManager.getQuestionList());

在jsp页面中

var json = "${data}";
         $.each(json, function (index, item) {
        console.log("qustions"+item);
         });

在json中,可变数据如下

In json varible data is as follows

var json = "[com.hello.in.model.Question@405b7c, com.hello.in.model.Question@9393a9]";

如何迭代数据?

我可以通过以下代码得到问题,optA和其他信息

I can get question,optA and other by following code

 <c:forEach items="${data}" var="question">
            ${question.question}<br>
        </c:forEach> 

但是我想要js中的qestions值.

but i want values of qestions inside js.

$( document ).ready(function() {
//I want here coz i want to do some processing
}

我该怎么做?

推荐答案

您不能直接在js上访问java arrayList.您需要使用json或xml才能将其访问js.当您执行var json = "${data}"时,它仅调用toString函数,并且没有等效于Java列表的js.

You cannot access java arrayList directly on js. You need to use json or xml to access them onto js. When you do var json = "${data}", it just invokes toString function and there is no js equivalent of java list.

为了避免这种情况,您可以简单地使用net.sf.json.JSONArray中的JSONArray或任何将java转换为json并将其存储在模型属性中的框架.

In order to circumvent that, you can simply use JSONArray from net.sf.json.JSONArray or for that matter any framework that converts java to json and store it in model attribute.

model.addAttribute("data", new JSONArray().addAll(questionManager.getQuestionList()));

现在在javascript上,您应该可以使用js表示法访问它.

on javascript now, you should be able to access it with js notation.

        var json = JSON.parse("${data}");
         $.each(json, function (index, item) {
          alert(item.question);
          alert(item.optA);
         });

这篇关于显示列表列表从spring控制器发送到jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆