Struts and tiles assist in component-based development (1)
Combine views to facilitate web application Construction
The "Model-View-controller (MVC)" framework is used to generate modular applications (these applications can clearly divide the logic, style, and data ), is a proven and convenient method. In the Java World, Struts is one of the most famous and frequently-talked MVC open source code embodiment. Developers dedicated to struts have recently enhanced the core functions of the project and improved view support (combined with the tiles view component framework to enhance support for Component-Based Development ), in order to increase reusability and enhance consistency. In this article, wellie Chao explains why struts and tiles combination is the best toolkit for creating web applications and shows you how to get started with it, the changes made since struts 0.9 are described here.
I started to learn how to create software that dates back to the middle of 1980s and spent the first two months writing a single block of code using linear streams and embedded display, logic, and data. At that time, it seemed like the most common method. With rich experience, I learned to encapsulate code into objects, separate data from logic, reconstruct public code, and practice other well-recognized programming principles.
Web developers accept MVC
In 1994, the development of mainstream Web applications was just getting started. Due to the immature web, only a few tools can help developers build Web software. As a result, applications in a specific solution mix HTML code with application logic. Obviously, changes to the UI design and updates to the business logic are both difficult and expensive in large applications, because tightly coupled representations and logic bring these two elements together, this leads to errors and slow progress. In addition, mixed code requires some developers to have the UI design knowledge, or require developers and graphic designers to have a close working relationship, which often leads to a waste of time.
The introduction of JSP technology and tags slightly improves this change because the logic and display can be separated. The UI Designer can work effectively on the display, while the developer can focus on the logic. However, this method still has some defects. In particular, the development of some operations (and public operations) is still very difficult. A verification form is a typical example. As many people know, Form Verification is like this:
- Display form; wait for the user to fill in and then submit the data.
- Check whether the values of each field are valid. If an error occurs, the form is displayed again.
- Processing user input data may store it in a database.
- Display the processing result or next step (possibly another form) to the user on the new page ).
If you only use JSP pages in this process, when you need to change the code again, you will find that, according to the manageability concept, it is difficult to "Route" control from one page to another. Do you want to place step 1 and Step 2 on the same page? If multiple separate JSP pages are used, how can we track which pages link to other pages and how to change the file name or location of a page? In addition, how to re-display the original form with an error message when detecting an error in a domain in step 1, but keep the value that the user has filled in? Struts is an open-source "Model-View-controller" framework that helps solve all these problems, making it easier for developers to work.
This article does not discuss the MVC platform in depth. For more information, see the section titled "struts, an open-source MVC implementation" in Malcolm Davis.DeveloperworksArticle. The article you are reading discusses the changes made to struts since the publication of Malcolm, including the tiles library. As for the code installation process, this article only involves the steps required for the minimum installation of Jakarta Tomcat 4.0 (Catalina. If you are not using tomcat, read the manual to learn about your application server.
Background Knowledge about struts and tiles
Craig Mcclanahan is the technical director of the Apache Tomcat project. He created the struts project to satisfy his desire for this. As the first choice of the "Model-View-controller" framework and officially recognized open source code implementation, it has become increasingly popular. It is distributed along with delivery products and is supported by Sun and IBM. Because Craig is actively involved in Tomcat and Struts development, Struts will continue to be highly compatible with the reference implementation of JSP and Servlet specifications, and thus be highly compatible with all J2EE application servers.
Malcolm Davis's Struts-relatedDeveloperworksThis article covers the functions of struts 0.9. to be brief, I will only discuss the changes to struts 0.9 and the struts topics that are not involved in it. The current struts release version is 1.0.2, but a beta version marked as March 19, 2002-B1 has been available since January 1, 1.1. Because the beta version indicates that encoding is completed and only error correction is performed in this case, it is very likely that the final version of 1.1 will be available soon, this is already in the struts email list. Therefore, any new project that uses STRUTS may use code 1.1 as the basis, which is what I will discuss.
The useful addition to struts frameworks since version 0.9 includes improved form verification functions, the ability to specify form elements through XML declarations, and the ability to dynamically define bean features. However, the most important addition may be to merge the tiles template library into the struts distribution edition.
Have you ever wanted to create a group of pages (or possibly the entire application) in a simpler way ), in addition, the user interfaces on each page are consistent-with the same navigation bar, header, and footer? How can I display the rectangular content of multiple Portlet classes on a page with more content? With the help of the tiles framework, you can complete these two tasks and other tasks. You can insert static and dynamic content by defining a screen and a set of core XML files that can be embedded in the JSP page. The tiles framework allows you to build a componentized view, and assemble them as you wish to improve flexibility, reusability, consistency, and maintainability.
The interaction between struts and tiles is good, because the developers of the two projects have realized that the two are complementary, so they decided to make the two work together. Developers can specify the tiles page definition as the target view for struts operations (according to strutsForward). Because both struts and tiles comply with the JSP tag library specifications, you can mix the struts tag and tiles tag on the JSP page.
You may be eager to try the tiles framework and learn exactly what it can do. If you want to know the running status of these examples before installing the examples in this article, you can see how they run on the JBoss server with embedded tomcat.
Struts and tiles are auxiliary tools for web development, so you need to set a Web container to experiment with them; Set tomcat to your container, and then set struts and tiles packages, I will explain this process step by step in the next section. These guides also show you how to install the sample code in this article. Once all this is done, you are ready to continue this article. Example 1 the application did not use struts and tiles; it demonstrated a page-centric approach. By comparing it with example 2, you will see how struts and tiles greatly improve the structuring and manageability of your web development. Finally, Example 3 demonstrates how easy it is to add a feature to a web application that uses STRUTS and tiles that has been started and is running.