如何调用返回 JSP 文件中列表的 Java 方法?
我已经考虑过调用 Java 方法,但我一直试图调用返回列表的 Java 方法,以便我可以在 JSP 中使用该列表。
JSP:
<%@ page import="mainPack.ShoppingService"%>
<%@ page import="java.util.List" %>
$('#mainCheckbox').on( "change",function () {
<%
ShoppingService s = new ShoppingService();
%>
<%
List<String> storesList = s.getStoresList();
%>
//unresolved variable error
console.log(storesList.contains("Macys"));
});
Java:
public List<String> getStoresList() {
return storeDao.getStoresList();
}
我还尝试进入控制器并将其设为属性。
JSP:
$('#mainCheckbox').on( "change",function () {
//I get a Uncaught Reference Error: Macys Not Defined
var jsArray = ${storesList};
});
Java:
model.addAttribute("storesList", ShoppingService.getStoresList());
我不确定第二次尝试是否有效。当我打开调试器时,我看到它似乎将列表转换为:
var jsArray = [Macys, JcPenny, Kohls, Target];
I've looked into invoking a Java method but I am stuck trying to call a Java method which returns a list so that I can use that list in JSP.
JSP:
<%@ page import="mainPack.ShoppingService"%>
<%@ page import="java.util.List" %>
$('#mainCheckbox').on( "change",function () {
<%
ShoppingService s = new ShoppingService();
%>
<%
List<String> storesList = s.getStoresList();
%>
//unresolved variable error
console.log(storesList.contains("Macys"));
});
Java:
public List<String> getStoresList() {
return storeDao.getStoresList();
}
I've also tried going into the controller and making it an attribute.
JSP:
$('#mainCheckbox').on( "change",function () {
//I get a Uncaught Reference Error: Macys Not Defined
var jsArray = ${storesList};
});
Java:
model.addAttribute("storesList", ShoppingService.getStoresList());
I am not sure the second attempt isn't working. When I open up debugger I see that it seems to translate the list into:
var jsArray = [Macys, JcPenny, Kohls, Target];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解你的问题,你想返回/显示列表到 JSP 中,那么这就是我的想法 -
If I understand your question correctly, you want to return / display list into JSP , then this is what I am thinking --