JSP design pattern

Source: Internet
Author: User
Tags implement insert modify query
js| Design JSP design pattern
One: Introduction
Remember beginners JSP, always like to compare him and asp,php, also accustomed to using the ASP development model to write JSP, and 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 corresponding 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, manager,entry,database under the 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 (query 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) {
Take a guestbook
}
Sqlstr Query criteria
Orderstr Sorting criteria
Rcdbegin Record begins
Rcdend record end \ r
//
Public ArrayList batchload (String sqlstr,string orderstr,int rcdbegin,int rcdend) {
ArrayList inside the package guestbook
}

public void Insert (Guestbook info) {

}

public void Update (Guestbook info) {

}

public void Delete (int id) {
Take a guestbook
}

public int getrcdnums (String sqlstr) {
Take 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.

VI: Technical Advantages
1: Structure clear \ r
2: Easy maintenance \ r
3: Protection code is better.
..........
VII: concluding remarks \ r
I just briefly introduce, concrete use you need to accumulate in the actual application, if need my code see, contact me, if you feel write and dog excrement, very embarrassed, wasted your precious 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.