Seven experiences of Jakarta struts application

Source: Internet
Author: User
Tags exception handling extend versions

Editor's note: This article describes the author's Hard-won experience in the process of using struts. If you're a Java programmer who develops web apps for JSP and Servlet, and you're considering a build based on struts, you'll find a lot of insightful and valuable information here.

1. To consider extending the struts framework only when necessary

A good framework has many advantages, first of all, it must be able to meet the user's predictable needs. For this reason, struts provides a common architecture for Web applications, so that developers can focus on how to solve real-world business problems. Second, a good framework must also be able to provide extension interfaces where appropriate, so that applications can extend the framework to better suit the needs of users.

It would be great if the struts framework met the needs of any project on any occasion. But in reality, no framework claims to be able to do that. There must be some specific application requirements that the developers of the framework cannot foresee. Therefore, the best way to do this is to provide enough extension interfaces to allow development engineers to adjust struts to better meet their specific requirements.

There are many places to expand and customize in the Struts framework. Almost all configuration classes can be replaced with a custom version of a user, which can be done simply by modifying the struts configuration file.

Other components, such as Actionservlet and Requestprocessor, can also be replaced with custom versions. Even the new features in Struts 1.1 are designed in accordance with the principle of extension. For example, the exception handling mechanism allows the user to customize the handle of exception handling to better respond to errors that occur in the application system.

As a framework for this kind of adjustable features in its more suitable for your application, but also to a large extent, the impact of the project development effect. First, because your application is based on an existing, mature, stable framework such as struts, the number of errors found in the testing process will be greatly reduced, while also reducing development time and resource input. Because you no longer need to put in the development power to write the code for the underlying framework.

However, achieving more functionality will cost more. We must be careful to avoid unnecessary misuse of extended performance, which is composed of a core package and many toolkits that already provide many of the features that have been implemented. Therefore, do not blindly extend the struts framework, first determine whether the other methods can be used to implement the existing functionality. Make sure that struts does not implement the functionality you want before you decide to write the extension code. Otherwise, repetitive functionality can lead to chaos and extra effort to clear it up in the future.

2. Using exception handling declarations

To define the logical process of an application, the proven experience is that it is recommended to be implemented in a configuration way outside of the code, rather than in the program code. In the Java EE, such examples abound. From implementing the security and transactional behavior of EJBs to describing the relationship between JMS messages and destinations, many run-time processing processes can be defined outside of the program.

The struts creator used this approach from the outset by configuring the Struts configuration file to customize the various aspects of the application's runtime. This is a continuation of the new features of version 1.1, including new exception handling capabilities. In previous versions of the Struts framework, developers had to deal with the error conditions that occurred in struts applications themselves. In the latest release, the situation has changed dramatically, and the Struts framework provides a built-in class called Exceptionhandler, which is used by the system defaults to handle errors generated by the action class running. This is one of many extensible interfaces that we mentioned in the previous tip.

Struts the default Exceptionhandler class generates a Actionerror object and holds it in the appropriate scope object. This allows the JSP page to use the error class to alert the user of any problems. If you think this does not meet your needs, then it is convenient to implement your own Excepionhandler class.

Methods and mechanisms of specific custom exception handling

To customize your exception handling mechanism, the first step is to inherit the Org.apache.struts.action.ExceptionHandler class. This class has 2 methods to cover, one is Excute () the other is Storeexception (). In most cases, you only need to overwrite the Excute () method. The following is the Excute () method declaration for the Exceptionhandler class:

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.