Learn Javaweb must read the article! Why do we use frameworks like Struts2,springmvc and spring?

Source: Internet
Author: User
Tags new set

(What do we learn in the framework of Javaweb?) Why are they so popular? What did they bring to us? What did the servlet do for us? What does MVC mean? How important is spring's existence? How to understand these problems from the thought, understand Javaweb, the answer is in this article, Importnew year good text, hope more people see! Reprinted from Http://www.importnew.com/15831.html)

this year I've been thinking about the problem of front-end separation in web development, and now it's quite a snack, with the depth of the problem, Now the technical framework of the control layer of many Web projects is migrated from Struts2 to Springmvc, I suddenly have a new question can not get the correct explanation, why we now do Java Web Development, will choose STRUTS2 or SPRINGMVC such framework , 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 .

There are many good frameworks in the software, there is a type of framework, it is based on an existing technology, Providing a technical framework of the same business functions as existing technologies, a new technical framework that is more user-friendly, robust, and more powerful than the original technology, such as JQuery, and the struts2 and SPRINGMVC that are discussed in this article, it is quite complex to delve into these frameworks, but these framework benefits actually only one: is to let users only care about the core business development, the framework to help you block the original technology and business development unrelated to 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 Servlets is to receive a request from the browser to the server, and to return the service end of the response (response) to the user's browser , the browser and the server through the HTTP protocol to communicate, 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 , theWeb 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 after the server uses this request to finish the corresponding processing, The service-side program encapsulates the result information into the response object, then the response object to the Web container, the Web container transforms the Response object into the HTTP protocol message, and the message is passed back to the browser, and the browser finally parses the response message. Show the end 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, which provides doget based on the HTTP request type. Dopost and other methods,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 features in the development of the servlet, it will encounter more or less confusing problems, especially when encountering some complex special requests: For example, File upload, return special file format to the browser, At this time using servlet development is not very convenient, servlet development There is a problem may be often overlooked, that is, the request of the type of data conversion, HTTP protocol transmission is text form, to the Web container after parsing is also the text type, if you encounter currency, numbers, This type of date requires us to convert according to the actual situation, if the page transmits 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 easily lead to program 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 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 struts2, the entire alternative servlet action itself is a javabean.

Java Enterprise Development A technical feature is the use of JavaBean, struts2 is , first struts2 the framework to type and encapsulate the data that the page transmits, encapsulates the request information into this JavaBean property, so that when we develop the Web program eliminates the problem of type conversion and encapsulation problems , which I mentioned earlier that traditional servlets are defined according to the HTTP protocol, It will be in the way you request (post or get) to handle the user's request, but for a program developer, a request, specific to a URL, in fact, for the server is the service side is a function provided externally, or the service side of an 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 shield the servlet HTTP request and the specific Business action transformation problem, each method in JavaBean can and each URL request one by one corresponds, this inevitably reduces the difficulty of the development of the problem .

servlet another function , 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 the difference of HTTP response messages, if we use servlet development will be 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 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 where the C layer is the control layer, the control layer is like the Russian double-headed eagle (a head to the east to see a head to the west) like , a head to the M layer model layer, 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, 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 provides a custom labeling technique that uses an HTML-like tag to parse the code, and the STRUTS2 framework provides a complete set of Custom Label technology , which doesn't seem to sound like much, but it works great because custom labels are called custom because everyone can define themselves, and if no specification is necessarily confusing, and a well-defined set of custom tags is a system engineering, A complete set of custom labels is equivalent to our own definition of a new set of development language, the people who do the program to hear this must understand that the development of a complete set of custom tags and the workload and development is difficult to imagine, and the custom label is closely linked to the control layer, the difficulty of adding a dimension, So the custom label provided by STRUTS2 will be a qualitative leap for business development.

Servlet has two important techniques: Listeners and Filters , which are rarely used by listeners in web development, are very specific, 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's understanding of spring 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 , in the Java instantiation class through the new keyword, every time new class will produce a new instance object, it is very wasteful, sometimes this kind of waste is very dangerous, because in the process of development we often only need a class can always produce one instance object this time we have to usesingle-case mode, in addition to the design pattern, you can alsoFactory modeTo produce objects, people who have used spring to see the above text will know that the bean definition in spring corresponds to the above content one by one, the scope property single produces a singleton object, prototype produce a new object, the bean can also be factory-generated object, can saySpring's Beanis a tool for making 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 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 built 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.eliminate the interdependence of such objects in each other's nested relationshipsThe 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 calledControl Reversal, the control inversion means that the function of building objects in a Java program is handed over to the container, and the dependency injection is when the program wants to use an object, the container injects it into the program, which is calledDependency 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? 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.

Regardless of the above-mentioned inheritance mode, or the association is actually to enhance the target object ability development means, in the design mode has a proxy mode , the proxy mode uses the inheritance pattern and the correlation pattern together, the proxy pattern is the synthesis of the inheritance pattern and the correlation pattern, but the function of this complex is not to solve the problem of object injection, but to find a nanny or a secretary for the object of operation, which is like the number second in the novel. The second head of the external representative of the concrete example object, the import and export of the instance object is through this number second head, because the concrete example object is a head, a head is to do great things, so some transactional, repetitive work such as tea, arranging the car, such a job is not to bother a head of the king, It was the head of number second who helped fix it, and that's what aop thought , The AOP solution develops transactional, non-core business-agnostic issues , 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 the communication between the two sides of the information flow, while reducing the cost of communication between the two sides, in the real organization of a good communicator is certainly the leader of the company, Very will communicate the leadership can mobilize a variety of resources of enthusiasm, good communication leadership will do the Sea Na Hundred rivers, let 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 process of the world,Spring's role is advertised as a decoupling between programs, Spring can reduce the coupling degree between different modules , because the communication of information between different modules in the program development is accomplished through the object transmission, and the object can pass smoothly is to construct the good object rationally, and the construction of the object can manage the object transmission. This is what spring brings to the system architecture design.

Learn Javaweb must read the article! Why do we use frameworks like Struts2,springmvc and spring?

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.