JSP design mode

zhaozj2021-02-16  51

JSP design mode --- MVC mode

One: Introduction

I used to write ASP and PHP code, later in order to catch up with the pace of the times. I started to get involved in JSP.

But when I just started writing, I always like to write a procedure for JSP and ASP. Later I found that it was really stupid. And I wrote the JSP code better to write ASP, I can't reflect JSP and Java. Strong. Of course, this is what I want to discuss in JSP today in JSP.

2: What is MVC?

MVC is an abbreviation of Model_View_Control. Model_view_control is a typical structure for software design. In this design structure, an application is divided into three parts: Model, View, and Control, each part is responsible for different functions. Model refers to the application's data, and the operation of these data; View refers to the user interface; Control is responsible for the synchronization between user interface and program data, that is, the operation of the two directions: First, in the user interface (view Operation completes the update of program data (Model), and the change is changed to the user interface in time to the user interface (VIEW).

Three: Design Ideas

When you build a website. You must consider the problem of interface, and interface modification is very common. If you implement all your operations in JSP, you have trouble in the modification interface. The art does not understand JSP, you must go to revise countless files, don't be too big, and at this time, you will be very tight, you can reduce your troubles.

JSP is only responsible for displaying the page at the design, that is, the JSP calls the BEAN (Struts, Servlet) and then displaying the bean (struts, servlet) responsible for collecting the data required by JSP, and passing the ARRAYLIST (ArtTibute) to JSP. If you need to submit a form, it is generally directly submitted to the struts, servlet, and then returns the processing information later. And the corresponding business logic is implemented by bean.

4: Bean design

I usually have two directorys when designing Bean:

For example, the article system is:

com.guessbook.business

with

com.guesbook.db

These two folders (package)

Business decentralized business logic and packaging data

DB drop-up operation database and database connector (of course, I don't use the data pool, it is recommended to use the stored procedure)

Five: Design instance

I don't know why all write design instances and entry have like to use a message board, then I use it.

under business

GuestBookinfo.java (for packaging data) GuestbookFactory.java (connected dbguestbook.java) guestbook.java (used to write abstract classes, interfaces and implement business logic)

under DB

There are dbguestbook.java (all the operations of the database are in this, such as Insert, Update, SELECT, DELETE, and not operate within a method) and DBConnection.java (Database connection file)

Data table structure

Create Table Guestbook

ID INT8 Default NextVal ('seq_guestbook'), / ** Probire ** /

Title varchar (), / ** theme ** /

Body Text, / ** Content ** /

SAYID INT8, / ** Speaker ** /

TOID INT8, / ** Acceptant ** /

Saytime DateTime Default now (), / ** message time ** /

NEWFLG Smallint Default 1 / ** Whether to view ** /

);

The following is some of the code, only for reference:

Guestbookinfo.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 Method)

}

GuestbookFactory.java

==============================================

Public class guestbookfactory {

Private static string classname = "com.guestbook.db.dbguestbook";

Public guestbookfactory () {

}

Public Static Guestbook getInstance () {

Try {

Return () class.Forname (classname) .newinstance ();

}

Catch (Exception E) {

Return NULL;

}

}

}

Guestbook.java

===============================

Package com.guestbook.business;

Import java.util.list;

Public Abstract Class Guestbook {

Protected guestbookinfo gi;

Public guestbook () {

}

Public guestbookinfo getGi () {

Return GI;

}

Public void setgi (guestbookinfo gi) {

THIS.GI = GI;

}

Public Abstract List getGuestbookList ();

// The following is your way to operate the database.

}

DBGUESTBOOK.JAVA

============================

Package com.guestbook.db;

Import java.sql. *;

Import java.util.list;

Import java.util.arraylist;

Import com.guestbook.business. *; public class dbguestbook extends guestbook {

String SQL;

Public dbguestbook () {

}

Public List getGuestbookList () {

ResultSet RS = NULL;

List guestbooklist = new arraylist ();

DBCONNECTION CONN = New DbConnection ();

Try {

RS = conn.executeQuery ("Select * from guestbook");

While (rs.next ()) {

Guessbookinfo gi = new guestbookinfo ();

gi.setid (rs.getint ("ID"));

/ / Implement other methods

GuestbookList.Add (GI);

}

}

Catch (Exception E) {

System.err.println

"com.guessbook.db.dbguestbook.getguestbooklist ()" "Error");

E.PrintStackTrace ();

}

Finally {

CONN.CLOSE ();

Return GuestbookList;

}

}

}

Six: Technical advantages

1: Clear structure

2: Convenient maintenance

3: The protection code is better.

转载请注明原文地址:https://www.9cbs.com/read-19770.html

New Post(0)