Use your strut

zhaozj2021-02-16  48

Understanding the MVC architecture For a powerful web application built with Struts, it is important Struts as an item of Jakarta, which provides a method that can use JavaServer Pages (JSP) and servlets together in a web application. Its purpose is to resolve inherent issues in applications that are completely implemented by JSP or completely by servlet. For example, Servelts can generate an HTML page, but it is very troublesome. On the other hand, JSP can be easily used in the traditional HTML page, but the JSP page has other shortcomings. In particular, it is difficult to use JSP to separate the contents of the content. It is easy to mix the Java code with HTML, and what makes it makes slow and difficult to maintain.

However, because JSP pages are easy to use, they become the preferred method of building a dynamic web application with Java. In addition to easy programming, the JSP page is also improved, so they now overcome some of the previous limitations. JavaBeans and tag libraries are only several improvements in the foundation JSP technology. This type of method -JSP page is separately responsible for processing the request and reply client - called Model 1 architecture.

JavaServer Pages are special circumstances of servlets, so both can work together to make up for each shortcomings, which seems to be logical. This type of method - your web architecture contains distant but interconnected data patterns, display code and program control logic JSP and Servlet components - called Model 2 architecture, or Model-View-Controller MVC) architecture.

In order to use the Struts architecture and programmed with JSP and Servlets, the understanding of the MVC architecture is necessary. The main difference between Model 1 and MVC architecture is where the request is processed. In the Model 1 architecture, requests are received through JSP, mainly through JSP processing. If the JSP page requires services from any other application component, such as a database, then you will properly call from the page, return the data to the page, schedule data format and display it. You can put some code in one or more JavaBeans, but do not have logic to completely separated the logic.

The MVC method uses the best features of the JSP and Servlet methods, so that both technologies can work together. Clearly, servlet is a processing layer (controller). The servlet receives the request, which is very similar to the JSP page in the Model 1 architecture and determines how to meet those requests. This means that the request and output of the servlet control input.

The business logic embodies the mode in the MVC architecture. Business logic code is handled for page. If the request to enter the servlet is a database query, the servlet transmits this request to a SQL call or a similar database code. If the request is a purchase request including the input credit card number, then the event is taken over. In a sense, the architecture of the architecture is all why the application is in a leading position.

The JSP page is a display layer (view), which is where the user is interactive with the application. It provides input and display results. The page should not include any scripts. It just transmits data to servlet and receives and displays the returned data.

The advantage of the architecture should be apparent. First, it will be clearly separated and displayed. The results are ideal, there is no processing process on the JSP page, there is no data format in servlet or business logic. Another benefit of this separation is that the Java programmer can focus on servlet code, and HTML writers can focus on JSP. Second, the controller servlet does all the decisions on the page. There will be no decisions in your page and logic. This improves the performance and scalability of an application because requested different components that can be directed to the architecture, even different servers. The MVC architecture MVC architecture is not necessary to be the best way to use for all Java applications. As you imagine, it is often very complicated when preparing and encoding - this is unnecessary to simple applications. When the page navigation is relatively simple and fixed, and the page structure in the application can be managed by a simple directory structure, the Model 1 architecture is still the best way. These applications tend to embed the page flow information into the link between the page. (Forward in JSP "tells you that there is embedded logic in this page, let you make a decision to display the next page.) Standards for static applications with limited traffic or scalability requirements, standard JSP mode is still a viable option.

In some cases, you might want to transplant a JSP application to the MVC architecture. For example, you may start with Model 1, simple JSP architecture, as your needs increase, you will find that it is too complicated or too difficult. If you spend a long time to make a relatively simple modification, or if you often discover bugs in your code, then your JSP-oriented application may no longer be a suitable method. With the development and changes of the application, the flow of the page and the business logic have also increased. The application becomes difficult to maintain because the page flow logic is distributed across multiple pages, and commercial logic may begin to exist in unplanned places. The best time to go to MVC from Model 1 is when these types of maintenance problems occur.

You can also plan to transfer your application from an architecture to another. When your JSP page contains scripting elements, custom tags, or JavaScript to perform a Forward () operation, you may want to adjust your initial design and become a design with an MVC architecture.

Refactoring is a nice term here. It refers to a technology that reconstructs the code structure in a highly rigorous manner. When your code becomes difficult to understand, modify and debug, you usually start thinking. You can use several ways to adjust the code: You can simply rename variables and methods, or transplant some code into different types of execution modes. For more information on adjustment and technology, please visit Martin Fowler's website www.refactoring.com or read the book he writes refactoring: Improving the design of existing code.

In many cases, you have a meaningful MVC architecture at the beginning. For example, your application is designed for a wide range of business applications, or in a few years, your app may scale to a fairly high traffic. Designing a MVC architecture requires a deep concern. Most programmers have found a write logic script or JavaBeans, and it is easy to display with HTML. When you design a MVC architecture, you must first make a complete design. This means that the type of request that needs to be processed is analyzed to determine which components (JavaBean, database, or other servlets) will process these requests, and how the response to those requests is displayed to the user. The key to the design is the liquidity of request and data. Designing application components for the MVC architecture is just an extension of you now using JSP and Servlets. The challenge is to build a servlet, which receives the request and divides the requests to the different components of the application. Transfer a database request to a database, or transfer a process request to a JavaBean, but what if a request contains these two elements? Or if the nature of the request cannot be determined before some processing occurs, what? Struts architecture This challenge makes us return to the Struts architecture. The Struts provides a highly automated way to implement the MVC architecture. Its structure implements the MVC and includes a controller servlet, a set of JSP pages, and the business logic of the application. The controller requests the user to package and directs them to other objects in the architecture.

The Struts framework is surrounded by an actionmapping structure. The controller converts the user request in the form of the HTTP message into the action of the application. ActionMApping Specifies the path of the request, the scheduled to process the request, and any other information you need to serve the request. ActionMApping creates an Action object to handle the request. Once an Action object has completed a task, it will respond directly to a user request by writing results on a JSP page, or it allows an application to move to other places to respond.

Next month, I will tell the details of the MVC mode for the Struts architecture. Then, I will use Struts to design and implement a web application, which uses the Servlet controller as the "scheduling" foundation for the Action object of the program executing the user request.

For more information on the Struts architecture, see the cover articles "Struts: a solid web-app framework" written by Tim Holloway.

转载请注明原文地址:https://www.9cbs.com/read-20639.html

New Post(0)