I. Introduction
I remember that when I was a beginner in jsp, I always liked to compare it with asp and php, And I was used to writing jsp in asp development mode. Later I found this was a silly practice, in fact, jsp has already used the MVC mode when it comes out. Below I will briefly talk about jsp design using MVC for design.
II. Introduction to MVC
MVC is the abbreviation of model, view, and control. That is to say, when using jsp, there are corresponding files for corresponding operations. Generally, jsp is only responsible for viewing, that is, displaying pages only. The business logic is implemented by bean (ejb. The following describes implementation without using ejb. If ejb is used, the ejb is responsible for M. C which is usually implemented by servlet. Or use struts.
For details about struts, visit http://jakarta.apache.org/struts. I will introduce it in future articles.
Iii. Design Ideas
When you build an Application. You must consider the interface problems, and the interface modification is very common. If you implement all the operations in jsp, it will be too much trouble to modify the interface. If the artist does not understand jsp, you must modify countless files by yourself. The header should not be too large, but the time is usually very tight. using MVC can reduce your troubles.
During the design, jsp only displays the page. That is to say, jsp calls the data transmitted by Bean (Struts, servlet) and then displays the data required by jsp (Struts, servlet, use ArrayList (arttivity) to pass to jsp. If a form needs to be submitted, it is generally submitted directly to Struts and servlet. After processing, the processing information will be returned. The corresponding business logic is implemented by Bean.
Iv. Bean Design
When using Bean, there are usually three types of beans: manager, entry, and database.
The bean in the manager performs business logic
The bean under the entry is the encapsulation of data. In fact, each database table has a bean. Jsp also obtains all classes.
The bean in the database is the database for operations, such as insert, update, delete, load (querying a record ),
Batchload (query multiple records ).
The relationship between them is that the entry is responsible for encapsulating data and returning results as database call parameters.
The result of the manager calling the database. Manager communicates with jsp. Jsp results are returned from the manager. jsp requires operations to call the manager. Even if an insert statement exists in the database, you still need to encapsulate it again in the manager. The purpose is to make the structure as simple as possible. Database is only responsible for database operations. The manager only processes the logic (fetch the corresponding data), while the entry only encapsulates the data obtained from the database, or encapsulates the parameters obtained from the page, send the Bean as a parameter to the corresponding Bean.
V. design instances
I will discuss the message board as an example below:
Entry/Guestbook. java (message board object)
Database/GuestbookMap. java (Update, delete, modify Message Board)
Manager/GuestbookManager. java (process all transactions)
Data Table 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),/** topic **/
Body text,/** content **/
Sayid int8,/** speaker **/
Toid int8,/** receiver **/
Saytime datetime default now (),/** message time **/
Newflg smallint default 1/** check **/
);
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 (){
} $ False $
Public int getId (){
Return this. id;
}
Public void setId (int _ id ){
This. id = _ id;
}
........
(All get/set methods)
}
GuestbookMap. java
====================================
Import Guestbook;
Public class GuestbookMap (){
Public GuestbookMap (){
}
Public Guestbook load (int id ){
// Obtain a guestbook
}
// Sqlstr query Conditions
// Orderstr sorting Condition
// The rcdbegin record starts
// The rcdend Record ends.
//
Public ArrayList batchLoad (String sqlstr, String orderstr, int rcdbegin, int rcdend ){
// The ArrayList encapsulates the Guestbook
}
Public void insert (Guestbook info ){
}
Public void update (Guestbook info ){
}
Public void delete (int id ){
// Obtain a guestbook
}
Public int getRcdNums (String sqlstr ){
// Retrieve the number of records
}
}
GuestbookManager. java
Encapsulate the required methods as needed. This part is what you want to write.
==================================
In the preceding method, the database file can be automatically generated. I have developed this tool. Contact me if necessary. What you need to write is the method in GuestbookManager. You may think that the workload is larger than all your operations in jsp, but the structure is very clear. You also need to write a database connection pool. All your database operations are taken from one place, and it is costly to connect to the database every time.
Vi. Technical advantages
1: clear structure
2: easy to maintain
3: the Protection Code is better.
..........