admin管理员组文章数量:1026989
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 badges2 Answers
Reset to default 1The 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>
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 badges2 Answers
Reset to default 1The 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>
本文标签: javascriptHow could I get ModelAndView object in js by using for loopStack Overflow
版权声明:本文标题:javascript - How could I get ModelAndView object in js by using for loop? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745656438a2161625.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论