Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions."— Presentation transcript:

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

2 11-2 Copyright © 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do 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

3 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

4 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

5 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"); } }

6 11-6 Copyright © 2004, Oracle. All rights reserved. Forwards Forwards can be defined: By using the Page Flow Diagram In the XML file In the Structure pane /authUser success failure /page1 /page2

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

8 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. return mapping.findForward("help");

9 11-9 Copyright © 2004, Oracle. All rights reserved. Form Beans User Name Password Logon logonBean Submit Logon authUser menu Populates

10 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

11 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

12 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( … }

13 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.

14 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.

15 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");

16 11-16 Copyright © 2004, Oracle. All rights reserved. Sample Page Flow success failure <action path="/authUser" name="logonBean" type="view.AuthUserAction"> Logon authUser menu

17 11-17 Copyright © 2004, Oracle. All rights reserved. Sample Page Flow: Struts Elements <action path="/authUser" name="logonBean" type=" view.AuthUserAction "> success failure Logon authUser menu

18 11-18 Copyright © 2004, Oracle. All rights reserved. <action path="/authUser" name="logonBean" type=" view.AuthUserAction "> Sample Page Flow: Struts Elements success failure Logon authUser menu

19 11-19 Copyright © 2004, Oracle. All rights reserved. Sample Page Flow: Form Bean User Name Password Logon logonBean Submit success failure Logon authUser menu

20 11-20 Copyright © 2004, Oracle. All rights reserved. Sample Page Flow: Form Bean <form-bean name="logonBean“ type="org.apache.struts. action.DynaActionForm"> success failure Logon authUser menu

21 11-21 Copyright © 2004, Oracle. All rights reserved. Sample Page Flow Passed to User Name Password Logon logonBean Submit Populates success failure Logon authUser menu

22 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"); }

23 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

24 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.

25 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

26 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

27 11-27 Copyright © 2004, Oracle. All rights reserved. Practice 11-1

28 11-28 Copyright © 2004, Oracle. All rights reserved. Practice 11-1

29 11-29 Copyright © 2004, Oracle. All rights reserved. Practice 11-1

30 11-30 Copyright © 2004, Oracle. All rights reserved. Practice 11-1


Download ppt "11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions."

Similar presentations


Ads by Google