Understanding Framework (STRUTS2, Hibernate, Spring) and MVC design patterns in a comprehensible way

Source: Internet
Author: User

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 use login as a simple example to see how we write Web programs in different eras.

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.

<%   
String name = Request.getparameter ("name");
String Password = request.getparameter ("password");

Userhandler Userhandler =new Userhandler ();
if (userhandler.authenticate (name, password)) {
%>
<p> Congratulations, Login success </p>
<%
} else {
%>
<p> Sorry, Login failed </p>
<%
}
%>

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.

  

Publicclass Loginservlet extends HttpServlet {

/* (Non-javadoc)
* @see Javax.servlet.http.httpservlet#dopost

(Javax.servlet.http.HttpServletRequest, Javax.servlet.http.HttpServletResponse)
*/
@Override
Protectedvoid DoPost (httpservletrequest req, HttpServletResponse resp) throws

Servletexception, IOException {
String message =null;
RequestDispatcher dispatcher = Req.getrequestdispatcher ("/result.jsp");
String name = Req.getparameter ("name");
String Password = req.getparameter ("password");

Userhandler Userhandler =new Userhandler ();
if (userhandler.authenticate (name, password)) {
Message = "Congratulations, login successful";
} else {
message = "Sorry, Login failed";
}

Req.setattribute ("message", message);
Dispatcher.forward (req, resp);
}
}

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

  

<servlet>   
<servlet-name>Login</servlet-name>
<servlet-class>
Com.demo2do.servlet.LoginServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>
/login
</url-pattern>
</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
Address specific business Sites ———— control So, 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.

Understanding Framework (STRUTS2, Hibernate, Spring) and MVC design patterns in a comprehensible way

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.