Why do we use frameworks like Struts2,springmvc and spring in Java Web development?

Source: Internet
Author: User
Tags new set

This year I have been thinking about the problem of the separation of the front and back of web development, and now there is a lot of snacks, with this problem in depth, and now the company many Web project control layer of the technical framework moved from STRUTS2 to Springmvc, I suddenly have a new question can not get the correct explanation , why do we do Java Web Development now, would choose a framework such as struts2 or SPRINGMVC, rather than using a servlet and JSP technology? Especially now our web front-end pages are using the velocity of the template language to develop, discard the JSP, such a choice will give us Java web development of what kind of benefits, delay the idea of this problem, I found new questions, Why does a lot of Java Enterprise Development now choose the spring framework, and what does the spring framework bring to the applications we develop? Such a thought really asked me, I seem to find a bunch of people can be completely convinced of the answer, and finally I found that I think I am familiar with the most commonly used technology, in fact, there are many unfamiliar to me, these strange and puzzling place is I can use them at a higher level of the key, Today this article I talk about these issues, but struts2,spring such technology after so many years of accumulation is quite large and complex, their face is very broad, I have been using them for many years, there are many techniques are unfamiliar and unclear, So this article is not a comprehensive answer to my topic, but based on my existing knowledge structure to understand the problem.

There are a lot of good frameworks in the software, there is a type of framework, it is based on an existing technology, providing the same business functions as the existing technology framework, the new technology framework than the original technology is more user-friendly, more robust and more powerful, such as: JQuery, As well as the struts2 and Springmvc discussed in this article, it is quite complex to delve into these frameworks, but they have only one advantage: to let users only care about the core business development, the framework to help you block the original technology and business development unrelated to the various technical issues. Frameworks such as JQUERY,STRUTS2 or SPRINGMVC are excellent because they are doing so well that many programmers who use it don't know the true face of the original technology, so we want to understand struts2 better, use more skillfully and in-depth , here we will jump out of struts2 technology, to the source of STRUTS2 technology servlet, carefully study the characteristics of the servlet, only in this way can we put struts2 framework better.

The role of the servlet is to receive requests from the browser to the server and to return the service-side response (response) to the user's browser, which communicates via the HTTP protocol between the browser and the server. The process is that the browser according to the user's choice to assemble the relevant information according to the HTTP protocol Message Specification assembly request HTTP message, the message transmits through the network to the designated server, the server receives this message information through the specific Web container, for example: Tomcat,jetty,jboss such a web container , the Web container will parse the HTTP message, and if it is a user request, the final parsed message will be stored with a request object, and the server will use this request to finish the corresponding processing, and the service-side program encapsulates the result information into the response object. Then the response object is given to the Web container, and the Web container transforms the response object into a message of the HTTP protocol and passes the message back to the browser, and the browser finally parses the response message and displays the final result to the user.

The

Web container creates the Servlet interface, the Servlet interface is where the developer implements the business logic himself, Programmer development servlet is like filling in the blanks, The context or context hint of the fill-in question is the request and response object, but the servlet interface in the Java EE specification is simple, with three methods Init,service and destory, but this interface is too general, So the specification also provides a HttpServlet class, this class according to the HTTP request type provides doget,dopost and so on, the most characteristic of the servlet interface is defined according to the characteristics of the HTTP protocol, so do Servlet development time If the user is not particularly familiar with the HTTP protocol characteristics, will encounter more or less confusing problems, especially when encountering some complex special requests: such as file upload, return special file format to the browser, the use of servlet development is not very convenient, Servlet development There is a problem that may often be overlooked, is the type of data requested to convert, HTTP protocol transmission is text form, to the Web container after parsing is also the text type, if you encounter currency, numbers, dates such types require us to convert according to the actual situation, If the page is sending a lot of information, we have to do a lot of type conversions, which is not technically useful, is a physical activity and can easily lead to bugs. At the same time, Java Enterprise Development is around JavaBean, type conversion of good data to be encapsulated in the corresponding JavaBean, this kind of things for the project development is definitely not a good thing, so the old struts1 for this problem found a solution, is to define a DTO object (data Transfer object), specifically responsible for doing such things, but to the struts2, the entire alternative servlet the action itself is a javabean.

