Struts Framework Anna Paščenko. What is Struts? An open source framework for building Java web...

Post on 05-Jan-2016

216 views 0 download

transcript

Struts Framework

Anna Paščenko

What is Struts? An open source framework for building Java

web applications.

A Good Framework is… A defined support structure in which another

software project can be organized and developed.

A Good Framework is… A defined support structure in which another

software project can be organized and developed.

Allows to spend more time on meeting software requirements, not on more tedious low level details of providing a working system.

A Good Framework is… A defined support structure in which another

software project can be organized and developed.

Allows to spend more time on meeting software requirements, not on more tedious low level details of providing a working system.

Provides proven and reliable design patterns and code architecture to an application.

Java Web Frameworks > 50 open source frameworks only

Struts Tapestry Spring Cocoon Maverick Echo

Struts One of the most popular and widely used Java

Web frameworks: stable and mature good documentation large user community open source developed by industry experts flexible and extendable

Struts Application architectures based on the Model 2

approach, a variation of the classic Model-View-Controller (MVC) design paradigm.

MVC architecture The MVC architecture divides applications into

three layers: model, view, controller

MVC architecture The MVC architecture divides applications into

three layers: model, view, controller

Each layer handles specific tasks and has specific responsibilities to the other areas.

MVC architecture

MVC architecture A model

represents business data and business logic or operations that access and modify this

business data

notifies views when it changes provides the ability for the view to query the model

about its state provides the ability for the controller to access

application functionality

MVC architecture

MVC architecture A view

renders the contents of a model accesses data from the model and specifies how

that data should be presented updates data presentation when the model changes forwards user input to a controller

MVC architecture

MVC architecture A controller

defines application behavior an application typically has one controller for each set of

related functionality

dispatches user requests selects views for presentation interprets user inputs and maps them into actions to

be performed by the model

MVC Model 2

Struts MVC

Struts MVC Struts provides its own Controller component

and integrates with other technologies to provide the Model and the View.

Struts MVC For the Model, Struts can interact with

standard data access technologies (JDBC, EJB), most any third-party packages (Hibernate, iBATIS,

Object Relational Bridge etc.).

Struts MVC For the View, Struts works well with

JavaServer Pages, including JSTL and JSF, Velocity Templates, XSLT, etc.

Struts Architecture

Struts Controller Layer ActionServlet struts-config.xml Action ActionForward ActionForm

ActionServlet Handles requests to a Struts application.

configured as Servlet in the web.xml file

ActionServlet Handles requests to a Struts application.

configured as Servlet in the web.xml file specifies the url pattern to be handled by the servlet

ActionServlet Handles requests to a Struts application.

configured as Servlet in the web.xml file specifies the url pattern to be handled by the servlet uses the configuration defined in struts-config.xml

file to decide the destination of the request

struts-config.xml <form-beans>

describes a particular form bean, which is a JavaBean that implements the org.apache.struts.action.ActionForm class

struts-config.xml<form-beans><!-- sample form bean descriptor for a DynaActionForm

<form-beanname="logonForm“

type="org.apache.struts.action.DynaActionForm"><form-property

name="username"type="java.lang.String"/>

<form-propertyname="password"type="java.lang.String"/>

</form-bean>end sample --></form-beans>

ActionForm Encapsulates the form properties. Passed to an Action class methods as a

parameter.

struts-config.xml <form-beans>

describes a particular form bean, which is a JavaBean that implements the org.apache.struts.action.ActionForm class

<global-forwards> configures the global mappings of logical names to

mappable resources

struts-config.xml<global-forwards>

<forward

name="welcome"

path="/Welcome.do"/>

</global-forwards>

ActionForward Represents a destination to which the controller

might be directed after finishing the processing activities.

Is a return value of the Action class methods.

struts-config.xml <form-beans>

describes a particular form bean, which is a JavaBean that implements the org.apache.struts.action.ActionForm class

<global-forwards> configures the global mappings of logical names to

mappable resources <action-mappings>

contains action definitions

struts-config.xml<action-mappings>

<action

path="/Login"

type="myPackage.LoginAction"

<forward name="forwardNext"

path="/pages/NextAction.jsp"/>

</action-mappings>

ActionMapping Stores the mapping information. Passed to an Action class methods as a

parameter.

Action The Struts framework provides an abstract

Action class that you must extend for your own needs.

The ActionServlet calls the Action's execute() method to perform whatever task the Action class was meant to perform.

Within the overriden execute() method, you can put in whatever logic you need in order to fulfill the request.

Actionpublic class LoginAction extends Action {

/** * Called by the ActionServlet when the a user attempts to

login. */ public ActionForward execute(ActionMapping mapping,

ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

// Check username and password.return mapping.findForward("forwardNext");

}}

Struts Examples Several example applications are bundled with

the Struts distribution: Blank - A simple template for starting new Struts

applications. MailReader - The original Struts example

application. Examples - Various demonstration applications

combined as separate modules.