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, 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.
The Web container creates the servlet interface , the Servlet interface is where the developer implements the business logic itself, and the programmer develops the servlet as a fill-in, while the context or context hint of the fill-in question is the request and the Response object , but the servlet interface in the Java EE specification is simple, three methods Init,service and Destory, but this interface is too general, so the specification also provides a HttpServlet class, This class provides methods such as Doget,dopost based on the HTTP request type, and the most important feature of the servlet interface is defined according to the characteristics of the HTTP protocol, so if the user is not particularly familiar with the HTTP protocol characteristics when doing the servlet development. 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 may be often overlooked, Is the type of the requested data conversion, HTTP protocol transmission is the text form, to the Web container 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 to send a lot of information, we have to do a lot of type conversion, This kind of work has no technical content, is a physical activity and can easily lead to procedural errors. 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 project development is absolutely not a good thing, so the ancient struts1 for this problem found a solution is to define a DTO object (data Transfer object) that is specifically responsible for doing such things, but in struts2, the entire alternative servlet 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.
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 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 level of the V Layer View layer, the model layer is written in Java, the control layer is also a service-side language development, So the M layer and the C layer of communication there is no 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 of things, But in order for the server to be understood by the browser, we have to put the response information on the server to the page, so we need a technology to translate the Java information into the HTML page, which is a JSP technology provided in the Java EE specification, JSP is actually a service-side technology rather than the client technology , but it seems more like HTML technology, the earliest JSP development is directly to the Java code written to the page, this disadvantage everyone knows, after the Java EE specification provides a custom label technology, using a similar HTML tag way to parse Java code, The STRUTS2 framework provides a complete set of custom labeling techniques , which doesn't seem to sound like much, but it works very well, because custom labels are called customizations that everyone can define by themselves, without a specification that inevitably creates confusion, And a perfect set of custom tags is a system engineering, a complete set of custom labels is equivalent to our own definition of a new development language, the program of people to hear this must understand that the development of a complete set of custom tags and the workload and development difficulty is unimaginable, and the custom label is closely linked to the control layer, The difficulty adds a 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 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 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, The bean definition in spring corresponds to the above one by one, the scope property single produces a singleton object, prototype produces a new object, and the bean can also produce objects in Factory mode, which can be said that spring's bean is the tool to make the object. 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 metal, we need machines, we need bullets, and the machine and the bullets are two new objects, each one of which is nested and interrelated, Guys, imagine how complicated it would be if we were to build a gun object in Java code, and if we were to construct a more complex aircraft carrier than a simple gun object, then the cost of constructing the object would be unimaginable, and how can we eliminate the interdependent relationship between these objects? 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 implementation of this association (remember not the inheritance relationship OH), then a class to be assigned to the new class what is the way? Generally only twoType: 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? Reproduced