11 copyright © 2004, oracle. all rights reserved. customizing actions

26
11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

Upload: spencer-payne

Post on 08-Jan-2018

226 views

Category:

Documents


2 download

DESCRIPTION

11-3 Copyright © 2004, Oracle. All rights reserved. Struts Configuration File Is XML type file Is the application resource descriptor Is used by the servlet to determine actions to perform

TRANSCRIPT

Page 1: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11Copyright © 2004, Oracle. All rights reserved.

Customizing Actions

Page 2: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-2 Copyright © 2004, Oracle. All rights reserved.

Objectives

After completing this lesson, you should be able todo the following:• Describe the Struts XML elements and structure• Describe the anatomy of an action• Use the execute method to enhance the behavior

of an action• Describe the use of form beans• Use a dynamic form bean

Page 3: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-3 Copyright © 2004, Oracle. All rights reserved.

Struts Configuration File

• Is XML type file• Is the application resource descriptor• Is used by the servlet to determine actions to

perform

<struts-config> <action-mappings> <action path="/action1"/> <forward name="success" path="/page1.do"/> </action> </action-mappings> <message-resources parameter="view.ApplicationResources"/></struts-config>

Page 4: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-4 Copyright © 2004, Oracle. All rights reserved.

Creating the Action Class

• In the context menu, select “Go to Code.”• Or, double-click the action.• Specify a name for the action.

/auth

Page 5: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-5 Copyright © 2004, Oracle. All rights reserved.

Default Code of an Action

