我有两个表调用 maintab,subtab 用于生成菜单栏。
maintab has
maitabId ,main_tab_name
subtab has:
sub_tb_ID ,main_tb_id,subtab_name ,Url
我想得到两个包含 list1=maintabid 和 maintabname 的列表
list2=subtabname,maintabID & url
我想使用 spring mvc 返回两个列表。并在 jsp 页面中检索以填充菜单。请给我 Controller 类和 jsp 的代码:我对此示例使用 hibernate 和平铺。
我累了
public String listmaintabAndsubtabs(Map<String, Object> map) {
map.put("maintab", new maintab());
map.put("maintabList", contactService.listmaintab());
return "maintab";
}
如何通过一种方法同时返回子标签和主标签....
最佳答案
为什么要只返回一个列表,用map代替。
在你的 Controller 中你可以使用,
Map mp = new HashMap();
mp.put("list1", lst1);
mp.put("list2", lst2);
return mp;
在你的jsp中,你可以迭代 map ,
for (Map.Entry<> entry : mp.entrySet()) {
String listKey = entry.getKey();
List<> childLst = entry.getValue();
}
编辑:
一旦你有两个列表,你可以用多种方式迭代它们,
你可以使用
for (X obj: childLst) {//X表示列表包含的对象类 System.out.println(obj);
您还可以使用迭代器循环遍历列表。
关于java - 如何从 spring mvc 中的 Controller 类返回两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30815016/