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 What kind of benefits, the idea of this problem, I found new questions, why now a lot of Java Enterprise-level development will choose the spring framework, 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 say these questions, 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 a lot of technical unfamiliar and unclear, so this article is not a comprehensive answer to my topic, It is 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, that is, they are doing so well that many programmers who use it are already unsure of the true face of the original technology, so we need to understand struts2 better, use more skillfully and in depth, Here we have to jump out of the struts2 technology, to the source of STRUTS2 technology servlet, carefully study the characteristics of the servlet, only in this way we can struts2 framework to learn 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.

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 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 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.

Servlet another role is to construct the response object, so that the page to get the correct response, in fact, the modern browser is a multimedia tool, text, pictures, video, etc. can be displayed in the browser, the different resources will lead to HTTP response message differences, If we use the servlet development will be based on the different resources in the Java program in the 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 from a Java program in the form of a configuration file, and use the same configuration to manage it in a way that is similar to spring's AOP approach, so that results are handled more uniformly, more manageable, and improve the robustness of the program. and reduce the difficulty of development.

servlet in the MVC development model is where the C layer is the control layer, the control layer is like a 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 to see, A head to the V Layer View layer, the model layer is also written in Java, 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, the browser is not understand the Java language, 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, This is the Java EE Specification provides JSP technology, JSP is actually a server-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 your Web application. 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 significance of spring is really a troublesome thing, many people on spring Understanding is actually stuck in the use phase (for example: declarative transactions are very useful and so on), today's spring technology ecological environment is magnificent, spring has all-encompassing, its content is no less than its source of Java language, And the big spring boxes are built on the IOC and AOP technologies, so that we can understand why spring is so much more than just the two technologies.

  First of all, the first explanation for IOC,IOC technology 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 find out exactly how accurate the description is. The essence of IOC technology is the technique of building objects in other words, the technique of instantiating a class into an object, the instantiation of a class in Java through the new keyword, each time a new class creates an instance object, which is wasteful and sometimes dangerous. Because in the program development time we often only need a class can always produce only one instance object this time we have to use a singleton mode, in addition, in the design mode can also be factory-generated objects, the use of spring people see the above text will know, Spring bean definition is Corresponding to the above content one by one, the scope property single produces a singleton object, prototype produces a new object, the bean can also be factory-generated objects, it can be said that spring Bean is the tool to make objects. Object-oriented programming is equivalent to the display of an entity in life, for example, we have an object is to complete the operation of hunting, then hunting this object contains two helper objects: Man and gun, only man and gun to the object of hunting, then the hunting object to complete the operation of hunting, But the object of building a man and a gun is not as simple as it looks, here's a gun, for example, to create a gun we need the metal, the machine, the bullet, and the machine and the bullet are two new objects, each of which is nested in each other, and imagine if we were in Java code The object of building a gun is how complicated, if we are not to construct a simple gun object but a more complex aircraft carrier, then the cost of the construction of 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, which we can get from the container when we need to use an instance in our Java code. Then the object's build operation is taken over by the spring container, so it is called control inversion, which means that the function of building objects in a Java program is taken over by the container, and the dependency injection is when the program wants to use 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 the new class, and then create an association between the two classes. Spring's IOC container is the association (remember not the inheritance relationship), then a class to be assigned to the newWhat are the options for the class? There are generally only two kinds: one is through the constructor, one is through the setxxx way, which is also the spring container used to two standard injection methods.

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 program development, the communication between the different modules is through the object transmission, and the object can be smoothly passed is to reasonably build the object, and the management of the object to build a way to manage the object delivery, which is the benefits of spring to the system architecture design.

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.