JSP design mode

zhaozj2021-02-16  94

JSP design mode

One: Introduction

When I start school JSP, I always like to compare him and ASP, PHP, and I used to use the ASP development model to write JSP, and then I found this is really stupid. In fact, JSP has already used MVC mode. . Below I briefly say that JSP design uses MVC to design.

Two: MVC introduction

The MVC is actually an abbreviation, view, and control, that is, when using JSP, there is a corresponding file to implement the corresponding operation. Usually JSP is only responsible for View is only responsible for displaying pages. BEAN (EJB) is implemented in business logic. The discussed below is achieved without using EJB. If you use EJB, EJB is responsible for M.c usually by servlet. Or use Struts.

Struts introduces you to http://jakarta.apache.org/struts to see. I will introduce in later articles.

Three: Design Ideas

When you build an Application. 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

Bean usually has three types, respectively, and the Manager, Entry, Database directory below.

BEAN under Manager makes business logic

The bean under Entry is packaged data, in fact, every database table has a bean. All JSP is all class.

The bean under Database is the operational database, perform such as INSERT, UPDATE, DELETE, LOAD (query a record),

BatchLoad (querying multiple records).

The relationship between them is entry responsible for packaging data, as a calling parameters of Database, returns the result.

Manager calls the result of the Database processing. Manager and JSP communications. The results obtained from JSP come from Manager, JSP needs to do operation calling Manager, even an insert has such a method in Database but in Manager, you still need to package it once. The purpose of this is to make the structure as simple as possible. Database is only responsible for operating the database. Manager only does logic (take the appropriate data) to handle the corresponding logic, while the entry is encapsulated by Database, or the parameter package obtained by the page, as a parameter passed to the corresponding bean.

Five: Design instance

Let's discuss the message board as an instance:

Entry / Guestbook.java (Message Board Object) Database / GuestbookMap.java (Update, Removing, Modify Message Board) Manager / GuestbookManager.java (Processing 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), / ** theme ** / body text, / ** content ** / syyid int8, / ** spokesperson ** / toid int8, / ** acceptor ** / saytime datetime default now (), / ** message time ** / newflg smallint default 1 / ** Whether to view ** /); Guestbook.java ======================================================================================================================================================================================================== 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)} guestbookmap.java =========================== === Import guestbook; public class guestbook () {public guestbookmap () {} public guestbook load (int id) {// Take a guestbook} // SQLSTR Query Condition // ORDERSTR Sort Condition // RCDBEGIN Record start // RCD end recording end / r // public ArrayList batchLoad (String sqlstr, String orderstr, int rcdbegin, int rcdend) {// ArrayList which encapsulates Guestbook} public void insert (Guestbook info) {} public void update (Guestbook info) {} Public void delete (int ID) {// Take a guest int GtrDNUMS (String Sqlstr) {// Take records Number}} GuestBookManager.java The method you want to write as needed.

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

The above ways Entry, Database file can be automatically generated, I have developed this tool, if you need to contact me. What you need to write is the method in GuestBookManager. You may feel that the workload is big in JSP than all your operations, but this structure is very clear. You still need to write a database connection pool, all your database operations are taken from one place, and each time you go to connect to the database overhead. Six: Technical advantages 1: Clear structure 2: Maintenance is convenient 3: Protection code is better.

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

New Post(0)