◆Introduction to JSP Design Patterns
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.
◆Introduction to MVC in JSP Design Patterns
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.
◆Design Concept of JSP Design Mode
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.
◆Bean design in JSP Design Mode
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 uses the logic to fetch the corresponding data) to process the corresponding logic, 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.
◆Design Example of JSP Design Mode
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 increment1;
- /** Serial number generator **/
- Create table guestbook (
- Id int8DefaultNextval ('Seq _ guestbook'),/** Primary key **/
- Title varchar (64),/** Topic **/
- Body text,/** Content **/
- Sayid int8,/** Spokesman **/
- Toid int8,/** Receiver **/
- Saytime datetimeDefaultNow (),/** Message time **/
- Newflg smallintDefault 1/** Check **/
- );
- Guestbook. java
- ======================================
- ImportJava. util .*;
- Public ClassGuestbook (){
- Private IntId;
- PrivateString title;
- PrivateBody title;
- Private IntSayid;
- Private IntSayid;
- PrivateDate saytime;
- Private ShortNewflg;
- PublicGuestbook (){
- }
- Public IntGetId (){
- Return This. Id;
- }
- Public VoidSetId (Int_ Id ){
- This. Id = _ id;
- }
- ........
- (All get/set methods)
- }
- GuestbookMap. java
- ====================================
- ImportGuestbook;
- Public ClassGuestbookMap (){
- PublicGuestbookMap (){
- }
- PublicGuestbook load (IntId ){
- File:// Obtain a guestbook
- }
- File:// Sqlstr query Conditions
- File:// Orderstr sorting Condition
- File:// The rcdbegin record starts
- File:// The rcdend Record ends.
- //
- PublicArrayList batchLoad (String sqlstr, String orderstr,IntRcdbegin,IntRcdend ){
- File:// The ArrayList encapsulates the Guestbook
- }
- Public VoidInsert (Guestbook info ){
- }
- Public VoidUpdate (Guestbook info ){
- }
- Public VoidDelete (IntId ){
- File:// Obtain a guestbook
- }
- Public IntGetRcdNums (String sqlstr ){
- File:// 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. 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 database operations are taken from one place, and it is costly to connect to the database every time.
- A brief overview of embedded objects in JSP
- Functions and principles of several encodings in JSP and Servlet
- Use JSP include mechanism to improve appearance
- Six common issues for JSP Programming
- JSP tag library Parsing