One of the technical features of Java's Enterprise development is the use of JavaBean, one of the features of STRUTS2 is that it is a typical javabean to replace the servlet's operating class. First, the STRUTS2 framework encapsulates the data of the page transfer and encapsulates the request information into this JavaBean attribute, so that when we develop the Web program, we eliminate the problem of type conversion and encapsulation. As I said before, the traditional servlet is defined according to the HTTP protocol, which processes the user's request in the way you request it (post or get), but for a program developer, a request, specific to a URL, In fact, for the service side is the service side of the external provision of a function, or a service side of the external action, if we use the servlet development program we have to translate the HTTP action into a specific business action, which makes the program development become cumbersome, enhance the development of the difficulty, So struts2 alternative servlet JavaBean block the servlet HTTP request mode and the specific Business action transformation problem, each method in JavaBean can and each URL request one by one correspondence, this inevitably alleviate the difficulty of development.

The servlet another function is constructs the response object, causes the page to obtain the correct response, in fact the modern browser is a multimedia tool, the text, the picture, the video screen and so on everything can display in the browser, the resource difference will cause the HTTP response message difference, if we use Servlet development is based on the different resources in the Java program in a hard-coded form of processing, such a program is difficult to reuse, and if the programmer to understand the processing of a resource is not in place, will lead to problems arise, Struts2 this logic out of the Java program in the form of a configuration file, and using configuration to manage it uniformly, this approach is similar to spring's AOP approach, so that results are handled more uniformly and more manageable. It also improves the robustness of the program and reduces the difficulty of development.

Servlet in the MVC development model is the C layer is the control layer, the control layer like the Russian double-headed eagle (a head to the east to look at a head to the west), a head to the M-layer model layer, a head to the V-Layer view layer, the model layer is also used Java written, the control layer is also a service-side language development, so the M layer and the C layer of communication is not a natural barrier, but and the V Layer View layer is not the same, this is a cross-language communication, for the browser, it only know Html,javascript and CSS, Browsers do not understand the language of Java, but to let the server can be understood by the browser to accept, we have to put the service side of the response information on the page, so we need a technology to translate the Java information into the HTML page, which is in Java EE specification provides jsp technology, JSP is actually a service-side technology rather than client technology, but it seems more like HTML technology, the earliest JSP development is directly to the Java code written to the page, the disadvantage of everyone knows, The Java EE specification then provided a custom label technology, using an HTML-like tag to parse the code, and the STRUTS2 framework provided a complete set of custom labeling techniques that didn't sound like much, but it did a great work, Because custom labels are called custom, everyone can define their own, if not a specification is bound to create confusion, and a complete set of custom tags is a system engineering, a full set of custom labels is equivalent to our own definition of a new set of development language, The people who do the program hear this, they'll understand. It's hard to imagine the effort and difficulty of developing a complete set of custom labels, and the custom labels are tightly connected to the control layer, and the difficulty increases by one dimension, so the custom label provided by STRUTS2 will be a qualitative leap for business development.

There are two important techniques in the servlet: listeners and filters, which are rarely used by listeners in web development, are very special cases, and most web development can ignore its use. The listener we use most probably is the listener created and destroyed for ServletContext, ServletContext is the global object of the entire Web application, and it binds to the life cycle of the Web application, This listener is therefore used to initialize and destroy the global information of the Web application, such as the initialization of the spring container. More interesting is the filter, there is an interceptor in the struts2, their role is to intercept the request, because the interceptor is the unique function of struts2, in the struts2 using the interceptor is naturally more comfortable than the use of filters, in fact, the interceptor technology is more advanced than the filter, Because interceptors use the reflection technology, the interceptor intercepts more faces, the ability to control requests is stronger, and the tasks it can accomplish are more colorful.

When I first approached Struts2, I was told that one of the goals of the struts design was to block the operation of the request and response objects in the control layer, because the sons of the two HTTP protocols would cause confusion in web development, But I often unconsciously use these two objects in actual development. And I do front-end development very like to use Ajax, when using AJAX technology I hate Struts2 's custom tags, I prefer to use JavaScript technology in the page to deal with a variety of information, the final struts2 in my eyes is a Servlet variant, so there was a time when I often wondered if I could abandon struts2 and use servlets directly, because struts2 used too many reflection mechanisms, especially with annotations (annotations are implemented with reflection), and the performance of reflection in Java is very low. , using the servlet directly will definitely improve the efficiency of Web application execution. It was hard to do, because I couldn't use spring technology flexibly in the servlet .

