One: Introduction
Remember beginners JSP, always like to compare him and asp,php, also accustomed to using the ASP development model to write JSP later found that this is really silly practice, in fact, JSP came out of the MVC model has been used. Let me briefly say that JSP design is designed using MVC.
II: Introduction to MVC
MVC is actually a model, view, control abbreviation, that is, when using JSP, there are corresponding files to implement the appropriate operation
Usually the JSP is only responsible for view, which is only the display page. Business logic such as a bean (EJB) to implement. The following discussion is implemented without using EJBS. It is usually implemented by the servlet if the M.C is responsible for using EJB,EJB. or use struts. Struts introduction You but go to http://jakarta.apache.org/struts to see. I'll introduce you later in the article.
Three: Design ideas
When you build a application. You have to consider the interface problem, and the interface modification is very common. If you implement all the actions in the JSP, you are in a lot of trouble once you modify the interface. Art does not understand JSP, you have to modify countless files yourself, not too big head, and at this time is very tight, using MVC can reduce some of your trouble. JSP is only responsible for displaying the page at design time, that is, the data passed by the JSP call Bean (Struts,servlet) is then displayed and the Bean (Struts,servlet) is responsible for collecting the data that the JSP needs, using ArrayList (Arttibute) Pass to JSP. If you need to submit a form, generally also directly to struts,servlet, after processing and then return processing information. and the corresponding business logic is implemented by the bean.
Four: The bean design
The bean usually has three classes when I use it, under the Manager,entry,database directory respectively.
The bean below the manager does business logic
The bean under entry is the encapsulation data, in fact, each database table corresponds to a bean. The JSP also gets all the classes.
The bean under database is the operation database, executing such as insert,update,delete,load (querying a record), Batchload (querying multiple Records).
The relationship between them is that entry is responsible for encapsulating the data, as a database call parameter, to return the result.
The manager invokes the results of the database processing. Manager and JSP communication. JSP results from the manager, JSP needs to do the operation to call the manager, even if an insert in the database exists such a method but in the manager you still need to be packaged once. This is done to make the structure as simple as possible. Database is only responsible for manipulating databases. The manager only makes logic (take the appropriate data) to handle the corresponding logic, and entry only the data that the database is encapsulated, or the parameters of the page to encapsulate, as a parameter to the appropriate bean.
Five: Design examples
Let me take the message board as an example to discuss:
Entry/guestbook.java (Message Board object)
Database/guestbookmap.java (update, delete, modify message board)
Manager/guestbookmanager.java (Handling all transactions)
Datasheet structure (PostgreSQL)
Create sequence Seq_guestbook increment 1;
/** Serial number Generator **/
CREATE TABLE Guestbook (
ID int8 default nextval (' Seq_guestbook '),/** primary key **/
Title varchar (64),/** theme **/
Body text,/** Content **/
Sayid int8,/** spokesperson **/
Toid int8,/** accepted **/.
Saytime datetime Default Now (),/** message Time **/
NEWFLG smallint default 1/** have viewed **/
);
Guestbook.java
=======================
Import java.util.*;
public class Guestbook () {
private int id;
Private String title;
Private body title;
private int Sayid;
private int Sayid;
Private Date Saytime;
Private short NEWFLG;
Public Guestbook () {
}
public int getId () {
return this.id;
}
public void setId (int _id) {
this.id=_id;
}
........
(All is Get/set method)
}
Guestbookmap.java
==============================
Import Guestbook;
public class Guestbookmap () {
Public Guestbookmap () {
}
Public Guestbook load (int id) {
FILE://took a guestbook
}
File://sqlstr Query criteria
File://orderstr Sorting criteria
File://rcdbegin Record begins
File://rcdend End of record
//
Public ArrayList batchload (String sqlstr,string orderstr,int rcdbegin,int rcdend) {
File://ArrayList inside the package guestbook
}
public void Insert (Guestbook info) {
}
public void Update (Guestbook info) {
}
public void Delete (int id) {
FILE://took a guestbook
}
public int getrcdnums (String sqlstr) {
file://the number of record bars
}
}
Guestbookmanager.java
Encapsulate the required methods as needed, this part is what you want to write
The above way Entry,database file can be automatically generated, this tool I have developed, if need to contact me. What you need to write is the Guestbookmanager inside, and you may find that the workload is larger than all your operations in the JSP, but the structure is very clear. All you need is to write a database connection pool, all of your database operations are taken from one place, and connecting to the database is expensive every time.