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

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

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745656440a4638571.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信