Now I want to talk about spring.

  Spring technology can be said to be the most important technology in Java Enterprise Development, but really understand the role and meaning of spring is really a troublesome thing, many people to spring understanding is actually stuck in the use phase (for example: declarative transactions are very useful and so on), Today's spring technology ecosystem is spectacular, spring is all-encompassing, its content is no less than its original Java language, and spring is such a large box based on the IOC and AOP technology, Only by deep understanding of these two technologies can we understand why spring is a box that can fit so many things.

First,IOC,IOC Technology The first explanation is called inversion of control, and it's also an explanation that relies on injection, which is hard to read in two names, but when you understand how it works, you'll see how accurate they are. The essence of IOC technology is the technique of building objects in other words, the technique of instantiating a class into an object,In the Java instantiation class through the new keyword, each time new class will produce a fresh instance object, it is very wasteful, sometimes this kind of waste is very dangerous, because in the program development time we often only need a class can always produce one instance object this time we have to use the singleton mode, Also in the design mode can be produced by the factory-style objects, usedspring people see the above text will know, Spring bean definition and the above content one by one corresponds to the Scope property single to create a singleton object, prototype produce a new object, Beans can also produce objects in Factory mode, which can be said that spring beans are tools for making objects. Object-oriented The object in programming is equivalent to displaying an entity in life, for example, we have an object to do the hunting operation, then the hunt this object contains two helper objects: Man and gun, only man and gun gave the object of hunting, Then the hunter can complete the hunting operation, but the object of building a man and a gun is not as simple as it looks, here is a gun, for example, to create a gun we need metal, machine tool, need bullets, and machine tools and bullets are two new objects, these objects are nested each other, the people imagine if we in the Span id= "7_NWP" >java code to build a gun object that is how complicated, if we want to construct is not a simple gun object but more complex aircraft carrier, then the cost of constructing this object is unimaginable, how to eliminate this object nested interdependent relationship? spring provides a way for spring to provide a container in which we define the dependencies of each object in the XML file and build the object by the container. When we need to use an instance in the Java code can be obtained from the container, then the object's construction operation is taken over by the spring container, so it is called control inversion, control inversion means that the function of the building object in the Java program to the container takeover, Dependency injection is when a program uses an object, the container injects it into the program, which is called dependency injection. In Java development we want to use the functionality provided by a class in two ways, one is to construct a new class, the new class inherits the class, and the other is to define a class in a new class, and then create an association between the two classes, and spring's

Whether it is the above-mentioned inheritance, or the relevance of the method is to enhance the target of the ability of the development means, in the design mode has a proxy mode, the proxy mode of the inheritance pattern and the association mode is used together, the proxy mode is a combination of inheritance and association mode, However, the function of this complex is not to solve the problem of object injection, but to find a babysitter or a secretary for the specific object of operation, and this is the same as the novel second Head, the head of the second external representative of the concrete example object, the entrance and exit of the instance object is through this number second head, Because the specific instance object is a head, a chief is to do great things, so some transactional, repetitive work such as tea, arranging cars, such work is not to bother a head of the house, but the head of the second to help solve, this is the idea of AOP, AOP solution Development in the transactional, Issues unrelated to the core business, but these issues are necessary for the implementation of the business scenario, and AOP is also a way to save code in real-world development.

Spring's core technology is the essence of a communication mechanism, spring is always doing its best to make communication between the two sides of the information unblocked, while reducing the cost of communication between the two sides, a good communicator in the real organization is the leader of the company, will communicate the leadership can mobilize the enthusiasm of various resources, Good communication leadership will do the sea to the hundred rivers, so that a variety of different people follow him, so today's spring is a big box, anything can be installed.

Spring is much like a bank, it can not directly create material wealth, but all the resources to flow through it, it can control the direction of economic development, back to the program of the world,Spring's role is advertised as decoupling between the programs, spring can reduce the coupling between different modules, the reason is that in The communication of the information between different modules in the program development is accomplished through the object transmission, and the object can be smoothly communicated is to construct the good object reasonably, and the management good object's construction way can manage the object transmission, this is spring to the system architecture design brings the benefit.

Why do we use frameworks like Struts2,springmvc and spring in Java Web development?

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.