簡體   English   中英

如何將不同對象的 ArrayList 組合到一個列表 / Map 中,然后從組合列表 /Map 中檢索每個 arraylist 的值?

[英]How to combine ArrayList of different objects into one list / Map and then retrive value of each arraylist from combined list /Map?

I have a function that have 3 arraylists of bean classes.I want to add/combine them into one and return that through function.That combined arraylist is received by controller. 我想分別檢索所有數組列表並設置一個變量,該變量將用於在 jsp 頁面上顯示數據。

我在 function 中有這段代碼,我從數據庫中獲取值。我想組合 sbean1、tbean1 和 dbean1 並通過 function 返回它。

public /* datatype*/ selectMyBlogs(){

/* code for getting data from database*/

List<StudentBean> sbeanl = null;
List<TeacherBean> tbeanl = null;
List<DepartmentBean> dbeanl = null;

StudentBean sbean = new StudentBean(sid,sname,scourse,sdept);
sbeanl = new ArrayList<StudentBean>();
sbeanl.add(sbean);
TeacherBean tbean = new TeacherBean(tid,tename,tsubject,tsal,tdept);
tbeanl = new ArrayList<TeacherBean>();
tbeanl.add(ebean);
DepartmentBean dbean = new DepartmentBean(did,dcourses,dstudents,dteachers);
dbeanl = new ArrayList<DepartmentBean>();
dbeanl .add(dbean);

/* code to combine all lists*/

return /* combined data */;
}

This is my controller class function where I will get the value return from above function.dbconn is the object of class and selectMyData() is the function in which above code resides.Then want to fetch each list data seperately and give them a variable using request.setAttribute() 方法,以便我可以在我的 jsp 頁面上顯示數據。 我這樣做是因為我想在一頁上顯示學生老師和部門的數據。

private void viewMyData(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException{
        
/*what will come here - list/Map? */ = dbconn.selectMyData();

/* how to fetch seperate list data here*/

request.setAttribute("", );

/*then use set setAttribute method so that data can be show on jsp page*/

RequestDispatcher rd = request.getRequestDispatcher("myData.jsp");
rd.forward(request, response);
}

請有人告訴我該怎么做。 我嘗試使用 arraylist 將其組合,但我不知道該列表的 angular 括號中會出現什么。

I have a function that have 3 arraylists of bean classes.I want to add/combine them into one and return that through function.That combined arraylist is received by controller. 我想分別檢索所有數組列表並設置一個變量,該變量將用於在 jsp 頁面上顯示數據。

我在 function 中有這段代碼,我從數據庫中獲取值。我想組合 sbean1、tbean1 和 dbean1 並通過 function 返回它。

public /* datatype*/ selectMyBlogs(){

/* code for getting data from database*/

List<StudentBean> sbeanl = null;
List<TeacherBean> tbeanl = null;
List<DepartmentBean> dbeanl = null;

StudentBean sbean = new StudentBean(sid,sname,scourse,sdept);
sbeanl = new ArrayList<StudentBean>();
sbeanl.add(sbean);
TeacherBean tbean = new TeacherBean(tid,tename,tsubject,tsal,tdept);
tbeanl = new ArrayList<TeacherBean>();
tbeanl.add(ebean);
DepartmentBean dbean = new DepartmentBean(did,dcourses,dstudents,dteachers);
dbeanl = new ArrayList<DepartmentBean>();
dbeanl .add(dbean);

/* code to combine all lists*/

return /* combined data */;
}

This is my controller class function where I will get the value return from above function.dbconn is the object of class and selectMyData() is the function in which above code resides.Then want to fetch each list data seperately and give them a variable using request.setAttribute() 方法,以便我可以在我的 jsp 頁面上顯示數據。 我這樣做是因為我想在一頁上顯示學生老師和部門的數據。

private void viewMyData(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException{
        
/*what will come here - list/Map? */ = dbconn.selectMyData();

/* how to fetch seperate list data here*/

request.setAttribute("", );

/*then use set setAttribute method so that data can be show on jsp page*/

RequestDispatcher rd = request.getRequestDispatcher("myData.jsp");
rd.forward(request, response);
}

請有人告訴我該怎么做。 我嘗試使用 arraylist 將其組合,但我不知道該列表的 angular 括號中會出現什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM