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

javascript - How could I get ModelAndView object in js by using for loop? - Stack Overflow

programmeradmin2浏览0评论

I want to ask, how could I get ModelAndView object's value in js by using for loop?

In controller, I wrote this:

@RequestMapping(value="SearchCourse.html", method=RequestMethod.GET)
public ModelAndView searchCourse() {
    ModelAndView model = new ModelAndView("/student/SearchCourse");
    model.addObject("schoolList", schoolService.listSchool());
    return model;
}

In js, if I wrote:

${
    schoolList.get(0).getSchoolname()
}

Then, I can get the result.

However, if I wrote a for loop:

var schoolList = new Array();
for(var i = 0; i < "${schoolList.size()}"; i++) {
    alert("${schoolList.get("+i+").getSchoolname()}"); //error
}

Then, I will get an error. I know I can't get write code like the above because i misuse the syntax. But, how can I get the schoolList in js by using ModelAndView?

I want to ask, how could I get ModelAndView object's value in js by using for loop?

In controller, I wrote this:

@RequestMapping(value="SearchCourse.html", method=RequestMethod.GET)
public ModelAndView searchCourse() {
    ModelAndView model = new ModelAndView("/student/SearchCourse");
    model.addObject("schoolList", schoolService.listSchool());
    return model;
}

In js, if I wrote:

${
    schoolList.get(0).getSchoolname()
}

Then, I can get the result.

However, if I wrote a for loop:

var schoolList = new Array();
for(var i = 0; i < "${schoolList.size()}"; i++) {
    alert("${schoolList.get("+i+").getSchoolname()}"); //error
}

Then, I will get an error. I know I can't get write code like the above because i misuse the syntax. But, how can I get the schoolList in js by using ModelAndView?

Share Improve this question edited Apr 7, 2015 at 17:04 xsami 1,33016 silver badges32 bronze badges asked Apr 7, 2015 at 16:27 TommyQuTommyQu 5391 gold badge7 silver badges19 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

The first expression is not JavaScript, it is jstl expression. If you want access to the model in JavaScript you either need to assign values to javascript variables or return JSON objects to your requests. To assign values to javascript array, you should do something like this:

<script>
 var schoolList=new Array();
  <c:forEach items="schoolList" var="school">
      schoolList[]=${school.schoolname};
   </c:forEach>

</script>

Notice, that there is a mix of javascript and jstl in this code. This is a sample code, there might be errors.

Check the answers to the questions below which were similar to yours.

How to transfer java array to javaScript array using jsp?

How to pass array from java to javascript

Converting a Java ArrayList of strings to a JavaScript array

Thanks @jny ! I have figured it out by myself. We can simply use the code below to get the list and iterate the list.

<script>
var schoolList=${schoolList}
for(var i=0;i<schoolList.length;i++) {
  alert(schoolList[i].schoolname);
}
</script>
发布评论

评论列表(0)

  1. 暂无评论