Jakarta Struts application

  Abstract: The Jakarta Struts 1, only when necessary to consider extending Struts framework of a good framework has many advantages, first of all, it must be able to meet user demand for the foreseeable.    To this end Struts Web application for a generic … 

  Jakarta Struts application 
  1, and only when necessary to consider extending Struts framework of a good framework has many advantages, first of all, it must be able to meet user demand for the foreseeable.    Struts Web application to provide for a common framework so that developers can concentrate on how to solve actual business problems.    Secondly, a good framework must also be able to provide the appropriate local expansion interface for applications to extend the framework to better adapt to the actual needs of users. 

  If Struts framework on any occasion, any good project can meet the demand, then it is fantastic.    But in fact, not a framework that can do this.    There will be some specific application requirements, the development of the framework is not expected to have the.    Therefore, the best solution is to provide adequate expansion interface, making development engineers can adjust the struts to better meet their specific requirements. 

  Struts framework in a lot of places available in the expansion and customization.    Almost all of the configuration can be replaced by a customized version of this is as simple as amend the Struts configuration file can be done. 

  And other components such as ActionServlet RequestProcessor can also use custom versions instead.    Even Struts 1.1, new features can also extended in accordance with the principles of design.    For example, in exception handling mechanism to allow the user to customize the handle Exceptions to the application of a better system to respond to the mistakes. 

  This adjustable as a framework of its more suitable for your application is also a great extent, affected the project development results.    First of all, because your application is based on an existing mature, stable framework like Struts, found in the testing process will greatly reduce the number of errors, but also to shorten development time and reduce resource inputs.    Because you no longer need to invest in developing power for the preparation of the basic framework of the code. 

  However, to achieve greater functionality is to spend a greater price.    We must be careful to avoid unnecessary expansion of the abuse of performance, is Struts from the core package with a lot of tool kits, and they have already provided a lot of the functions. 

  Do not blindly expand Struts framework, make sure not to use other means to the use of existing capabilities to achieve.    Decided to expand in the preparation of the code must be confirmed before Struts realize you do not have to function.    Otherwise, duplication of functions will lead to confusion in future must spend extra energy remove it. 

  2, the use of exception handling statement to the definition of application logic processes and mature experience is recommended in the code, the allocation method used to achieve, rather than write died in the code.    In J2EE, such examples abound.    EJB from achieving the safety and sexual affairs to describe JMS message and the relationship between destinations, many of the operational processes are in the process of definition. 

  Struts founders from the start this approach, configured Struts configuration files to customize applications running in various areas.    This is the new version 1.1 features, continue, including the new exception handling functions.    Struts framework in the previous version, developers will have to deal with Struts application of the wrong situation. 

  In the latest version, the situation has changed, Struts Framework provides a built-in a class called ExceptionHandler, action for the system's default handling of the error in the operation.    This is also in the skills of a framework we mentioned one of the many interface can be extended. 

  Struts default ExceptionHandler category will generate a ActionError object and save it in the appropriate range (scope) object.    This will allow the use of the wrong type JSP pages to remind users what problems may arise.    If you think this does not meet your needs, then can easily achieve your own ExcepionHandler category. 

  Exception Handling specific customization of the methods and mechanisms to customize their exception handling mechanism, the first step is inherited org.apache.struts.action.ExceptionHandler category.    This class has two methods can be covered, one excute () is another storeException (), in most cases, which only need to cover the excute () method. 

  As you can see, this method has several parameters, including the original anomaly.    ActionForward method returns an object, exception handling for the end of controller will request should be forwarded to the places. 

  Of course you can achieve any treatment, but generally, we must check the abnormal dished out, and against the specific type of abnormal processing.    Default, the system's abnormal processing is to create a wrong information, the request forwarded to the configuration file to the designated place. 

  Custom Exception Handling a common example is the processing of nested anomaly.    Assuming that the anomaly contains nested abnormal, these nested abnormal contain other anomalies, we must cover the original execute () method, the preparation of each abnormal error messages. 

  Once you have created their own ExceptionHandler category, it should be in the Struts configuration file in this part of the statement, to allow you to use Struts know that the definition of exception handling since replacing the default exception handling. 

  Can be configured your own ExceptionHandler Action Mapping category for a specific part or all of Action objects.    If it's for Action Mapping specific on the part of the elements of the configuration. 

  If want this class can be used for all the Action object can be specified in the elements.    For example, suppose we have created Exception class CustomizedExceptionHandler for all categories of Action, in the elements of the many attributes can be set. 

  In this paper, the most important attribute is the handler attribute, the value of the attribute handler is inherited from the definition of the category of Class ExceptionHandler full name.    If the property is not defined, Struts will use their default values.    Of course, other attributes are important, but if we want to cover the default exception handling the case, handler is the most important attribute. 

  Finally, it should be noted that the point is that you can have different exception handling to deal with different types of anomalies.    In the above example, to deal with any java.lang.Exception CustomizedExceptionHandler subclass.    In fact, you can define multiple abnormal class, each devoted to different abnormal tree.    The following XML fragment explain how to configure to achieve this. 

  Here, once abnormal dished out, the struts framework in the configuration file will try to find ExceptionHandler, if not found, then struts along the Father of the anomaly linked up at various levels to find a match until found so far.    Thus, we can define a hierarchical structure of the abnormal handling relations, in the configuration file has been shown this point. 

  3, use of the application module (Application Modules) 

  Struts 1.1 is a new application of the concept of modules.    Application Module allows a single application Struts divided into several modules, each module has its own Struts configuration file, JSP pages, Action, and so on.    This new feature is to address the development of large and medium-sized contingent of up to complain about a problem that, in order to better support parallel development allows multiple configuration files rather than individual configuration files. 

  Note: In the early beta version, known as the characteristics of the application (sub-applications), recently renamed the purpose is to reflect their more logical division of labour. 

  Obviously, when many developers get together to participate in a project, the single Struts configuration file very easily lead to resource conflicts.    Struts application modules in accordance with the functional requirements to allow divide, in many cases this has proved more close to reality.    For example, suppose we have to develop a typical store applications. 

  Can be divided into components of the module such as catalog (Catalog), customer (customer), customer service (customer service), order (orders).    Each module can be distributed to a different directory, so the part of the resources are very easy positioning, help develop and deploy. 

  Note: If you do not have the project divided into a number of modules, the Struts framework, in support of a default application modules.    This makes applications can be created under version 1.0, have portability, because applications will be automatically as the default application modules. 

  In order to use the multi-application modules, must be prepared to implement the following steps: 

  For each application modules to create an independent Struts configuration file. 

  Configuration Web deployment descriptors Web.xml file. 

  Org.apache.struts.actions.SwitchAction procedures used in the module to achieve between Jump. 

  The establishment of independent Struts Struts configuration file for each application module must have their own configuration files.    Allowed to create their own independent of the other modules of Action, ActionForm, exception handling or even more. 

  Continue to be above the shops application as an example, we can create the following configuration: a document called struts-config-catalog.xml, including catalog (Catalog), items (goods inventory), and other inventory-related functions configuration information; Another document, entitled struts-config-order.xml, including the order (orders), and order tracking (order tracking) settings.    The third configuration is the struts-config.xml file, which contains the default application modules belonging to the general function. 

  Configuration Web deployment descriptors in the early version of Struts, we Web.xml Struts configuration file specified in the path.    Fortunately, this did not change, help backward compatibility.    But for a number of application modules, we need to Web deployment descriptors in the new configuration file settings. 

  The default applications (including an early version of Struts), Struts framework document in Web.xml View with config element for Action mapping included in the application and other settings. 

  Note: If you can not find the existing elements of the "config" keywords, Struts framework will use the default / WEB / struts-config.xml 

  In order to support multiple applications module (Struts 1.1 new features), the additional element must be increased.    With the default element difference is that the additional elements of each application module counterparts, it is necessary to config / xxx name in the form, string xxx behalf of the module only by name.    For example, in the shops application examples, elements can be defined as follows (Note bold): 

  The first element corresponding default application modules.    The second and third elements representing the non-default application modules catalog and order. 

  When Struts printed in the application, it first printed in the default application module configuration files.    Then find a string config / xxx form of additional initialization parameters.    For each additional configuration files also include analytical and memory.    Upon completion of this step, the user can freely use the config / strings behind the name is to call the corresponding application modules. 

  Between multiple applications module called Class Action for the creation of an independent modules each application configuration files, we may need to call different modules Action.    So we must use the Struts framework SwitchAction category.    Struts application modules will be automatically added to the name of a URL, as Struts applications automatically add the names added to the same URL. 

  Application Module is a new framework for the expansion of parallel contribute to the team development.    If you have a small team that no essential use of this property, not a modular.    Of course, even if there is only one module, the system is the same operation. 

  4, the JSP put WEB-INF after JSP source code to protect in order to better protect your JSP avoid unauthorized access and peep, a good approach is to Web pages of documents stored in the WEB-INF directory. 

  Developers will usually JSP pages of their Web application documents stored in the corresponding subdirectory under.    With the catalog (Catalog) related JSP be stored in the catalog under the subdirectory.    JSP associated with the customer, with orders in accordance with the relevant JSP, and so this method storage. 

  This method is the issue of the paper these pages easy to see the source code was stolen, or directly call.    Some situations, this may not be a big problem, but in certain instances it may constitute a potential safety problems.    Users can bypass the controller directly call Struts JSP is also a problem. 

  To reduce risk, these pages document can be moved to WEB-INF directory.    Servlet Based on the statement, WEB-INF not as a public document Web Application part of the tree.    Therefore, the WEB-INF directory of resources is not for direct services to clients.    We still can use the WEB-INF directory of the JSP pages to provide views to the customer, the customer has no direct requests to visit JSP. 

  If the JSP pages of these documents [WEB-INF directory, in the pages of time to call in the "WEB-INF" add to the URL.    For example, in a Struts configuration file as a write a logoff action Action mapping.    JSP path which must be "WEB-INF" beginning. 

  This method under any circumstances Struts practice may be a good way.    Is the only attention should be paid to the skills that you need to JSP and Struts action of a link.    Even if the Action is a very basic JSP is very simple, we must always invoke a Action, and call it by the JSP. 

  One final note is that not all containers can support this feature.    Early versions of WebLogic can not explain Servlet statement, it is unable to provide support, it was reported in the new version has been improved.    In general use before check your Servlet containers. 





  5, the use of Prebuilt Action development efficiency upgrade Struts framework with several prebuilt Action category, the use of their development time can be greatly saved.    One of the most useful is org.apache.struts.actions.ForwardAction and org.apache.struts.actions.DispatchAction. 

  Use ForwardAction 

  In the application, as long as may be frequent targets of Action will be transmitted to a JSP situation.    In the last point had always referred to by the calling JSP Action is a good habit.    If we do not have any in Action in the implementation of business logic, but would like to follow the visit from Action page, you can use ForwardAction, it allows you to create many empty dismissed from the Action category.    ForwardAction use of the benefits is not necessary to create your own Action category, you only need to do is configure the Struts configuration file an Action mapping. 

  For example, assume that you have a JSP document index.jsp, but can not directly call the page, through a process must be allowed to call Action category, when / home is called, will call ForwardAction and to request forwarded to index.jsp pages. 

  Not to discuss the adoption of a Class Action transmitted directly to a page of the situation, we still need to pay attention to the use of the forward elements of the properties to achieve the objectives of transponders.    These two methods can save you time and help to reduce the documentation requirements for a number of applications. 

  Use DispatchAction 

  Struts includes DispatchAction is another considerable savings in development time can be the Action category.    Class Action and the other only a single execute () method to achieve a single business, DispatchAction allows you to type in a single Action in the preparation of a number of business-related approach.    This could reduce the number of Class Action, and the related business methods brought together to make maintenance easier. 

  DispatchAction to use the function of the need to create their own category, through inheritance DispatchAction be abstract.    To be provided for each business must be a specific method signature.    Then we will be able to set these methods in a single Action category, do not have to provide a method for each category Action. 

  ShoppingCartDispatchAction calling in a certain way, only in the URL provided in the method name as a parameter value.    That is to say, call addItem () method may seem similar to the URL: 


  Http://myhost/storefront/action/ 
  Cart? Method = addItem 




  ShoppingCartDispatchAction which method parameter in the call to the method.    Parameter names can be arbitrary configuration, where the use of the "method" is just one example.    Parameters can be in the name of self-Struts configuration file settings. 

  6, the use of dynamic ActionForm 

  In Struts framework, the target for packaging ActionForm HTML forms (including request), and Back dynamic display to the user data.    They must be fully JavaBean, and inheritance. Struts inside of the ActionForm At the same time, users have the choice of two default method coverage. 

  This feature can save a lot of time because it can assist in the performance of the automatic verification.    ActionForm the only shortcoming is the need for different forms of HTML generated more than ActionForm class in order to preserve data. 

  If, for example, a page containing a user's registration information, the next page will contain information users Referral, then need to have two different ActionForm class.    This major application system will lead to excessive ActionForm class.    Struts 1.1 to make this a very good improvement, the introduction of the concept of dynamic ActionForm class in the Struts framework through DynaActionForm categories and sub-categories can be achieved dynamic ActionForm, dynamic ActionForm allows you Struts configuration file ActionForm completed the full configuration; again it is not necessary in the application process to create specific ActionForm class. 

  Specific configuration is: Struts configuration file by adding one element, type attribute set to DynaActionForm or a sub-class of its full name. 

  ActionForm dynamic can be used for Action category and JSP, with the use of ordinary ActionForm the same, only a small difference.    If the use of ordinary objects need to provide ActionForm get and set methods in data and settings.    In the above example, we need to provide getUsername () and setUsername () method and set up a username variable, and there are the same methods used to obtain a password and set up variables. 

  Here, we use the DynaActionForm, it Map variables stored in a Class object, and so we must use DynaActionForm get in category (name) and the set (name) method, parameter name is to visit examples of the variable name.    For instance, to visit DynaActionForm username in the value of similar code can be used: 

  String username = (String) form.get ( "username"); 

  The value stored in a Map objects, I have to remember to get () method returns the Object object to the mandatory conversion.    DynaActionForm There are several very useful subclass.    One of the most important is DynaValidatorForm, this dynamic and Validator ActionForm with the use of public Validator package to provide automatic verification.    This feature allows you to be designated in the code of certification rules.    Will be a combination of two of the developers, will be very attractive. 



  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply