Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION TO STRUTS

Similar presentations


Presentation on theme: "INTRODUCTION TO STRUTS"— Presentation transcript:

1 INTRODUCTION TO STRUTS
Presentation by NEELIMA MULAMPAKA 2008 Spring Class – CSI668

2 STRUTS - Agenda History Application Framework
Model-View-Controller Design Pattern Struts Architecture How Struts Works Major Struts Components The Big Picture Strengths And Weaknesses A Simple Application References Assignment

3 History Struts is hosted by the Apache Software Foundation (ASF) as part of its Jakarta Project. The primary architect and developer of the Struts framework is Craig R.McClanahan. MVC-based (Model-View-Controller) open source software Used for constructing web applications using Servlets and JSPs Why is it called Struts ?

4 Framework Is a reusable, semi-complete application that can be specialized to produce custom applications . Application frameworks build on a common ground to provide developers with a reusable structure that can serve as the foundation for their own products. Provides developers with a set of backbone components .

5 Common Framework Strategies
Java web application frameworks use several common techniques to help make products easier to design, write, and maintain. Some common components: External configuration files. A central controller External presentation systems

6 Framework Components

7 Model-View-Controller

8 MVC-based Architecture
3 Major Components in Struts Servlet controller (Controller) Java Server Pages or any other presentation technology (View) Application Business Logic in the form of whatever suits the application (Model) The centerpiece of Struts is MVC style controller. Struts is focused on Controller Struts is Model and View independent

9 Controller Is the switch board of MVC architecture
Every request goes through the controller Responsible for flow control (action mapping) of the request handling Reads configuration file to determine the flow control

10 Controller Components in Struts Framework
Major Struts classes as they relate to MVC ActionServlet - The part of the Controller that receives user gestures and state changes and issues view selections ActionMapping - The state change event ActionForm - The data for a state change ActionClass - The part of the Controller that interacts with the model to execute a state change or query and advises the ActionServlet of the next view to select ActionForward - A user gesture or view selection

11 How Struts Works

12 Struts Flow (Struts 1.0)

13 Struts View Helpers Tag Library Descriptor Purpose
struts-html.tld JSP tag extension for HTML forms struts-bean.tld JSP tag extension for handling JavaBeans struts-logic.tld JSP tag extension for testing the values of properties

14 The Big Picture Struts request-response process
A client requests a path that matches the Action URI pattern . The container passes the request to the ActionServlet. If this is a modular application, the ActionServlet selects the appropriate module. The ActionServlet looks up the mapping for the path. If the mapping specifies a form bean, the ActionServlet sees if there is one already or creates one . If a form bean is in play, the ActionServlet resets and populates it from the HTTP request. If the mapping has the validate property set to true, it calls validate on the form bean . If it fails, the servlet forwards to the path specified by the input property and this control flow ends.

15 The Big Picture(contd)
If the mapping specifies an Action type, it is reused if it already exists or instantiated . The Action’s perform or execute method is called and passed the instantiated form bean (or null). The Action may populate the form bean, call business objects, and do whatever else is needed . The Action returns an ActionForward to the ActionServlet. If the ActionForward is to another Action URI, we begin again; otherwise, It’s off to a display page or some other resource. Most often, it is a JSP.

16 The Big Picture

17 What Does ActionServlet Do?
Performs the role of Controller Process user requests Determine what the user is trying to achieve according to the request Pull data from the model (if necessary) to be given to the appropriate view, and Select the proper view to respond to the user Delegates most of this grunt work to Action classes

18 ActionServlet(contd)
Invoking ActionServlet One of the standard servlet settings is the servlet mapping. The container uses this setting to decide which requests are sent to which servlet: <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>

19 ActionServlet(contd)
ActionServlet is configured in the web.xml <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> </servlet>

20 ActionMapping (You provide it)
Action Mapping is configured in Struts Config File Each action element has requires the following attributes to be defined: path: The application context-relative path to the action type: The fully qualified java classname of your Action class name: The name of your <form-bean> element to use with this action

21 ActionMapping(contd)
<action-mappings> <action path="/submit" type="hansen.playground.SubmitAction" name="submitForm" input="/submit.jsp" scope="request" validate="true"> <forward name="success" path="/submit.jsp"/> <forward name="failure" path="/submit.jsp"/> </action> </action-mappings>

22 ActionForm (You provide it)
Provided by developer Define an ActionForm bean (that is, a Java class extending the ActionForm class) for the input form Extend Struts-provided ActionForm class Define it in servlet-config.xml file <form-bean> name attribute of <Action> class Contains only property getter and property setter methods for each field-no business logic Provides standard validation mechnism

23 ActionForm(contd) <form-beans> <form-bean name="submitForm"
type="hansen.playground.SubmitForm"/> </form-beans>

