As the title, is what I want to say, as far as good, only to see the only know
In Javaweb, the most used container is list,map, so this article only around the use of these two containers to explain, then the two containers are derived from the common Arraylist,hashmap,list<map<string,object >>
For list
Scenario One: Appearing in projects written using pure JSP
At this point it will be used directly on the page, such as when we fetch data in the database, we usually take out the database data, and then construct a list to facilitate the page value display.
1ResultSet rs =NULL;2String sql = "SELECT * FROM Newscategory";3PSTMT =conn.preparestatement (SQL);4rs =pstmt.executequery ();5List List =NewArrayList ();6 while(Rs.next ()) {7Newscategory category =Newnewscategory ();8Category.setid (Rs.getint ("id"));9Category.setnewscategory (rs.getstring ("Newscategory"));TenCategory.setparentid (Rs.getint ("ParentID")); One List.add (category); A}
View Code
Scenario two: When using the servlet+jsp MVC implementation, which appears in the DAO implementation, is a method that returns a list for the servlet to invoke
1 PublicList getcategorylist ()throwssqlexception{2ResultSet rs =NULL;3String sql = "SELECT * FROM Newscategory";4PSTMT =conn.preparestatement (SQL);5rs =pstmt.executequery ();6List List =NewArrayList ();7 while(Rs.next ()) {8Newscategory category =Newnewscategory ();9Category.setid (Rs.getint ("id"));TenCategory.setnewscategory (rs.getstring ("Newscategory")); OneCategory.setparentid (Rs.getint ("ParentID")); A List.add (category); - } - returnlist; the}
View Code
Scenario three: Using the Ssh,ssi framework to write directly using the method in the framework to achieve
For map:
A status code that is typically used to respond after a request has been processed by a servlet, as well as information about the prompt
1 @ResponseBody2@RequestMapping (value={"ajaxupdateaddress"},method=requestmethod.post)3 PublicMap<string,object>ajaxupdateaddress (httpservletrequest request,model model,address Address) {4 5map<string,object> map =NewHashmap<string,object>();6Map.put ("Status", 300);7Map.put ("Success",false);8Map.put ("message", "Operation failed");9 Try {Ten Address.setmemberid (Member.getid ()); One addressservice.updateaddress (address); AMap.put ("status", 200); -Map.put ("Success",true); -Map.put ("message", "Operation succeeded"); the}Catch(Exception e) { -Log.error ("Modify address Failed", e); - } - + returnmap; - +}
View Code
For a slightly more complex construct, follow-up notes
The simple use of containers in Java