简体   繁体   English

如何从spring mvc中的控制器类返回两个列表

[英]how to return two lists from controller class in spring mvc

I have two tables call maintab,subtab for generate menubar. 我有两个表调用maintab,subtab用于生成菜单栏。

maintab has 

maitabId ,main_tab_name

subtab has:
sub_tb_ID  ,main_tb_id,subtab_name ,Url

I want to get two list containig list1=maintabid & maintabname 我想得到两个列表containsig list1 = maintabid&maintabname

list2=subtabname,maintabID & url

I want to return the two list using spring mvc. 我想使用spring mvc返回两个列表。 And retrieve in jsp page to populate a menu.Please give me a code of controller class and jsp: i use hibernate and tile to this sample. 并在jsp页面中检索以填充菜单。请给我一个控制器类和jsp的代码:我使用hibernate和tile到这个样本。

i tired 我累了

public String listmaintabAndsubtabs(Map<String, Object> map) {

        map.put("maintab", new maintab());
        map.put("maintabList", contactService.listmaintab());

        return "maintab";
    }

how to to return subtabs and main tabs both by one method.... 如何通过一种方法返回子标签和主标签....

Why do you want to return only a list, use map instead. 为什么要只返回一个列表,而是使用map。

In your controller you can use, 在您的控制器中,您可以使用,

Map mp = new HashMap();

mp.put("list1", lst1);
mp.put("list2", lst2);

return mp;

in your jsp, you can iterate the map, 在你的jsp中,你可以迭代地图,

for (Map.Entry<> entry : mp.entrySet()) {
    String listKey = entry.getKey();
    List<> childLst = entry.getValue();
}

EDIT : 编辑:

Once you have two list, you can iterate them, in multiple ways, 一旦你有两个列表,你可以用多种方式迭代它们,

you can use 您可以使用

for (X obj: childLst) { // X indicates the class of object the list contains System.out.println(obj); for(X obj:childLst){// X表示列表包含System.out.println(obj)的对象类; } }

you can also use iterator to loop through the list. 你也可以使用迭代器遍历列表。

One way to do this is to have an dto. 一种方法是使用dto。 Dto pattern is when your data doesn't fit in your approach towards them. Dto模式是指您的数据不适合您的方法。 So you can have a class like this 所以你可以拥有这样的课程

public class MenuDto {
      private List list1;
      private List list2;          

      your accessor method for list1 & list2
}

And then your method in controller can just pass out an instance of MenuDto. 然后你在控制器中的方法可以传递一个MenuDto的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM