JSP Design Mode

Source: Internet
Author: User

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 in the manager, entry, and database directories.

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 (query a record), and 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 as parameters and passes them 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 (){

}

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 ){
File: // get a guestbook
}
File: // sqlstr query Condition
File: // orderstr sorting Condition
File: // The rcdbegin record starts.
File: // rcdend Record ends
//
Public ArrayList batchLoad (String sqlstr, String orderstr, int rcdbegin, int rcdend ){
File: // The ArrayList contains the Guestbook
}

Public void insert (Guestbook info ){

}

Public void update (Guestbook info ){

}

Public void delete (int id ){
File: // get a guestbook
}

Public int getRcdNums (String sqlstr ){
File: // number of records retrieved
}
}

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.