Why should I upgrade to Struts 1.1?

zhaozj2021-02-08  387

Struts 1.1 Final is finally released. New features include support for multiple sub-applications, Dynabean and Beanutil, declarative exception handling, Validator, etc. Familiar with Struts is definitely available in Struts 1.1, below this article is to see people who are not familiar.

------------------

Introduction

Since the release of Struts 1.0, Struts has gradually become a de facto standard for MVC (aka Model-2) implementation for developing medium-to-large scale web-based applications on the Java platform. Struts fits nicely in the J2EE technology stack and Fills The Gap Which is not addressed neither by the servlet / jsp Standard or the EJB Standard. Giants Like ibm and supporting struts in their product set.

Built on the success of Struts 1.0, Struts 1.1 beta was released in March this year and showcased at JavaOne 2002 and the latest Struts 1.1 beta-2 was released just two weeks ago. The immediate question existing Struts users or evaluators will have is "Should I Now Consider To Migrate My EXISTING Applications To Struts 1.1 and start to develop my new processing on struts 1.1? "

TO ANSWER this Question, We need to consider what import new features are offfacts there is..................

New features

Instead of overwhelming you with the full list of all the new features, I Will Go Through The Most Significant Enhancements in Struts 1.1 in this section.

Multiple Sub-Applications

One of the shortcomings in Struts 1.0 is manageability of the configuration file (commonly known as struts-config.xml) when the development of the application involves a sizable team. This problem arises because a Struts-based application must run under one controller servlet, . the ActionServlet, and the ActionServlet can use only one struts-config.xml It is not an issue when the project is relatively small and the team consists of a couple of developers; however, when the project size becomes significant and the project involves a large number of developers, maintaining all the mapping information in a single file becomes increasingly problematic.Struts 1.1 solves this problem nicely by introducing multiple sub-applications. in Struts 1.1, each sub-application has its own struts-config.xml file. A Large struts-based applimention can thus be easy partitioned Into Largely Independnt Modules, IE Sub-Applications, And Each Sub-Team Can Maintain THEIR STRUTS-Config.xml Independently.

The sub-application scheme is a natural extension of the servlet context mapping scheme of the URI paths used by servlet containers. According to the Servlet standard, when the servlet container receives a request with a URL, the servlet container will try to match the prefix of the URI path to a deployed web-application in the container. What Struts 1.1 does is it maps the second prefix of the path to a sub-application. in effect, this prefix mapping scheme creates another level of namespace for each sub-application For example, for the uri,

http://some-host.com/myapp/module2/editsubscription.do

/ myapp is the context path for a web-application called "myapp" and / module2 is the sub-app prefix for a struts sub-application callaled "module2" .dynabean and beanutils

Another Major Complaint Usually Heard Amongst Struts 1.0 Users Is The Extensive Effort Involved in Writing The Formbean (a.k.a. Actionform) Classes.

Struts provides two-way automatic population between HTML forms and Java objects, the FormBeans. To take advantage of this however, you have to write one FormBean per HTML form. (In some use cases, a FormBean can actually be shared between multiple HTML forms . But those are specific cases.) Struts' FormBean standard follows faithfully the verbose JavaBean standard to define and access properties. Besides, to encourage a maintainable architecture, Struts enforces a pattern such that it is very difficult to 'reuse' a model-layer Object (Eg a valueObject from the ejb tier) AS A FORMBEAN. Combining All these Factors, A Developer Has To Spend A Significant Amount of Time To Write Tedious Getters / Setters for All The Formbean Classes.

