Apache Struts

Struts is an open- source framework for the presentation and control layer of Java web applications. Struts accelerates the development of Web applications considerably by processing HTTP requests in a standardized process. It uses standard technologies like Java Servlets, JavaBeans, resource bundles, and XML, as well as various Apache Commons packages.

For the developer, this means that many application-relevant functions are already implemented and ready to use. Struts is already used in many Web applications, and commonly regarded as a sound framework. Struts was developed by Craig McClanahan in 2000. Since then, an ever-growing community of developers working on the improvement of the framework. As of version 2, the Struts framework is merged with the WebWork framework. The approach of Struts was also used for the development of Struts4PHP.

As one of the most famous Jakarta Struts projects in 2004 has become one of " top level Apache Project".

  • 4.1 The presentation 4.1.1 Tiles
  • 4.1.2 Validator

Construction

The Struts framework is based on the design pattern " Model View Controller ". The components provided by Struts available come from the areas of presentation (view) and program control (controller). Functions from the model range must be implemented elsewhere in an application. The framework currently contains about 300 Java classes, which are grouped into eight core packages.

Theoretical Approach

Model 1 architecture with Java Servlets / JSP

After the technology of Java servlets was introduced, it quickly realized that creating HTML pages with servlets time consuming and the further development of views can be tedious. In addition to that, all three components of the Model- View-Controller approach were implemented in a servlet. Then JSP was introduced, the web designer made ​​especially the work easier and asserted itself in a short time. Hence the Model 1 architecture, which describes that data storage, business logic and View are to settle in a JSP, which, however, in cases of complex application logic leads to new problems developed: The JSP becomes unmanageable very quickly and can be serviced only by developers, have both HTML and Java knowledge. While moderates the use of tag libraries (such as JSTL ) this problem somewhat because no Java code more is needed. The basic conceptual problem of mixing presentation with professional functionality remains.

Model 2 architecture with Struts

The problems listed have helped that you had to develop a new approach that separates the individual components clearly distinguishable and Java Servlets and JSP integrates alike. The result was the Model 2 architecture for web applications, which has found its use in the Struts framework. This uses a front controller, which provides that each call is only processed centrally and then forwarded to the appropriate controller.

Struts2 features

  • Simple based on POJOs Action classes
  • Simple testability
  • Threadsave
  • Ajax support
  • jQuery plugin
  • Dojo plugin (deprecated)
  • AJAX Client Side Validation
  • Support for templates
  • Different types of results
  • Easy to expand with plugins
  • REST plugin ( REST -based actions, extension -less URLs)
  • Convention Plugin ( Action Configuration via Conventions and Annotations)
  • Spring plugin ( Dependency Injection )
  • Hibernate Plugin
  • JFreeChart plugin ( Charts )
  • Rome plugin ( RSS feeds)

Practical Implementation

The goal is the separation of presentation, data management and application logic. This increases the visibility and maintainability. The core elements in Struts, which are to be implemented:

The components are in the main configuration file of Struts linked.

Example struts- config.xml in Struts1:

      <-! FormBean definitions ->                             < - Action definitions ->                 < action path = "register"                   name = "Register Form"                   type = " bar.foo.web.RegisterAction "                   input = " / web / register.jsp "                   scope = "request"                   validate = "true" >                                       < / action>         Example in Struts2 struts.xml:

          name="index">              / jsp / index.jsp < / result>          < / action>      The presentation

A paradigm is to use as little Java code ( scriptlets ) as possible in the JSP. To make this possible, one should use the supplied Struts tag libs who will help when reading out the form bean.

Example:

Here the attribute "email address" of the action ( Struts2 ) or form bean ( Struts1 ) is filled via the setter method after sending the form with the contents of the text field. Furthermore value="..."/> HTML element in the JSP generates an

Tiles

Struts also has a template extension, called Tiles. They allow the developer to its website component-based build ( header, footer, content, and so on ). Tiles can include both JSPs and other tiles.

Validator

Since version 1.1 of Struts has a validation tool which helps the developers in reviewing the notices dispatched form data. It works with so-called validators that perform a specific scan task and can be reused. Struts has some pre-made with validators (eg for testing whether a field is filled in or contains a valid number ). Provides a validator detects an error, it automatically redirected to the main page and the error displayed. It is also possible to check the error via JavaScript on the client side and display the error before sending the form in a window.

The form bean ( Struts1 )

The form bean is a normal JavaBean that contains all necessary data for the JSP and the action. It is the interface between these two components and is linked via the struts- config with a form in the JSP. When the form is submitted, the bean itself (before the action is executed ) fed via the setter methods with the corresponding input values ​​by the ActionServlet.

The FormBean also includes a validation method which, when activated in the configuration, the data of the form bean checked before they are sent to the action. It also includes a reset method to reset the content so that it can be reused, which reduces the complexity of storage management is reduced (see: Slab allocator ).

Since this type of data storage is often too rigid, the opportunity was created by the Struts developers who declare beans in the struts- config.xml, which the rigid and complex programming of the beans is not required. Then the ActionForm is casted to a DynaBean the Apache Commons project Beanutils to access and the data are then retrieved via key. When using dynamic beans also can be used to validate the Commons validation framework can be used.

The Action

The action is the component that communicates with the backend, overtaken by their data, and they also re- writes then. They usually contain more testing and evaluation mechanisms.

The action is also responsible for the navigation of the website, as it must return an ActionForward after work. Depending on the Forward returns the action, the Struts framework decides based on the struts- config, should be forwarded to which side.

72181
de