【问题标题】:how to send multiple ArrayList from a Servlet to JSP without using JSTL如何在不使用 JSTL 的情况下将多个 ArrayList 从 Servlet 发送到 JSP
【发布时间】:2020-12-12 13:15:19
【问题描述】:

我必须将三个 ArrayList 从 servlet 发送到 jsp。 我的问题是我不能从同一个方法返回三个 arrayList。

public ArrayList afficher(String s)
ArrayList <A> list = new ArrayList<A>();
ArrayList <B> list2 = new ArrayList<B>();
//some operations

list.add(new A("aaa"));

list2.add(new B("bbbb"));

return list, list2;





ow can I proceed?


【问题讨论】:

  • 您可以创建 List&lt;List&lt;Object&gt;&gt; 也可以使用 ModelAndView 对象并在其中添加所有模型。

标签: java jsp servlets arraylist methods


【解决方案1】:

您可以将变量作为属性存储在ServletContext 中,并在您的JSP 中检索它们。有关详细信息,请参阅此previous question。

【讨论】:

    【解决方案2】:

    您可以使用 ModelAndView 作为返回类型来创建您的方法。

    ModelAndView mav = new ModelAndView();
    mav.setViewName("welcomePage");
    mav.addObject("list", list);
    mav.addObject("list2", list2);
    return mav
    

    在该 mav 对象中,您可以添加 n 个模型。

    在 jsp 上,您可以使用 ${list}, ${list2} 访问它们。

    @Data
    @Builder
    class Main {
    
        public List<List<Object>> getList() {
            List<List<Object>> list = new ArrayList<>();
            list.add(Arrays.asList(new A[]{new A("A"), new A("A1"), new A("A2")}));
            list.add(Arrays.asList(new B[]{new B("B"), new B("B1"), new B("B2")}));
            return list;
        }
    
    }
    
    class A {
        String name;
    
        public A(String name) {
            this.name = name;
        }
    }
    
    class B {
        String name;
    
        public B(String name) {
            this.name = name;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-03
      • 2016-04-01
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多