Struts 1.1 offers an alternative, Dynamic ActionForms, which are based on DynaBeans. Simply put, DynaBeans are type-safe name-value pairs (think HashMaps) but behave like normal JavaBeans with the help of the BeanUtils library. (Both the DynaBeans and the BeanUtils library were found to be useful and generic enough that they have been 'promoted' into Jakarta's Commons project.) With Dynamic ActionForms, instead of coding the tedious setters / getters, developers can declare the required properties in the struts-config.xml files . Struts will instantiate and initialize Dynamic ActionForm objects with the appropriate metadata. From then onwards, The Dynamic ActionForm instance is treated as if it is an ordinary JavaBean by Struts and the BeanUtils library.Declarative Exception Handling

If you have developed web applications long enough, you will realize a recurring pattern emerges: when the backend (eg the EJB tier) throws you an exception, you nearly always need to display an error page corresponding to the type of that exception Sooner or. Later, You Will Come Up With A Mechanism To Use a lookup table (eg an hashmap) to lookup an error Page from the Exception Class.

Struts 1.1 now provides a similar but more powerful mechanism to declare exception handling. In Struts 1.1, you can declare in the struts-config.xml the associations between an exception class and an exception handler. Using the default exception handler included in Struts, you Can Also Specify THE PATH OF ALROR PAGES. WITH INFORMATION, STRUTS WILL Automatical Forward to The Specified Pages WHEN An Action.

Like Other facilities in struts, The Exception Handlers Are Pluggable. You Can Write and Define Your Own Handler Classes if Needed.Validator

The Validator is not exactly a new feature. The Validator has been in the contrib package in the distribution since Struts 1.0.1. Since then, part of it has now been refactored and moved into the Jakarta-Commons subproject and renamed the Commons-Validator And The Struts Specific Portion is now Called The Struts-Validator. However, Since It Is in The Contrib Package, People May Overlook It and it is. Worthwhile to Mention it.

The Validator provides an extensible framework to define validation rules to validate user inputs in forms. What is appealing in the Validator is that it generates both the server-side validation code and the client-side validation code (ie Javascript) from the same set of validation rules defined in an XML configuration file. The Validator performs the validation based on regular-expression pattern matching. While a handful of commonly used validators are shipped with the framework (eg date validator, range validator), you can always define your own ones To suit your need.

Impact

Having gone through the list of exciting new features, it is time to examine the potentially negative impact of these enchacements. One of the highest priorities of the Struts 'developer community when developing Struts 1.1 has been to achieve maximum backward compatibility with 1.0. Struts' Developers Have Definitely Done A Good Job in this Regard; However, There Are Still Some Issues You NEED TO BE AWARE OF.

Default Sub-Application

To maintain backward compatibility, Struts 1.1 allows one default sub-application per application. The URI of the resources (ie JSPs, HTMLs, etc) in the default sub-application will have an empty sub-app prefix. This means when an existing 1.0 Application IS "Dropped" Into Struts 1.1, THEORETICLY, IT WILL Automatic Become The Default Sub-Application.direct Requests to JSPS

To take the full advantage of sub-application support, Struts 1.1 stipulates the requirement that all requests must flow through the controller servlet, ie the ActionServlet. Effectively, this means all JSPs must be fronted by Actions. Instead of allowing direct requests to any of The JSPS, All Requests Must Go Through an action and let the action forward to the appropriate jsp.

This is perhaps the biggest impact of migration to Struts 1.1 if you have not followed this idiom in your applications This restriction is required because without going through the ActionServlet, Struts navigation taglibs. (Eg and ) Used in the JSPS Will Not Have The Correct Sub-App Context To Work with.

ACTIONSERVLET Configurations

With the introduction of sub-applications, a more flexible way is introduced to configure each sub-application independently. Many of the configuration entries (eg resource bundle location, maximum upload file size, etc) that used to be defined in web.xml have Now Been Moved to Struts-Config.xml. The Original Entries in Web.xml Are Deprecated But Will Still Be Effective.

Path-mapped actions

Both extension-mapped (eg * .do) and path-mapped (eg / do / *) Actions Are Supported in struts 1.0. HoG, at the time of write (August 2002), MULITPLE SUB-Applications Will NOT Work with Path- Mapped actions. it is likely this issue will be resolved in the final release of struts 1.1.action.execute () And action.getResources ()

In Struts 1.0, request handling logic is coded in Action.perform ();. However, Action.perform () throws only IOException and SevletException To facilitate the new declarative exception handling, the request handling method needs to throw Exception, the superclass of all The CHECKED Exceptions. Therefore, to Both Maintain Backward Compatibility and Facilitate Declarative Exception Handling, Action.Perform () is now Deprecated in Favor of action.execute ().

You also have to be careful if you use DispatchAction in your existing applications. At the time of writing, the DispatchAction in Struts 1.1 beta has not yet been updated to use execute (). (A bug report has been filed in Struts' bug database .) The Dispatchaction Class Yourself, Declarative Exception Handling Will Not Work with Dispatchction Subclasses.

In addition, Action.getResources () is now deprecated. Instead, you should call Action.getResources (HttpServletRequest) instead. This allows Struts to return to you the sub-application specific message resources. Otherwise, the message resources for the default sub- App will be used.

Library Dependency

Struts 1.1 now depends on a handful of libraries from other Jakarta subprojects (eg Commons-Logging, Commons-Collections, etc.). Some of these libraries may cause classloading conflicts in some servlet containers. So far, people have reported in the mailing list the classloading problem of commons-digester / jaxp1.1, and commons-logging causing deployment difficulties in Struts 1.1 applications running on Weblogic 6.0. (The problems have been corrected in Weblogic 6.1 and 7.0.) Resources under WEB-INF

According to the Servlet specification, resources (eg JSP files) stored under WEB-INF are protected and can not be accessed directly by the browsers. One design idiom for Struts 1.0 is to put all the JSP files under WEB-INF and front them by Actions SO That Clients Cannot ILLEGALLY Access The JSPS.

With the introduction of sub-application prefixes in Struts 1.1, mapping resources under WEB-INF gets complicated. Extra configuration steps utilizing the pagePattern and forwardPattern attributes of the element in struts-config.xml is required to inform Struts to construct the Paths Correctly. More specification, you need to set these attributes to the pattern "/ web-inf / $ a $ p".

Miscellenous Issues for Advanced Uses

The Following Issues Are Relevant To You If You Are A Power Struts User And Make Use of Some Advanced Features in Struts:

If your application depends on the ActionFormBeans, ActionForwards or ActionMappings servlet context attributes, you have to get the information they need from the ApplicationConfig object instead. If you subclass ActionServlet, you may have to retrofit it back to that of Struts 1.1 or the new RequestProcessor Class as an actionServlet Has Been Refactored.

Conclusion

SO, "SHOULD I consider Upgrading to struts 1.1?" Here Are My Recommendations.

For EXISTING SMALL PROJECTS

IF you have a relationively small project and is happy with what struts 1.0 Currently Offers, It Makes Sense To Stay with Struts 1.0.

For EXISTING MedIUM-to-Large Projects

If you have a large project, the multiple sub-application feature is definitely the overriding factor in influencing your decision The litmus test is to ask yourself this question:. "Is the existing struts-config.xml file getting very difficult to manage due to its sheer file size? "If the answer is yes, you should consider doing a pilot trial to replace Struts 1.0 with Struts 1.1 in your development environment without partitioning your application into sub-applications first.

Since Struts 1.1 can treat your existing application simply as the default sub-application, the replacement should be straightforward. However, to test if your application is actually Struts 1.1 compliant, you should configure it as a sub-application with a non-empty prefix and go through some representative use cases to test and see if the application works as it used to be. Before you embark onto the pilot trial, you have to make sure all of your developers are aware of the issues listed in the previous section, particularly ............... ..

Once the pilot trial is successful and you decide to migrate to Struts 1.1, you can start to work out how to partition the application into sub-applications with adequate granularity. Being able to partition the application into sub-applications, you may also want to Connider reorganizing your team structure and the project structure of your source code control to align with the parting.for New Projects

If your project is a new one, the decision to use Struts 1.1 mainly depends on the amount of risk your project can take. Struts 1.0 is a proven technology but Struts 1.1 is still in beta. Besides, migrating backwards is always more difficult than migrating forwards. Once you start with Struts 1.1, it will involve a lot of hard work to go back to Struts 1.0. While the extra risk in using Struts 1.1 is generally very low (as the quality of work from Apache's developer community has been consistently very Good, IT Is Still A Non-Negligible Factor You Have to Consider.

About the Author John Yu is the co-founder and chief scientist at Scioworks Technologies, an enterprise software consulting and products company based in Singapore. He is the primary designer of Scioworks Camino, a visual modelling tool for developing Struts applications. John's J2EE / EJB experience dates back to a pilot project he led implemented on Tengah, what Weblogic AppServer was called before Weblogic was acquired by BEA, when he was a designer / architect working in Australia. John holds a Masters degree of Computing from Monash University, Australia and a POSTGRADUATE DIPLOMA in Management from Melbourne Business School, Australia. He Was Awarded The Telstra Research Fellowship and The Australia Postgraduate Award.

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

New Post(0)