A few of the most confusing aspects of the MVC framework:

Source: Internet
Author: User

Do not learn the framework for learning the framework, but to solve the problem and learn the framework, this is the correct way for a programmer to learn.

--The framework is created to solve one or more problems encountered in web development. Different frameworks are designed to solve different problems, but for programmers, they are just jar packages. The pros and cons of the framework also depend entirely on their comments on the degree of problem resolution and the elegance of the solution. Therefore, we must not learn the framework for learning the framework, but to solve the problem and learn the framework, this is the correct way to learn the programmer.

Now many beginners and programmers are flocking to learn the web development of the book-level framework: Struts2,spring,hibernate. It seems that these frameworks become a person who is proficient in Java and whether it will write the only factual criteria for the EE program and the necessary foundation for job hunting.

However, if you ask these programmers during an interview, why do you study these frameworks? What is the nature of these frameworks? Few people seem to be able to give me a very satisfactory answer. Because they are studying for the sake of learning, learning for the sake of work, not really going deep into a framework. In fact, all of us should think about the question: Why learn the framework? What did the framework bring me? Next, we take a login as the simplest example to see how the different eras, how we write Web programs

Many years ago, we wrote programs like this.

Many years ago, it was a poor age, if we were to use Java to do some dynamic interactivity on the Web page. A lot of people will tell you a technique called JSP. When I was still very confused with Java, I was told that JSP is a good thing, it can write Java code in the HTML code to complete the logic.

HTML code

  1. <%
  2. String name = Request.getparameter ("name");
  3. String Password = request.getparameter ("password");
  4. Userhandler Userhandler = new Userhandler ();
  5. if (userhandler.authenticate (name, password)) {
  6. %>
  7. <p> Congratulations, Login success </p>
  8. <%
  9. } else {
  10. %>
  11. <p> Sorry, Login failed </p>
  12. <%
  13. }
  14. %>

As a JSP, it can receive login requests sent from other JSPs and process them. In this way, we do not need any additional configuration files, and do not need any framework to help, we can complete the logic.

Later, we dropped the write logic on the page

Later, as the program was written more and more, we found that there were many problems with the way in which Java code was written in HTML code to complete the logic:

1. Java code is cluttered and poorly readable because it is promiscuous in an HTML environment. A JSP file can sometimes become dozens of k, or even hundreds of K. To find a logic, often unable to locate.

2. Writing code is very confusing, do not know where the code should be written in the end, do not know whether others have been implemented similar functions, where to reference.

3. Suddenly, a demand has changed. As a result, everyone begins to replace the whole head, but also to be cautious, for fear of changing the logic of others.

4. Logic handlers need to maintain their own lifecycle, and there is no unified support for many modules like database transactions, logs, and so on.

At this time, if there is a product, it will be able to extract the Java code on the page, so that the page as little as possible Java code, how good. So many people started using servlets to deal with those business logic.

Java code

  1. public class Loginservlet extends HttpServlet {
  2. /* (Non-javadoc)
  3. * @see Javax.servlet.http.httpservlet#dopost (javax.servlet.http.HttpServletRequest, Javax.servlet.http.HttpServletResponse)
  4. */
  5. @Override
  6. protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {
  7. String message = NULL;
  8. RequestDispatcher dispatcher = Req.getrequestdispatcher ("/result.jsp");
  9. String name = Req.getparameter ("name");
  10. String Password = req.getparameter ("password");
  11. Userhandler Userhandler = new Userhandler ();
  12. if (userhandler.authenticate (name, password)) {
  13. Message = "Congratulations, login successful";
  14. } else {
  15. message = "Sorry, Login failed";
  16. }
  17. Req.setattribute ("message", message);
  18. Dispatcher.forward (req, resp);
  19. }
  20. }

Here, we need to configure the URL request relationship for this servlet in Web. Xml.

