Struts

  • Uploaded by: mohanraop
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Struts as PDF for free.

More details

  • Words: 1,886
  • Pages: 33
BB: Struts ATS Application Programming: Java Programming

© Accenture 2005 All Rights Reserved

Course Code #Z16325

Objectives

This review session will cover Struts. Included in this review: What is Struts  Struts Framework Features  Struts Architecture  Struts Model Component  Struts View Component  Struts Control Component  Struts Tag Libraries (HTML, Bean, Logic)  ActionForm  ActionServlet  Action ©2005 Accenture All Rights Reserved.  © AccentureActionMapping 2005 All Rights Reserved 

2

Detailed in this review:





Overview

The Framework of the Struts How It is Implemented

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

3

What is Struts? 

An open source development framework for building web applications.



Based on Model-View-controller (MVC) design paradigm



Implementation of JSP Model 2 Architecture



2nd Release candidate of version 1.1 released



Consists of 8 Top-Level Packages



Approx 250 Classes and Interfaces

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

4

Struts Framework Features



Model 2 - MVC Implementation



Internationalization (I18N) Support



Rich JSP Tag Libraries



Based on JSP, Servlet, XML, and Java



Supports Java’s Write Once, Run Anywhere Philosophy



Supports different model implementations (JavaBeans, EJB, etc.)



Supports different presentation implementations (JSP, XML/XSLT, etc.)

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

5

Struts Architecture

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

6

Struts Architecture

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

7

Struts Architecture 

  



Central controller mediates application flow and delegates to appropriate handler. Handlers are tied to model components. Model encapsulates business logic. Control forwarded back through the Controller to the appropriate View. The forwarding can be determined by consulting a set of mappings in configuration file, which provides a loose coupling between 8 ©2005 Accenture All Rights Reserved. the View and Model.

© Accenture 2005 All Rights Reserved

Struts “Model” Components   



No built-in support for the model. No model components provided. Framework supports any component model. (JavaBeans, EJB, Corba, etc.) Should always decouple the application from a specific model implementation.

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

9

Struts “View” Components        

JavaServer Pages HTML JavaScript and Stylesheets Multimedia Files Resource Bundles JavaBeans (Part of model used by views) JSP Custom Tags ActionForms ©2005 Accenture All Rights Reserved.

© Accenture 2005 All Rights Reserved

10

Struts “Control” Components 

ActionServlet - Extends javax.servlet.http.HttpServlet - Receives all framework requests - One ActionServlet instance per web application



RequestProcessor - One instance per application module - Processes all requests for module - Invokes properAction instance - Default implementation provided by framework (can extend if necessary)



-

ActionClasses Extends org.apache.struts.action.Action Overrides the execute() method Acts as a bridge between user-invoked URI and a business method (Command pattern) Returns information about which view should be rendered next Part of the Controller, not the Model ©2005 Accenture All Rights Reserved.

© Accenture 2005 All Rights Reserved

11

Struts Tag Libraries 

THE HTML TAG LIBRARY - Tags used to create Struts input forms - Examples include (checkbox, image, link, submit, text, textarea)

< b> Shipping Address< td> Address:< td> City:< td> State:< td> Postal:
< html: text name=" checkoutForm" property=" shippingAddress"/>
< html: text name=" checkoutForm" property=" shippingCity"/>
< html: text name=" checkoutForm" property=" shippingState"/>
< html: text name=" checkoutForm" property=" shippingPostal"/>

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

12

HTML Tags (cont.) 

 





Acts as a bridge between JSP view and other components. Input forms are important for gathering data. Most of the actions of the HTML taglib involve HTML forms. Error messages, hyperlinking, internationalization. HTML tags must be nested within a form tag.

Inform handler which bean to use for initializing ©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved displayed values. –

13

Struts Tag Libraries 

-

THE BEAN TAG LIBRARY Tags used for accessing JavaBeans and their properties Examples include (define, message, write)


Current Total: $

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

14

Bean Tags (cont.)   

Tags for accessing beans and their properties Enhancements to <jsp:useBean> Convenient mechanisms to create new beans based on the value of: – – –

Cookies Request Headers Parameters

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

15



Struts Tag Libraries

THE LOGIC TAG LIBRARY – – – –

Managing conditional generation of output text Looping over object collections for repetitive generation of output text Application flow management. Examples include (empty, lessThan, greaterThan, redirect, iterate)

Logic Tag Library Example

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

16

Logic Tags (cont.) 

Provides presentation logic tags for: 

Value comparisons –



Substring matching –



match, notmatch

Presentation logic –



Include: = != <= >= < >

forward, redirect

Collections –

iterate ©2005 Accenture All Rights Reserved.

© Accenture 2005 All Rights Reserved