24 How to write ActionForm Bean
Add just getter and setter methods for each property of a input form Do not include any business logic code Add a standard validation method Controller will call this validation Define a property (with associated getXxx and setXxx methods) for each field that is present in the form

25 Action Class Focus on control flow
Process client request by calling other objects (BusinessLogic beans) inside its execute() method Returns an ActionForward object that identifies where control should be forwarded JSP Another Action

26 execute(..) method of Actionclass (Struts 1.1 only)
Invoked by controller Function Signature public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception;

27 ActionForward - View ActionForward object tells Servlet controller which JSP page is to be dispatched to JSP pages use ActionForm beans to get output Model data to display Struts contains a series of tag libraries Facilitates communication between HTML designers and developers Facilitates dynamic Web content

28 Struts Configuration (strut-config.xml)
It is like a Blueprint of your Application. Lot of information collected in one place. Every component in the struts configuration is a java object

29 Struts Configuration(contd)
A very simple application could create all of these informational objects in an initialization method and then set each object to the default values needed. For example: ActionForward logoff = new ActionForward(); logoff.setName("logoff"); logoff.setPath("/Logoff.do"); ActionForward logon = new ActionForward(); logoff.setName("logon"); logoff.setPath("/Logon.do");

30 Struts-config.xml <?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN“ " <struts-config> <!-- == Form Bean Definitions == --> <form-beans> <form-bean name="submitForm" type="hansen.playground.SubmitForm"/> </form-beans>

31 Struts-config.xml(Contd)
<!-- == Action Mapping Definitions == --> <action-mappings> <action path="/submit" type="hansen.playground.SubmitAction" name="submitForm" input="/submit.jsp" scope="request“ validate="true"> <forward name="success“path="/submit.jsp"/> <forward name="failure" path="/submit.jsp"/> </action> </action-mappings> </struts-config>

32 Validation Indicate you want input validation as attributes of <action> element under <action-mapping> in strut-config.xml file – validate=”true” Specify the JSP page that needs to be displayed when validation fails input=”/errorpage.jsp” Override validate() method within ActionForm class – optional

33 struts-config.xml:Validation
Example <action-mappings> <action path="/submit" type="hansen.playground.SubmitAction" name="submitForm" input="/submit.jsp" scope="request" validate="true"> <forward name="success" path="/submit.jsp"/> <forward name="failure" path="/submit.jsp"/> </action> </action-mappings> </struts-config>

34 Strengths HTTP-centric Standard logging Lightweight Open source
Strongly founded in design patterns

35 Weaknesses Logging Loads a single configuration file per application
Single ActionServlet Requires understanding of Struts components

36 A Simple Application Simple user registration application.
Registration screen Three Fields -username,password, and confirmation password. A successful registration requires that the two passwords match. If the registration is successful, control flows to a page that says successful!. If the two passwords do not match, control flows to a page that says failure.

37 Create The ActionForm package app; import org.apache.struts.action.*;
public class RegisterForm extends ActionForm { protected String username; protected String password1; protected String password2; public String getUsername () {return username;} public String getPassword1() {return password1;} public String getPassword2() {return password2;} public void setUsername (String username) {this.username = username;} public void setPassword1(String password) {this.password1 = password;} public void setPassword2(String password) {this.password2 = password;} }

38 Creating the RegisterAction
package app; import org.apache.struts.action.*; import javax.servlet.http.*; import java.io.*; public class RegisterAction extends Action { public ActionForward perform (ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) { // b Cast the form to the RegisterForm RegisterForm rf = (RegisterForm) form; String username = rf.getUsername(); String password1 = rf.getPassword1(); String password2 = rf.getPassword2();

39 Creating the RegisterAction(contd)
// c Apply business logic if (password1.equals(password2)) { try { // d Return ActionForward for success UserDirectory.getInstance().setUser(username,password1); return mapping.findForward("success"); } catch (UserDirectoryException e) { return mapping.findForward("failure"); } // E Return ActionForward for failure

40 Creating the Struts configuration file (struts-config.xml)
<?xml version="1.0" encoding="ISO " ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" " <struts-config> <form-beans> <form-bean name="registerForm" type="app.RegisterForm"/> </form-beans> <action-mappings

41 Creating the Struts configuration file (struts-config.xml)(contd)
<action path="/register" type="app.RegisterAction" name="registerForm" input="/register.jsp"> <forward name="success" path="/success.html"/> <forward name="failure" path="/failure.html"/> </action> </action-mappings> </struts-config>

42 References Struts In Action By Ted Husted Jakarta Struts By O’Reilly

43 Assignment The application you are going to create mimics entering an employee into a database. The user will be required to enter an employee's name and age.

44 NEELIMA MULAMPAKA nm121262@albany.edu
QUESTIONS ?? NEELIMA MULAMPAKA


Download ppt "INTRODUCTION TO STRUTS"

Similar presentations


Ads by Google