XML code

    1. <servlet>
    2. <servlet-name>Login</servlet-name>
    3. <servlet-class>
    4. Com.demo2do.servlet.LoginServlet
    5. </servlet-class>
    6. </servlet>
    7. <servlet-mapping>
    8. <servlet-name>Login</servlet-name>
    9. <url-pattern>
    10. /login
    11. </url-pattern>
    12. </servlet-mapping>

Refactoring here, we found that our workload itself did not decrease, but that the code moved from the JSP to the servlet, making the whole process seem a little clearer. However, what is the price we pay for so little cleanliness? Make a URL request configuration for each servlet in Web. Xml!

And then, the framework appears

Times further, it has been found that simple jsps and servlets have made it difficult to meet people's lazy demands. As a result, people are beginning to try to summarize some common Java classes to solve the problems encountered during web development. At this time, turned out a frame, called struts. It was very advanced to implement the MVC pattern and became the Gospel of the vast number of programmers.

Example of struts code I'm not going to post it. You can find a bunch of them on the Internet. To a certain extent, struts can solve the problem of responsibility assignment in web development, and make the display and logic separate. But for a long time, the programmers using struts are often unable to separate what we really need the web framework to do for us, what exactly do we need it to do?

What exactly do we want?

After reviewing our history of writing code, let's look back and see what we want.

Whether using JSPs, using Struts1, or Struts2, we need at least some of the necessary elements (if not, I really don't know what the program will look like):

1. Data

In this case, name and password. Together they form the core of the process. In fact, we tend to have a user class that encapsulates name and password, which makes our program more oo. In any case, the data is interspersed throughout the program and becomes the core of the program's operation.

2. Page display

In this case, it is login.jsp. Without this page, all the requests, validations, and error displays are not discussed. On the page, we need to use HTML to present the data we need to show. At the same time we also need to complete a certain page logic, such as error display, branch judgment and so on.

3. Locations where specific business is handled

Here, at different stages, the place to deal with the specific business is not quite the same. Originally used JSP and Servlet, later with Struts1 or Struts2 action.

These must appear above the elements, in different eras, were endowed with different forms of expression, some are bound by the times, its manifestations are very backward, and some are no longer used. But by pushing these outward forms, we can see that this is not the MVC we have revisiting.

Data ———— Model

Page Show ———— View

A place to handle specific business ———— Control

Therefore, the framework is not important, the concept is kingly. As long as you can understand the concept of MVC, the framework is just a jar package for you.

The concept of MVC is actually so simple that these concepts have been deep in our hearts, and what we lack is the excavation of their essence. Let's take a look at the picture below, which is a popular illustration of the MVC model for many years:

In this image, the MVC three box has its own structure and is clearly structured. But I think this picture ignores the problem that the data is moving, and once the data is moved in the view and control layers, there are a lot of problems:

1. Data is passed from the view layer to the control layer, how to make a flat string into a lively Java object

2. How can data format and content be verified conveniently from the view layer to the control layer?

3. Data from the control layer passed to the view layer, a lively Java object, and how to display in various forms on the page

4. If you are trying to send a data request from the view layer to the control layer, how do you know which class you want to call and which method? An HTTP request, and how to establish a relationship with the Java code of the control layer?

In addition, the control layer does not seem to be as simple as it seems, as it is a controller that at least needs to deal with the following problems:

1. As the facade façade that invokes the logic handler, what should we do if the logical handler has an exception?

2. What do we need to do to deal with the results of logical processing to meet the rich foreground display needs?

This one-on-one problem is based on the mining of the basic concepts of MVC. So, all of these questions need to be solved one by one when we write the program. Speaking of which, the questions raised at the beginning of this article should be answered: The framework is designed to solve one or more problems encountered in web development. Different frameworks are designed to solve different problems, but for programmers, they are just jar packages. The pros and cons of the framework also depend entirely on their comments on the degree of problem resolution and the elegance of the solution. Therefore, we must not learn the framework for learning the framework, but to solve the problem and learn the framework, this is the correct way to learn the programmer.

A few of the most confusing aspects of the MVC framework:

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.