Struts

from Wikipedia, the free encyclopedia
Struts

Struts logo.gif
Basic data

developer Apache Software Foundation
Publishing year October 10, 2006 to April 11, 2014
Current  version 2.5.22 GA travelcard
(November 29, 2019)
operating system platform independent
programming language Java
category Web framework
License Apache license
struts.apache.org

Struts is an open source - Framework for the presentation and control layer of Java - Web applications . Struts significantly speeds up web application development by processing HTTP requests in a standardized process. It uses standardized techniques such as JavaServlets , 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 for use. Struts is already used in a great many web applications and is widely regarded as a solid framework . Struts was developed by Craig McClanahan in 2000. Since then, a growing community of developers has been working to improve the framework. From version 2, the Struts framework is merged with the WebWork framework. The Struts approach was also used for the development of Struts4PHP.

As one of the most famous Jakarta projects , Struts advanced to an “Apache Toplevel Project” in 2004 .

construction

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

Theoretical approach

Model 1 architecture with JavaServlets / JSP

After the technology from Java - Servlets was introduced, we quickly realized that the creation of HTML may be pages with servlets time consuming and the further development of views very tedious. In addition, all three components of the Model-View-Controller concept were implemented in one servlet. Thereupon JSP was introduced, which made the work of the web designer especially easier and became established in a short time. This resulted in the Model 1 architecture , which describes that data management, business logic and view are to be located in a JSP, which, however, leads to new problems with complex application logic: The JSP becomes unmanageable in a very short time and can only be maintained by developers. who have both HTML and Java skills. The use of tag libraries (such as JSTL ) alleviates this problem somewhat, because Java code is no longer required. The basic conceptual problem of mixing presentation with technical functionality remains, however.

Model 2 architecture with Struts

The problems listed have contributed to the need to develop a new concept that separates the individual components more clearly and integrates Java servlets and JSP equally. The Model 2 architecture for web applications was created , which is used in the Struts framework. This uses a front controller , after which each call is first processed centrally and then forwarded to the corresponding controller.

Struts2 features

  • 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 plug-in ( dependency injection )
  • Hibernate plugin
  • JFreechart plugin (charts)
  • Rome plugin (RSS feeds)

Practical implementation

The aim is to separate presentation, data management and application logic . This increases the overview and the maintainability. The core elements in Struts to implement are:

Action
for program control, interface between the view and the application logic
JSP
for presentation

The components are linked together in the Struts central configuration file .

Example struts-config.xmlin Struts1:

 <struts-config>
     <!-- FormBean Definitionen -->
     <form-beans type="org.apache.struts.action.ActionFormBean">
         <form-bean name="RegisterForm" type="bar.foo.web.RegisterForm" />
     </form-beans>
     <!-- Action Definitionen -->
     <action-mappings type="org.apache.struts.action.ActionMapping">
         <action path="register"
                 name="RegisterForm"
                 type="bar.foo.web.RegisterAction"
                 input="/web/register.jsp"
                 scope="request"
                 validate="true">
             <forward name="success" path="/home.do" />
             <forward name="failure" path="/error.do" />
         </action>
     </action-mappings>
 </struts-config>

Example struts.xmlin Struts2:

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <package name="myApplication" extends="struts-default">
        <action name="index">
            <result>/jsp/index.jsp</result>
        </action>
    </package>
</struts>

The presentation

One paradigm is to use as little Java code (scriptlets) as possible in the JSP. To make this possible, you should use the tag libs provided by Struts , which help you to read the form bean.

Example:

<html:text name="RegisterForm" property="emailAddress" size="20"/>

Here the attribute “ emailAddress” of the action (Struts2) or form bean (Struts1) is filled with the content of the text field via the setter method after the form has been sent. An <input type="text" size="20" name="emailAddress" value="..."/>HTML element is also generated in the JSP.

Tiles

Struts also has a template extension called Tiles . They enable the developer to build his website on a component basis (header, footer, content, and so on). Tiles can include JSPs as well as other tiles.

Validator

As of version 1.1, Struts has a validation tool that helps the developer to check the shape data sent. It works with so-called validators , which carry out a specific test task and can be reused as required. Struts comes with some pre-built validators (e.g. for checking whether a field is filled out or contains a valid number). If a validator detects an error, the system automatically returns to the output page and the error is displayed. However, it is also possible to have the error checked on the client side via JavaScript and to display the error in a window before the form is sent.

The Form Bean (Struts1)

The form bean is a normal Java bean that contains all the data required for the JSP and the action. It is the interface between these two components and is linked struts-configto a form in the JSP. When the form is submitted, the bean ActionServletis fed with the appropriate input values via the setter methods (before the action is executed) .

This FormBeanalso contains a validation method which, if activated in the configuration, checks the data of the form bean before it is sent to the action. It also contains a resetmethod to reset the content so that it can be reused, which reduces the overhead for memory management (see: Slab allocator ).

Since this type of data management is often too rigid , the Struts developers created the option of declaring the beans in struts-config.xml , which eliminates the need for rigid and complex programming of the beans. For access, the is then castActionForm to one of DynaBeanthe Apache Commons BeanUtils project and the data is then queried using a key. When using dynamic beans, the Commons Validation Framework can also be used for validation .

The action

The action is the component that communicates with the backend , fetches data from it and also writes it back to it. It usually contains further checking and evaluation mechanisms.

The action is also responsible for navigating the website, as it has to return a after the work is done ActionForward. Depending on which forward the action returns, the Struts framework decides struts-configwhich page should be forwarded to.

See also

literature

  • Matthias Weßendorf: Struts - develop websites efficiently with Struts 1.2 & 1.3 and Ajax. 2nd Edition. W3L, Herdecke 2006, ISBN 3-937137-26-2
  • Michael Albrecht, Manfred Wolff: Struts packed. MITP, Bonn 2004, ISBN 3-8266-1431-3

Web links

Individual evidence

  1. projects.apache.org . (accessed on April 8, 2020).
  2. Struts 2.5 General Availability. Retrieved November 29, 2019 .
  3. Jakarta News March 18, 2004