17

ActionForm 

  



Java class that extends the org.apache.struts.action.ActionForm. Captures user data from the Http request. Stores the data temporarily. Acts as a “firewall” between the presentation tier and the application. Provides the ability to validate the user input. ©2005 Accenture All Rights Reserved.

© Accenture 2005 All Rights Reserved

18

ActionForm (cont.) 

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 – –



name attribute of class

Contains only property getter and property setter methods for each field - no business logic ©2005 Accenture All Rights Reserved.

© Accenture 2005 All Rights Reserved

19

ActionForm (cont.)

Example package com.jds.apps.hr.employee.form; import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; public class EmployeeInfoForm extends AbstractEmployeeForm { public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); // Last Name: if it's empty, has a special character, or exceeds it's maximum length if( objectIsNull.validate(this.getLastName() ) || stringIsEmpty.validate( this.getLastName() ) ){ errors.add("lastName", new ActionError("field.null","Last Name")); } else if( !stringIsValidD.validate( this.getLastName() ) ){ errors.add("lastName", new ActionError("field.invalid.specialcharacter", "Last Name", allowedCharactersD)); ©2005 Accenture All Rights Reserved. } © Accenture 2005 All Rights Reserved else if( !stringLengthIsValidFifty.validate( this.getLastName() ) ){

20

ActionServlet 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 ©2005 Accenture All Rights Reserved.



Delegates most of this grunt work to Action

© Accenture 2005 All Rights Reserved

21

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 Tile definition Velocity template Another Action ©2005 Accenture All Rights Reserved.

© Accenture 2005 All Rights Reserved

22

Action Class (cont.) What is Action Class?  Java class that does the “work” of your application – –



Handle request Perform business logic

Can be simple or sophisticated –



Simple action class does handle business logic by itself Sophisticated ones invoke Model components 

©2005 Accenture All Rights Reserved.

23

Action class functions as a Facade pattern in this case

© Accenture 2005 All Rights Reserved

Action Class (cont.)

Example package com.jds.apps.hr.employee.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class EmployeeAddAction extends AbstractAction { public ActionForward execute(ActionMapping actionMapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { EmployeeInfoForm viewForm = (EmployeeInfoForm) form; // Determine action, if edit or view mode ©2005 Accenture All Rights Reserved. UserBean userBean = this.getUserBean(request); String userBean.getUserType(); © Accenture 2005userType All Rights = Reserved

24

ActionMapping 





An ActionMapping object describes an Action instance to the ActionServlet. It represents the information that uniquely defines an instance of a particular action class. Provides useful information to both the Action.execute() and the ActionForm.validate() methods, enabling a developer to alter its behavior based upon the values describing this particular Action. ©2005 Accenture All Rights Reserved.

© Accenture 2005 All Rights Reserved

25

Configuring Applications Struts Configuration File 



Use configuration elements in developing Struts Application. These "static" configuration elements are:

– <message-resources> ©2005 Accenture – © Accenture 2005 All Rights Reserved –

All Rights Reserved.

26

Configuring Applications

HRS struts-config.xml file

<struts-config> <set-property property="autoCommit" value="false"/> <set-property property="description" value="Example Data Source Configuration"/> <set-property property="driverClass" value="org.postgresql.Driver"/> <set-property property="maxCount" value="4"/> <set-property property="minCount" value="2"/> ©2005 Accenture All Rights Reserved. 27 <set-property property="password" value="mypassword"/> © Accenture 2005 All Rights Reserved

Configuring Applications

HRS struts-config.xml file (cont.) …. ©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved
28

Configuring Applications

HRS struts-config.xml file (cont.) ©2005 Accenture All Rights Reserved. © Accenture 2005 Allname="approver" Rights Reserved

29

Configuring Applications

HRS struts-config.xml file (cont.)

Configuring Applications

HRS struts-config.xml file (cont.)

<set-property property="inputForward" value="true"/> <message-resources parameter="com.jds.architecture.util.messages"/> ©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

31

Resources     

http://java.sun.com/webservices/docs/1.0/tutorial/d http://struts.apache.org/userGuide/building_model. http://struts.apache.org/userGuide/building_view.h http://struts.apache.org/userGuide/building_control http://struts.apache.org/userGuide/building_contro http://jakarta.apache.org/struts/

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

32

Questions & Comments .

©2005 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved

33

Related Documents

Struts
June 2020 26
Struts
November 2019 49
Struts
May 2020 36
Struts
November 2019 50
Struts
November 2019 46
Struts
June 2020 5

More Documents from ""

Threads
May 2020 26
Jsp
May 2020 21
Servlet
May 2020 21
Ajax
May 2020 36
Wml
May 2020 18