Premise: In our program is the pursuit of high cohesion, low coupling, but if we put the JSP page code and Java code is written in the JSP code, so that the Java and JSP high-coupling so that not only make the JSP code page is very complex, but also particularly to the detriment of future maintenance, So we have an unwritten specification that is layered architecture, separating the Javad code from the JSP code, making the JSP code page cleaner and neater.
below I will use JSP and Java code to separate the method to make such a two-dimensional table (do a JSP page dynamic display information table )
First step: First we need an entity class to hold a whole piece of information for a person (according to the table information is numbered, name, class, hometown)
A. Let's create a project here I'll use student to name the project name (the project name is usually in full lowercase)
B. We'll build another package in the student project name is entity (this is a physical class, that is, a student's information)
C. Build a class in the package of entity, class name I'm called Studentinfo (Student information means)
D. The next step is to write the code in this class. First fill in the attributes and the fields of the two-dimensional table to be consistent
These properties are then encapsulated: Move the mouse to the ID where right-click = = "Find Source==>generate Getters and setters Click inside to have this page
The attributes are then automatically encapsulated
Step Two: Write an interface and class that implements the interface
A. First set up a package service and then build an interface class Studentservice
B. Write a method in the Studentservice interface to return the list<studentinfo>
Public Interface Studentservice {
How many business logic do you have to design how many methods
Public List<studentinfo> getallstudents ();
}
C. Create a class Studentserviceimpl from within the package (methods to implement the interface)
Public class Studentserviceimpl Implements Studentservice {
Fill in the business logic code inside
Public List<studentinfo> getallstudents () {
A collection of data that holds all student information
list<studentinfo> stulist = new arraylist<studentinfo> ();//Create List collection put all the students ' information in the collection.
Simulating the removal of 3 student information from the database
Studentinfo SI1 = new studentinfo ();//each new Studentinfo class represents a student's information
Studentinfo Si2 = new studentinfo ();//Here's a new three, so there's about three students.
Studentinfo Si3 = new studentinfo ();
Si1.setid ("1");//Add a number to the first student
Si2.setid ("2");//Add a number to the second student
Si3.setid ("3");//Add a number to the third student
Si1.setname ("Li Weiming");//Add a name to the first student
Si2.setname ("Wang Xianjun");//Add a name to the second student
Si3.setname ("Big Head");//Add a name to the third student
Si1.setclasses ("140");//Add a class to the first student
Si2.setclasses ("140");//Add a class to the second student
Si3.setclasses ("140");//Add a class to the third student
Si1.setclasses ("Mars");//Add a native place to the first student
Si2.setclasses ("Mercury");//Add a native place to the second student
Si3.setclasses ("Saturn");//Add a native place to the third student
Store 3 students in the list collection
Stulist.add (SI1);
Stulist.add (SI2);
Stulist.add (SI3);
return stulist;//returns the data from the list collection
}
}
Step three: Edit the JSP page code
A. Create a JSP page first
B. Fill in the code inside
<table width= "70%" boder= "0" cellspacing= "1" cellpadding= "1" align= "center" bgcolor= "#F00" >
<tr bgcolor= "#FFFFFF" >//first make a page with HTML code
<td> number </td>
<td> name </td>
<td> class </td>
<td> native place </td>
</TR>
<%
studentservice stuservice=new Studentserviceimpl ();//Interface new object (because the interface cannot be a direct new object)
, &NB Sp , &NB Sp , &NB Sp //So the subclass new object of the interface is Stuservice, which represents the interface
//implementation class
list<studentinfo> stulist=stuservice.getallstudents ();//The method that invokes the implementation class returns a list<studentinfo>
for (studentinfo student:stulist) {////Use traversal to output data for student information
%>
<tr bgcolor= "#FFFFFF";
Span style= "font-size:18pt" ><td><%=student.getid ()%></td>//then put the number, name, class, birthplace information in the form
Span style= "font-size:18pt" ><td><%=student.getname ()%></td>
<td><%=student.getclasses ()%></td>
<td><%=student.getplace ()%></td>
</TR>
<%}%>
</TABLE>
Running JSP pages
Write a simple two-dimensional table with JSP pages separated from Java code