Apache Struts Technology: A MVC Framework For Java Web Applications
Apache Struts Technology: A MVC Framework For Java Web Applications
Softsmith Infotech
Agenda
Introduction What is Apache Struts? Overview of traditional JSP/Servlet web applications The Model-View-Controller Design Pattern Struts implementation of the MVC Pattern ActionServlet struts-config.xml Action Classes ActionForms Validating user input JSPs and Struts TagLibs The Model Control flow of a typical request to a Struts application Additional features Summary
Softsmith Infotech
Model
holds application data and business logic is absolutely independent from the UIs
Softsmith Infotech
Controller
bridge between Model and View controls the flow of the application receives/interprets user input performs operations on the Model triggers View update
Benefits:
better maintainability and testability of applications ability to easily develop different kinds of UIs (e.g. console, GUI, ) separation of different tasks in development code reusability
Softsmith Infotech
Softsmith Infotech
Simple Login
Success.html
Failure.html
JSP
response
submit
JSP
ActionServlet
struts-config.xml
Softsmith Infotech
Controller ActionServlet
the central component in a Struts application
manages the flow of the application receives user requests and delegates them to the corresponding Action classes selects the appropriate View to be displayed next (according to ActionForward returned by an Action class)
represents a Single Point of Entry of the web application (Front Controller Pattern) implemented as a simple Java Servlet listed in the deployment descriptor of the surrounding Web Container (usually web.xml) for handling *.do requests can be extended, but in most cases this is not necessary
Softsmith Infotech
Example:
Controller Actions
perform logic depending on a users request Actions are Java classes that extend Struts Action class org.apache.struts.action.Action The Action's execute() method is called by the ActionServlet Tasks usually performed by Actions: depending on the type of action: perform the action directly (non-complex actions) call one or more business logic methods in the Model return an appropriate ActionForward object that tells the ActionServlet which View component it should forward to Ex.: failure or success in login application
Softsmith Infotech
Controller ActionForms
represent the data stored in HTML forms hold the state of a form in their properties provide getter/setter methods to access them may provide a method to validate form data ActionForms are Java classes that extend Struts ActionForm class org.apache.struts.action.ActionForm are filled with the form data by the ActionServlet one ActionForm can be used for more than one HTML form very useful when building wizards or similar types of forms
DynaActionForm ActionForms dynamically created out of XML definitions useful when having a large number of fields
Softsmith Infotech
Struts tag libraries provide access to Model data enable interaction with ActionForms provide simple structural logic (such as iteration) Example: ... <%@ prefix="html" uri="/WEB-INF/struts-html.tld" %>
<body> <html:errors/> <html:form action="login.do"> Username: <html:text property="username"/><br/> Password: <html:password property="passwd" redisplay="false"/><br/> <html:submit>Login</html:submit> </html:form> </body> Softsmith Infotech
The Model
Holds the data of an application and provides business logic methods Not directly part of the Struts framework! The Model is usually built of different kinds of Business Objects: JavaBeans simple Java classes, that follow certain naming conventions contain attributes and corresponding getters/setters reside in the Web Container Enterprise JavaBeans (EJBs) components containing business logic in a J2EE architecture reside in an EJB Container kinds of EJBs: Session Beans, Entity Beans, Message Driven Beans Often a database server is used to make data persistent
Softsmith Infotech
Softsmith Infotech
Summary
So, why is Struts so useful? structural separation of data presentation and business logic easy separation of development tasks (web design, database, ) increases maintainability and extendibility (new views!) increases reusability of code Struts provides a Controller that manages the control flow changes in the flow can all be done in struts-config.xml abstraction from (hard coded) filenames (forwards) easy localization (internationalization is more important than ever) based on standard Java technologies (JSP, Servlets, JavaBeans) thus running on all kinds of JSP/Servlet containers open-source affordable no dependence on external companies robustness (due to freely accessible source code) very vivid open-source project with growing developer community
Softsmith Infotech