public class AuthUserAction extends Action {/* This is the main action called from the Struts

framework.*/public ActionForward execute(

ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException { return mapping.findForward("success"); }}

Page 6: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-6 Copyright © 2004, Oracle. All rights reserved.

<action path="/authUser" type="view.AuthUserAction"> <forward path="/page1.do" name="success"/> <forward path="/page2.do" name="failure"/></action>

Forwards

Forwards can be defined:• By using the Page Flow

Diagram• In the XML file• In the Structure pane

/authUser

success

failure

/page1

/page2

Page 7: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-7 Copyright © 2004, Oracle. All rights reserved.

ActionForward of an Action Class

• The return parameter from ActionForward specifies where to send control.

• The default naming of a single forward is success.• The execute() method can be customized:

– Additional code can be added.– Other forwards can be specified.– Appropriate forward is done on conditional testing.

• The forward name represents a logical name. • A forward can also be a global forward.

Page 8: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-8 Copyright © 2004, Oracle. All rights reserved.

Creating Global Forwards

• A global forward is just like any forward, but it can be accessed by any action.

• The global forward is defined in the config file.

• The global forward is specified in the action class.

<global-forwards> <forward name="help" path="/help.do"/></global-forwards>

return mapping.findForward("help");

Page 9: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-9 Copyright © 2004, Oracle. All rights reserved.

Form Beans

User Name

Password

Logon

logonBean

Submit

Logon

authUser

menu

Populates

Page 10: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-10 Copyright © 2004, Oracle. All rights reserved.

Form Beans

• A form bean is used to transport data between a page and an action.

• Can be static:– Is defined in a FormBean class– Contains set(), get(), and reset() methods for

each field – Contains a validate() method for verifying user

input• Can be dynamic:

– Each field specified in struts.config.xml– Does not require Java code

Page 11: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-11 Copyright © 2004, Oracle. All rights reserved.

Creating a Static Form Bean

• Create an action in the Page Flow Editor.• Right-click and select “Go to Form Bean.”• Specify a name (suffix the name with “Form”).

• A new icon is displayed.

/authUser

/authUser

Page 12: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-12 Copyright © 2004, Oracle. All rights reserved.

Example: Static Form Bean

public class AuthUserActionForm extends ActionForm {String username;public String getUsername() { return username; }public void setUsername(String newUsername) { username=newUsername; }

public void reset( …

public ActionErrors validate( … }

Page 13: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-13 Copyright © 2004, Oracle. All rights reserved.

Dynamic Form Beans

• This is an alternative way for an action to have access to incoming fields from a page.

• The form bean class is not needed.• There is no need for getter and setter methods.• The field names are specified in the struts-

config.xml file.• New fields can be added dynamically.

Page 14: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-14 Copyright © 2004, Oracle. All rights reserved.

Creating a Dynamic Form

1. Create a form bean from the Structure pane.2. Specify

org.apache.struts.action.DynaActionForm as the type for the bean.

3. Create a new form property for each field.

<form-bean name="logonBean" type="org.apache.struts.action.DynaActionForm"><form-property name="username" type="java.lang.String"/>

Page 15: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-15 Copyright © 2004, Oracle. All rights reserved.

Using the Bean in an Action

• Using a static bean: – Create and cast the ActionForm type– Use the getXxx() method

• Using a dynamic bean:– Cast the form object passed to the execute()

method to a DynaActionForm type

AuthUserActionForm authForm = (AuthUserActionForm) form; String username=authForm.getUsername();

String username=(String)((DynaActionForm)form).get("username");

Page 16: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-16 Copyright © 2004, Oracle. All rights reserved.

Sample Page Flow

success

failure

<action-mappings> <action path="/logon“

forward="/logon.jsp"/> <action path="/authUser"

name="logonBean" type="view.AuthUserAction"> <forward

path="/logon.do“ name="failure"/>

<forward path="/menu.do“ name="success"/>

</action> <action path="/menu"

forward="/menu.jsp"/></action-mappings>

Logon

authUser

menu

Page 17: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-17 Copyright © 2004, Oracle. All rights reserved.

Sample Page Flow: Struts Elements

<action-mappings> <action path="/logon“

forward="/logon.jsp"/>

<action path="/authUser"name="logonBean"

type=" view.AuthUserAction "> <forward

path="/logon.do“ name="failure"/>

<forward path="/menu.do“ name="success"/>

</action>

<action path="/menu" forward="/menu.jsp"/>

</action-mappings>

success

failure

Logon

authUser

menu

Page 18: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-18 Copyright © 2004, Oracle. All rights reserved.

<action-mappings> <action path="/logon“

forward="/logon.jsp"/>

<action path="/authUser"name="logonBean"

type=" view.AuthUserAction ">

<forward path="/logon.do“ name="failure"/>

<forward path="/menu.do“ name="success"/>

</action>

<action path="/menu" forward="/menu.jsp"/>

</action-mappings>

Sample Page Flow: Struts Elements

success

failure

Logon

authUser

menu

Page 19: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-19 Copyright © 2004, Oracle. All rights reserved.

Sample Page Flow: Form Bean

User Name

Password

Logon

logonBean

Submit

success

failure

Logon

authUser

menu

Page 20: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-20 Copyright © 2004, Oracle. All rights reserved.

Sample Page Flow: Form Bean

<form-beans> <form-bean name="logonBean“ type="org.apache.struts.

action.DynaActionForm">

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

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

</form-bean></form-beans>

success

failure

Logon

authUser

menu

Page 21: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-21 Copyright © 2004, Oracle. All rights reserved.

Sample Page Flow

Passed to

User Name

Password

Logon

logonBean

Submit

Populates

successsuccess

failure

Logon

authUser

menu

Page 22: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-22 Copyright © 2004, Oracle. All rights reserved.

Sample Page Flow: Action Class

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException  {    DynaActionForm LAF = (DynaActionForm) form;    String un = (String)LAF.get("username");    String pw = (String)LAF.get("password");     if (un.equals("Scott")) {      return mapping.findForward("success");    } else      return mapping.findForward("failure");  }

Page 23: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-23 Copyright © 2004, Oracle. All rights reserved.

Sample Page Flow

Passed to

User Name

Password

Logon

logonBean

Submit

Populates

success

failure

Logon

authUser

menu

Page 24: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-24 Copyright © 2004, Oracle. All rights reserved.

Form Beans, Data Actions, and Data Pages

• ADF creates a form bean automatically when you create:– Data actions– Data pages

• The ADF Form Bean is called a DataForm.• DataForms do not require custom code:

– No static form bean class is needed.– No dynamic form bean declaration is needed.

• ADF uses these objects to manage form data.

Page 25: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-25 Copyright © 2004, Oracle. All rights reserved.

Summary

In this lesson, you should have learned how to:• Describe the Struts XML elements and structure• Describe the anatomy of an action• Use the execute method to enhance the behavior

of an action• Describe the use of form beans• Use a dynamic form bean

Page 26: 11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions

11-26 Copyright © 2004, Oracle. All rights reserved.

Practice 11-1: Overview

This practice covers the following topics:• Using the Page Flow Diagram• Adding data actions• Adding data pages • Creating form beans