Learning JavaWeb is a must-read article! Why do we use frameworks such as struts2, springMVC, and spring?

Source: Internet
Author: User

Learning JavaWeb is a must-read article! Why do we use frameworks such as struts2, springMVC, and spring?

(What is the purpose of learning such a framework in Javaweb? Why are they so popular? What did they bring to us? What does Servlet do for us? What is the practical significance of MVC? How important is Spring? In this article, I hope more people can see how to understand these questions and understand Javaweb! Reproduced from http://www.importnew.com/15831.html)

This year, I have been thinking about the issue of front-end and back-end separation in web development. Now, I am quite confused, now, the technical framework of the control layer of many web projects in the company is migrated from struts2 to springMVC. I suddenly have a new question that cannot be correctly explained, why do we choose frameworks such as struts2 or springMVC for java web development instead of using technologies such as servlet and jsp? In particular, the front-end pages of our web are all developed using templates such as velocity and jsp is discarded. What kind of benefits will this choice bring to java web development, with the idea of this problem extended, I found new questions. Why do many enterprise-level java Developers choose spring frameworks? What does spring framework bring to our applications? I think this is really a question. It seems difficult for me to find a string of convincing answers. In the end, I found that I am very familiar with common technologies, in fact, there are still many things that make me unfamiliar. These strange and incomprehensible aspects are also the key to whether I can use them at a higher level. I will talk about these issues in this article today, however, struts2 and spring technologies have been very large and complex after so many years of accumulation. They have been widely used for many years, many technologies are unfamiliar and unclear, soThis article is not a comprehensive article that answers my questions, but to understand this question based on my existing knowledge structure..

There are many excellent frameworks in the software, and there is a type of framework. It features a technical framework built on an existing technology that provides the same business functions as the existing technology, this new technical framework is easier to use than the original technology, more robust, and more powerful. For example, jQuery and struts2 and springMVC mentioned in this article are all quite complex, howeverFramework advantagesThere is actually only one:This means that users only care about the development of core services. The framework helps you block various technical issues irrelevant to the original technology and business development.. Frameworks such as jQuery, struts2, and springMVC are so good at this point that many programmers who use it do not know the true nature of the original technology, therefore, we need to better understand struts2 and use it more skillfully and deeply. here we need to jump out of struts2 technology, to the source servlet of struts2 technology, and carefully study the characteristics of servlet, only in this way can we learn the struts2 framework better.

Servlet is used to receive requests sent from the browser to the server, and return the response processed by the server to the user's browser.The browser communicates with the server through the http protocol. The process is that the browser assembles the requested http packet based on the user's choice according to the http Protocol packet specification, the packet is transmitted to the specified server over the network. The server receives the packet information through a specific web container, such as a web container such as tomcat, jetty, and jboss,The web Container will parse the http packet. If it is a user request, the final parsed packet information will be stored in a request object. After the server uses this request to complete corresponding processing, the server program encapsulates the result information in the response object, and then delivers the response object to the web Container. The web Container then converts the response object into an http packet and then delivers the packet back to the browser, the browser finally parses the Response Message and displays the final result to the user..

Web containers create servlet interfaces. servlet interfaces are the places where developers implement their own business logic. developers can develop servlet interfaces by filling in blank questions, the context or context prompt of the blank question is the request and response object. However, the servlet interface in the javaEE specification is very simple. There are three methods: init, service, and destory, but this interface is too general, therefore, the specification also provides an HttpServlet class, which provides methods such as doGet and doPost Based on the http request type,Servlet interfaces are defined based on the characteristics of http.Therefore, if you are not particularly familiar with the http protocol during servlet development, you will encounter more or less confusing problems, especially when encountering complicated and special requests, such as file upload, returning a special file format to a browser is not very convenient to use servlet for development at this time. Another problem in servlet development may be overlooked, that is, the type conversion of requested data, http transmission is in the form of text, and it is also the text type after parsing by the web Container. If you encounter a type such as currency, number, and date, we need to convert it according to the actual situation, if the page sends a lot of information, we have to do a lot of type conversion, this kind of work is not technical, it is a physical activity and it is easy to cause program errors. At the same time, enterprise development in java is centered around javabean, and data converted by types must be encapsulated into corresponding javabean. This transfer is definitely not a good thing for project development, so the old struts1 found a solution for this problem, that is, to define a DTO object (data transmission object) to be specifically responsible for such a thing, but in struts2, the action that replaces servlet itself is a javabean.

A Technical Feature of Java Enterprise development is the use of javabean,One of the features of struts2YesIt replaces the servlet operation class as a typical javabeanFirst, the struts2 framework converts and encapsulates the data transmitted on the page, and then encapsulates the request information into the javabean attribute, so that we canEliminating the hassle of type conversion and EncapsulationAs mentioned above, the traditional servlet is defined based on the http protocol. It will process user requests in your request method (post or get, however, for a program developer, a request, specific to a url, is actually a function provided by the server, or an external action by the server, if we use servlet to develop programs, we have to convert http actions into specific business actions, which makes program development cumbersome and makes development more difficult, therefore, struts2 replaces servlet's javabeanThe problem of converting http request methods and specific business actions in servlet is shielded. Every method in the javabean can correspond to each url request one by one, which inevitably reduces the difficulty of development..

Another role of Servlet is to construct a response object so that the page can get the correct response.In fact, modern browsers are multimedia tools, text, images, visual screens, and other things that can be displayed in browsers. Different resources will lead to differences in http response packets, if we use servlet development, we need to use hard-coded java programs based on different resources. Such programs are difficult to reuse, and if programmers cannot understand the processing of a certain resource, this will cause problems. struts2 splits the logic from the java program in the form of a configuration file and uses the configuration method for unified management, this method is similar to spring's AOP method, which makes the result processing method more unified and more conducive to management. It also improves the robustness of the program and reduces the difficulty of development.

In the MVC development mode, Servlet is the control layer in the C layer, and the control layer is like a Russian dual layer (one head is east and one head is west, one header looks at the M-layer model layer, and one header looks at the V-layer view layer. The model layer is also written in java, and the control layer is also developed in the server language, therefore, there is no natural obstacle to communication between the M layer and the C layer, but it is different from the V layer view layer. This is a cross-language communication. For browsers, it only understands html, javascript and css, the browser cannot understand the java language, but to make the server's things accepted by the browser, we must put the server's response information on the page, therefore, a technology is required to convert java information into html pages. This is the jsp technology provided in the javaEE specification. jsp is actually a server technology rather than a client technology, however, it seems to be more like html technology. In the earliest jsp development, java code is directly written to the page. Everyone knows the disadvantages. Later, the javaEE specification provides the custom tag technology, the struts2 framework provides a complete setCustom tag TechnologyThis does not seem to sound like a thing, but it has an extraordinary role, because the reason why custom tags are called custom is that everyone can define them by themselves. If there is no specification, it will inevitably lead to confusion, in addition, a complete set of custom tags is a system project. A complete set of custom tags is equivalent to defining a new development language on our own, when programmers hear this, they will surely understand that the workload and Development difficulty of developing a complete set of custom tags are unimaginable, and the custom tags are closely linked to the control layer, it is difficult to add a dimension, so the custom tag provided by struts2 will bring about a qualitative overflight for business development.

Servlet also has two important technologies:Listeners and filtersListener is rarely used in web development. It is used only in some special cases. In most web development, you can ignore it, the most commonly used listener may be the listener for creating and destroying ServletContext. ServletContext is the Global Object of the entire web application and is bound with the lifecycle of the Web application, therefore, this listener is used to initialize and destroy the global information of the Web application, such as the initialization operation of the spring container. What is interesting is that filters have an interceptor in struts2. They are used to intercept requests, because the interceptor is a unique function of struts2, the use of interceptor in struts2 is naturally more convenient than the use of filter. In fact, the technology used by the interceptor is more advanced than the filter. Because the interceptor uses the reflection technology, the interceptor intercepts more planes, the ability to control requests is stronger, and the tasks that can be completed are more diverse.

When I first came into contact with struts2, someone told me that one of the purposes of struts design was to block requests and response objects in the control layer, these two http protocol sons may cause confusion in web development, but I often unconsciously use these two objects in actual development. I also like to use ajax for front-end development. When using ajax technology, I hate struts2's custom tags. I prefer to use javascript technology to process various types of information on pages, in the end, struts2 is a servlet variant in my eyes. Therefore, for some time, I often wondered if I could discard struts2 and directly use servlet, because struts2 uses too many reflection mechanisms, in particular, annotations are used for configuration (annotations are implemented using reflection). Reflection execution efficiency in java is very low. using servlet directly can definitely improve the execution efficiency of web applications. In fact, this is very difficult to do, because at that time I was unable to flexibly use spring Technology in servlet.

Next I will talk about spring.

SpringThe technology can be said to be developed by java enterprises.The most important technologyBut it is really troublesome to really understand the role and significance of spring. Many people understand spring in the usage phase (for example, declarative transactions are very useful ), today's spring technology ecosystem is spectacular, and spring is all-encompassing. Its content is no less than its source java language, the large boxes of spring are built on ioc and aop technologies. Only by understanding these two technologies can we understand why spring boxes can hold so many things.

The first is ioc. The first explanation of ioc technology is control inversion. It also has dependency injection, which is hard to be understood literally, but when you understand its principles, you will find how accurate their descriptions are.The essence of Ioc technology is the object construction technology. In other words, it is the technology that converts a class instance into an object.The instantiation class in java is carried out through the new Keyword. Every time a new class is added, a new instance object will be generated. This is a waste, and sometimes this waste is quite dangerous, because during program development, we often only need to use a class to generate only one instance object.Singleton ModeIn addition, you can also useFactory methodGenerate an object. People who have used spring can see the above text, and bean definitions in spring correspond to the above content one by one. The scope attribute single generates a singleton object, prototype generates new objects. Beans can also generate objects in factory mode. spring bean is a tool used to create objects. In object-oriented programming, objects are equivalent to displaying an entity in life. For example, if an object is used to complete hunting, then the hunting object contains two secondary objects: People and guns, only when people and guns are assigned a hunting object can the hunting object complete the hunting operation. However, it is not as simple as it looks to build a person and a gun. Here we take a gun as an example, to create a gun, we need metal, machine tools, and bullets. Machine tools and bullets are two new objects. These objects are nested and correlated with each other, imagine how complicated it is if we construct a gun object in java code. If we want to construct a more complex aircraft carrier instead of a simple gun object, the cost of constructing this object is unimaginable. How can this problem be solved?Eliminate the relationship between such objects and their mutual nesting? Spring provides a method in which spring provides a container. We define the dependencies of each object in the xml file, and the container builds the object, when we need to use an instance in java code, we can get it from the container, so the object construction operation is taken over by the spring container, so it is calledControl reversalControl inversion means that the function of building an object in a java program is taken over by the container. Dependency injection means that when the program wants to use an object, the container will inject it into the program, this is calledDependency Injection. In java Development, we want to use the functions provided by a class. One method is to construct a new class and the new class inherits the class, another way is to define a class in the new class, so the two classes establish an association relationship, the ioc container of spring implements this Association (remember that it is not an inheritance relationship). So what can be done to assign a class to a new class? Generally, there are only two types: constructor and setXXX, which are two standard injection methods used by spring containers.

Both the Inheritance Method and association method mentioned above are development methods to enhance the target object capability. There isProxy ModeProxy mode combines the inheritance mode and association mode. Proxy mode is a combination of inheritance mode and association mode. However, the role of proxy mode is not to solve the problem of object injection, instead, find a nanny or secretary for the specific operation object. This is the same as head 2 in the novel. This head 2 represents the actual instance object, the entry and exit of the Instance Object are all through this no. 2 head, because the specific instance object is No. 1 Head, and No. 1 head is to do things, so some transactional and repetitive work such as tea, arrange the car. This kind of work is not to bother head 1's big drive, but to help head 2 solve the problem. This isIdea of aop,Aop solves transactional issues in program development and is irrelevant to core services.But these problems are necessary for the implementation of business scenarios. In actual development, aop is also a way to save code.

The essence of Spring's core technology isCommunication MechanismSpring always tries its best to smooth the communication between the two parties and reduce the communication cost. A person who is good at communication in a real institution must be the company's leader, leaders who can communicate with each other can mobilize the enthusiasm of various resources, and those who are good at communication will be able to follow different people in Haina baichuan, so today's spring is a big box, everything can be installed in the past.

Spring is like a bank. It cannot directly create material wealth, but all resources need to flow through it. It can control the trend of economic development and return to the world of programs,Spring is advertised as decoupling between programs. spring can reduce coupling between different modules.The reason is that information communication between different modules in program development is completed through object transmission, and whether objects can be smoothly transmitted is to properly construct objects, the Building Method of managed objects can manage object delivery, which is the benefit of spring for system architecture design.

Related Article

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.