An overview of the MVC pattern

Source: Internet
Author: User
Tags interface key modify relative access
MVC is a three-word abbreviation, respectively:
Model, view, and control controller.
The purpose of the MVC pattern is to achieve the functional division of the web system.

The model layer implements the business logic in the system, which can usually be implemented with JavaBean or EJB.
The view layer is used to interact with the user and is usually implemented using JSP.

The controller layer is a bridge between model and view, which can assign a user's request and select the appropriate view for display, and it can also interpret user input and map them to actions that the model layer performs.

Benefits of the MVC pattern


 Each of them, and non-interference in each other's position
In the MVC pattern, three tiers are applied to each other, so if one layer of requirements changes, you only need to change the code in the corresponding layer without affecting the code in the other layers.
 Benefits The division of labor in Development
In the MVC pattern, because the system is opened by layer, the Division of labor in the development can be better realized. Web designers can develop the JSP in the view layer, and developers who are familiar with the business can develop the business layer, while other developers can develop the control layer.
 Facilitates the reuse of components
Tiering is more beneficial to the reuse of components. If the control layer can be used independently as a component, the view layer can also be made into a common operating interface.

Application of different JSP construction



Why use servlet & JSP in combination?
 Typical approach: Use JSP to simplify the development and maintenance of HTML content
 for simple dynamic code, the use of scripting elements to invoke Java code to complete.
 for slightly more complex applications, you can use script elements to invoke custom classes to complete. (that is, the so-called Help Class)
 For more complex applications, use Java beans and custom tags
 But, these are not enough
 for complex processing, starting from the JSP will be difficult to deal with.
In addition to the convenience of isolating the actual code into separate classes, beans, and custom tags, jsp implicitly assumes that a single page gives a single basic view.

A misunderstanding of MVC

 Must adopt a complex framework
 Frameworks are sometimes useful
struts
javaserver Faces (JSF)
 but not necessary!
For most simple or moderately complex applications, using built-in RequestDispatcher is a good way to achieve MVC
MVC affects the design of the whole system
 We can use MVC to process a single request
 can think of it as an MVC scheme, not an MVC framework.
 Also known as the Model 2 program


Implementing MVC with RequestDispatcher

1. Define Java beans to represent data
2. Processing requests using a servlet
servlet read request parameters to check for missing or abnormal data.
3. Populating beans
 The servlet invokes the business logic or data access code to get the final result. The resulting results are placed in the bean defined in the first step.
4. Storing beans in the context of a request, session, or servlet
 The setattribute of this servlet call request, session, or servlet context object to store a reference to the bean that expresses the result of the request.


5. Forward a request to a JSP page
 the servlet determines which JSP page is appropriate to handle the current situation and uses the RequestDispatcher forward method to transfer control to that page.

6. Extracting data from the bean
The jsp page uses Jsp:usebean and a location that matches the 4th step to access the previously stored bean, and then uses Jsp:getproperty to output the Bean's properties.
The jsp page does not create or modify a bean; it simply extracts and displays the data created by the servlet.

The use of Jsp:usebean in MVC and
What's different in a standalone JSP page
jsp pages should not create objects
 All data objects should be created by the servlet. Therefore, in order to ensure that the JSP page does not create objects, we should use <jsp:usebean ... type= "package. Class "/>
Instead of
<jsp:usebean ... class= "package. Class "/>
jsp pages should also not modify existing objects
 Therefore, we should only use Jsp:getproperty, do not use Jsp:setproperty
。 Tip: Jsp:usebean's scope option
request
<jsp:usebean id= "..." type= "..." scope= "Request"/>
session
<jsp:usebean id= "..." type= "..." scope= "Session"/>
application
<jsp:usebean id= "type=" ... "scope= application"/>
page
<jsp:usebean id= "..." type= "..." scope= "page"/>
Or simply use <jsp:usebean id= "..." type= "..."/>
The MVC (Model 2) architecture does not use this scope.

Different ways to share data
 Displays a random number to the user.
 because each request should produce a new number, it is appropriate to share based on the request.

 Displays the user's last name and first name
The  data is stored for each client, so the session based sharing is more applicable.

 Displays a prime number of a specified length.
 data is shared among multiple customers, so the application based sharing is more appropriate.

Data sharing based on request
servlet
Valueobject value = new Valueobject (...);
Request.setattribute ("key", value);
RequestDispatcher Dispatcher =
Request.getrequestdispatcher ("/web-inf/somepage.jsp");
Dispatcher.forward (request, response);

jsp
<jsp:usebean id= "key" type= "Somepackage.valueobject"
scope= "Request"/>
<jsp:getproperty name= "key" property= "Someproperty"/>

Session-based data sharing
servlet
Valueobject value = new Valueobject (...);
HttpSession session = Request.getsession ();
Session.setattribute ("key", value);
RequestDispatcher Dispatcher =
Request.getrequestdispatcher ("/web-inf/somepage.jsp");
Dispatcher.forward (request, response);

jsp
<jsp:usebean id= "key" type= "Somepackage.valueobject"
Scope= "Session"/>
<jsp:getproperty name= "key" property= "Someproperty"/>

Data sharing based on ServletContext
servlet
Synchronized (This)
{
Valueobject value = new Valueobject (...);
Getservletcontext (). setattribute ("key", value);
RequestDispatcher Dispatcher =
Request.getrequestdispatcher ("/web-inf/somepage.jsp");
Dispatcher.forward (request, response);
}

jsp
<jsp:usebean id= "key" type= "Somepackage.valueobject" scope= "Application"/>
<jsp:getproperty name= "key" property= "Someproperty"/>

Relative URLs in the JSP page
 Question:
 forwarding using the request allocator is transparent to the customer. The initial URL is the only URL that the browser knows.
 Why is this more important?
What does the  browser do with these tags that resemble the following:

<link Rel=stylesheet
href= "Jsp-styles.css" type= "Text/css" >
<a href= "bar.jsp" >...</A>
 Answer: Browsers will think of them as a URL relative to the servlet

 The easiest solution:
 using a URL that starts with a slash

Summary
The MVC (Model 2) method applies To:
 A single commit produces multiple basic skins.
 Several pages have a large number of public processing processes.
 requires applications that provide multiple views of the same data, which is a good way of separating the data layer from the presentation layer, especially for developing applications related to the user's graphical interface

 Architecture
 A servlet answers the initial request
servlet completes the actual data processing and stores the results in the bean
bean stored in HttpServletRequest, HttpSession, or ServletContext
servlet uses the RequestDispatcher forward method to forward requests to a JSP page
jsp pages read from the bean by using Jsp:usebean and corresponding scopes (request, session, application)



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.