Struts and tiles assist in component-based Development (2)

Source: Internet
Author: User
Struts and tiles assist in component-based Development (2)

Install struts and tiles

On Linux machines with j2se 1.4 SDK, ant 1.4.1, Tomcat 4.0.3, and Struts 1.1-B1, the following instructions have been tested. If you encounter difficulties due to different versions of these software packages, you may need to use the version specified above to get started with setting and development of struts and tiles.

  1. If you don't have j2se 1.4 SDK (Java 2 platfrom, standard 1.4 Software Development Toolkit), get it from the http://java.sun.com/j2se/1.4/download.html and install it following the instructions in the package. You need the SDK instead of the JRE, but you can choose to obtain the Forte/SDK combination.
  2. Make sure that your environment variable java_home is set to the installation directory of j2se 1.4 SDK.
  3. If ant 1.4 is not available, obtain the binary distribution from the http://jakarta.apache.org/builds/jakarta-ant/release/v1.4.1/bin/ and decompress it. For ant 1.4 and all other packages required in the installation guide, make sure that they are obtained in the binary distribution instead of the source code. Otherwise, you will have to compile these packages before using them. Similarly, add the ant bin directory to your path.
  4. Get Tomcat 4.0.3 binary distribution from the http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.0.3/bin/ and decompress it. The file name should be similar to the jakarta-tomcat-4.0.3-LE-jdk14.tar.gz. To make it easier to reference this name in these guidance information later, let's temporarily call the installation directory (to and including the Tomcat directory) The tomcat_home path. On UNIX systems, the path is similar to/home/wchao/jakarta-tomcat-4.0.3-LE-jdk14 and similar to C:/jakarta-tomcat-4.0.3-LE-jdk14 on Windows systems.
  5. Obtain the Struts 1.1-B1 beta test release from the http://jakarta.apache.org/builds/jakarta-struts/release/v1.1-b1/ and decompress it (not in tomcat_home ). We call this directory struts_install. On UNIX systems, the directory is similar to/home/wchao/jakarta-struts-1.1-b1, similar to C:/jakarta-struts-1.1-b1 on Windows systems.
  6. Download the struts-tiles-examples.tgz and decompress it. It creates three directories: ex1, ex2, and EX3. These directories are called exdeskinstall, ex2_install, and ex3_install respectively.
  7. Go to the tomcat_home/bin directory.
  8. Enter./startup. Sh (if running UNIX) or./startup. BAT (if running Windows) to start the Tomcat server.
  9. Direct the web browser to http: // localhost: 8080/examples to verify that Tomcat is started and runs correctly. By default, Tomcat comes with the examples application. If examples does not work, Tomcat fails. See the Tomcat documentation to solve the problem.

Hello, world: First Attempt

To study our first example, follow these steps:

  1. Go to the exdeskinstall directory.
  2. Edit the build. xml file and enter appropriate values for Tomcat. Install. dir. Although the value can be an absolute or relative path, if you do not know how ant works, it is best to use an absolute path.
  3. Enter ant deploy. This will build the first example application to the war file for deployment, and then deploy it to Tomcat. If you get an error indicating that ant cannot be found, see step 3rd in the "Install struts and tiles" section and make sure that your path environment variable contains ant.
  4. Direct the web browser to http: // localhost: 8080/ex1. You will see the "Hello, world" page.

Example 1 the web application is very simple and demonstrates common web application functions. Almost all applications (including the simplest application) require that all pages have consistent user interfaces. Typically, this means that all pages have a public logo, top bar, top or left navigation bar, subject, and footer. In example 1, I intentionally hardcoded the public items on each page to illustrate this. New users of Web application development generally copy and paste existing code to the new document to add a new function page. It is easy to predict that this method is difficult to cope with future changes. Each time new content is added, the process of changing public page elements such as menus and logos takes longer, making it more error-prone. Obviously, the copy and paste method is a bad model for any application with a large number of pages.

A keen reader will realize that JSP technology provides functions including content from other servlets and pages. Why can't we just use the <JSP: Include/> tag to merge public elements? This will certainly make those elements easier to change. To change the menu, you only need to change the file that contains the menu. For all other pages, you only need to use the <JSP: Include/> flag to get the content in the menu, so that these pages can automatically get changes to the menu. However, this method is flawed when you need to change the actual layout or re-organize files and directories. When you decide to change the layout of a page-centered model, you must change each page, even if the access to public elements has been centralized, however, the layout itself is still described by the HTML code on each page (which elements and their locations are included ). Similarly, when you decide to change the file name or location that contains a public element, you must change the files that use this element one by one. Why? Because each file searches for each common element based on a fixed physical file name rather than a logical object name. Therefore, each reference to the physical file name must be updated. The tiles view component can solve these problems.

If we further study index. jsp and form1.jsp (the two JSP files constitute the application), we will find another drawback: the error processing is rather clumsy. The error handling code is in form1.jsp, where I must repeatedly display the code and add the code to insert the value entered by the user in the previous form screen (index. jsp. If the user profile information field has been changed, or if the display of the input form has been changed, the code in these two places must be updated. I can compare the error handling part of form1.jsp with the index. the initial form display in JSP is combined, but when the initial form is loaded, I still need to do additional work to set the field value to an empty string, in addition, I still need a physical file name to indicate the final static display of the user profile, which means that the application structure is still very fragile when the change occurs. Struts form automation can solve this clumsy form processing defect.

Table 1 below summarizes the advantages and disadvantages of a JSP-based, page-centric web application model demonstrated by Example 1 application.

Table 1. Overview of JSP-based methods

Advantages Description
Easy to get started. You only need to set tomcat, and then you can start coding. You do not need to use core files for synchronization or lengthy initial database configuration. The connection is specified by each separate JSP page or servlet.
Disadvantages Description
It is difficult to reuse the representation in different parts of the application. To some extent, you can use the <JSP: Include/> tag to solve some reuse, but they do not work well in managing changes.
Common Input and data processing tasks are boring and repetitive. Error handling is a common problem on common JSP pages. In addition, it is time-consuming and boring to manually enter the form values and manually retrieve those values in the request object.
The business logic and representation are closely coupled to combine the codes of the two. If you study index. jsp and form1.jsp, you will find that Java code is mixed with HTML code. The code is ugly and error-prone, and it is very difficult to separate Java coding or user interface development. In the end, we had to understand the role of HTML and Java encoding on the page.
There is no centralized description of the application stream or behavior. Unless you view the pages one by one, you cannot understand the overall impression of the application and how the operation flow works. As the project grows, errors, failures, and puzzles may occur.

 

Related Article

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.