In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shares with you the content of a sample analysis of jsp design patterns. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Jsp design pattern
One: introduction
I remember that when I first learned jsp, I always liked to compare him with asp,php, and I was used to using the development mode of asp to write jsp. Later, I found that it was really silly to do so. In fact, jsp has already used MVC mode since it came out. Let me briefly talk about jsp design using MVC to design.
Second: introduction to MVC
MVC is actually an acronym for module, view, and control, that is, when using jsp, there are corresponding files to implement the corresponding operations. Usually jsp is only responsible for view, that is, only for displaying pages. Business logic is implemented by bean (ejb). What is discussed below is the implementation without using ejb. If you use ejb,ejb to take charge of M.C, it is usually implemented by servlet. Or use struts.
You are going to http://jakarta.apache.org/struts to see the introduction of struts.
Third: design ideas
When you build an Application. You have to think about the interface, and interface modification is very common. If you implement all the operations in jsp, you will be in big trouble once you modify the interface. Artists do not understand jsp, you have to modify countless files, the head is not too big, and at this time the general time is very tight, the use of MVC can reduce some of your trouble.
At design time, jsp is only responsible for displaying the page, that is, jsp calls the data from Bean (Struts,servlet) and then displays it, while Bean (Struts,servlet) is responsible for collecting the data needed by jsp and passing it to jsp with ArrayList (Arttibute). If you need to submit a form, you usually submit it directly to Struts,servlet, and then return the processing information after processing. The corresponding business logic is implemented by Bean.
Four: the design of Bean
There are usually three types of Bean when I use them, one under the manager,entry,database directory.
The bean under manager does business logic.
Bean under entry encapsulates data, which means that there is a bean corresponding to each database table. All jsp gets are classes, too.
Bean under database is to operate the database, such as insert,update,delete,load (query a record).
Batchload (query multiple records).
The relationship between them is that entry is responsible for encapsulating the data and returning the result as a call parameter to database.
Manager calls the result of database processing. Manager communicates with jsp. The result of jsp comes from manager, and jsp needs to do an operation to call manager. Even if an insert exists in database, you still need to package it again in manager. The aim is to make the structure as simple as possible. Database is only responsible for operating the database. Manager only does logic (taking the corresponding data) to deal with the corresponding logic, while entry only encapsulates the data obtained by database, or encapsulates the parameters obtained from the page as parameters to the corresponding 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 (handles 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 * * /
Sayid int8,/** spokesperson * * /
Toid int8,/** recipient * * /
Saytime datetime default now (), / * * message time * * /
Have you checked newflg smallint default 1 to 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) {
/ / get a guestbook
}
/ / sqlstr query conditions
/ / orderstr sorting criteria
/ / start of rcdbegin recording
/ / end of rcdend record
/ /
Public ArrayList batchLoad (String sqlstr,String orderstr,int rcdbegin,int rcdend) {
/ / Guestbook is encapsulated in ArrayList
}
Public void insert (Guestbook info) {
}
Public void update (Guestbook info) {
}
Public void delete (int id) {
/ / get a guestbook
}
Public int getRcdNums (String sqlstr) {
/ / fetch the number of records
}
}
GuestbookManager.java
Encapsulate the required method according to the need, this is the part you want to write
= =
The above way entry,database files can be automatically generated, this tool I have developed, if you need to contact me. What you need to write is the method in GuestbookManager, you may feel that the workload is larger than all your operations in jsp, but the structure is very clear. What you also need is to write a database connection pool, where all your database operations are fetched from one place, and it is expensive to connect to the database every time.
Six: technological advantages
1: clear structure
2: easy to maintain
3: protecting the code is better.
Thank you for reading! This is the end of this article on "sample Analysis of jsp Design patterns". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.