Web Development with Karsten Schulz TerpNielsen Master Principal


Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark

Agenda Ÿ JDeveloper 10 g overview Ÿ Application Developer Framework (ADF) Ÿ Web Development with Struts

Full Lifecycle Support Design Application Modeling Build Test Deploy Data Modeling Coding Tuning Debugging Deployment Infrastructure IDE Platform Configuration Management

JDeveloper Framework Support Ÿ J 2 EE Frameworks – – – Application Development Framework (ADF) Apache Struts support ADF Business Components Oracle 9 i/AS 10 g Toplink Ui. X Java Server Faces (JSF)

Oracle ADF End-to-end J 2 EE Framework Ÿ Implements standard J 2 EE best practices Ÿ Model-View-Controller (MVC) design pattern Rich Clients Web and Wireless Clients Controller Model Business Services Ÿ Focus on the application, not the “plumbing” Ÿ Consolidation and evolution of previous frameworks

ADF – Productivity With Choice Rich Client Web / Wireless Swing / JClient JSP ADF UIX JSF Controller Struts ADF Metadata Services ADF Bindings Model ADF Data Control Java Classes JDBC EJB Session Beans Top. Link Queries Java Classes ADF Business Components Service Object EJB Finders EJB Entity Beans Top. Link Mapping View Web Services ADF Business Components Query Object ADF Business Components Entity Object Data Access Persistent Business Objects Business Services

Struts Model 1 Architecture Ÿ Browser access JSP pages – JSPs access Java. Beans that represent model Ÿ Control de-centralized – current page display, determined next page to display Ÿ Complex navigation requires use of scriplet code – Blurs the line between presentation and navigation code and making reuse difficult Ÿ Not a model to use in practice - maintenance difficult and does not scale well

Struts Model 1 Architecture Model 1 Decentralized controller - in each JSP page

Struts Model 1 Architecture No MVC - Statically Linked Pages Servlet/ JSP Web Server Servlet/ JSP

Struts Model-View-Controller ~ Model 2 Ÿ Introduces a controller servlet – – Controller Servlet handle the data access and navigational flow JSPs handle presentation Ÿ Controller centralizes the logic for dispatching requests to the next view based on the request URL, input parameters, and application state Ÿ Controller also handles view selection, which decouples JSP pages and servlets from one another

Struts Model-View-Controller ~ Model 2 Controller HTTP Request HTTP Response Servlet User Actions JSP State Change Java. Bean View Selection State Query JDBC Enterprise Java. Bean Change Notification View Model DB Database

Struts Model-View-Controller ~ Model 2 Applying MVC to a Page Flow Web Server Servlet/JSP Controller

Struts What is It ? Ÿ Open source Apache Jakarta Project Ÿ An implementation of MVC paradigm Ÿ A framework written in Java, to build web tier employing servlets, JSPs and XML Ÿ De facto standard for J 2 EE MVC applications Ÿ Bundled with JDeveloper and works on OC 4 J

Struts Components Ÿ Action. Servlet class – Part of controller that receives user input and state changes and issues view selections Ÿ Action class – Part of controller that interacts with model to execute a state change or query Ÿ Action. Form Beans – Data (from Form) bundled into Javabean for reuse and perform validation Ÿ Action. Forward class – stores path to a page where the user is sent to Ÿ Action. Mapping – holds mapping that tells controller which Actions, Action. Forms, Actions. Forwards to use

Struts Components (2) Ÿ Struts-config. xml – Glue of the framework - holds declarations of the earlier listed components and other details. Action. Servlets reads this file and create the configuration objects in memory Ÿ Strut Tag Libraries – Custom tags that simplify working with Struts aiding the view component and access Model thru’ Java Beans Ÿ Property files – store messages and handle different languages

Struts Pictorially Page 1 Web Browser App Server Jsp 1 Jsp 2 Jsp 3 Form Bean 1 JSP Engine request/session Form Bean 2 Other Bean 1 View Layer Controller Strutsconfig. xml Mappings • path • action • [form bean] • [forwards] Action 1. java Business Bean 1 Action 2. java Action 3. java Business Bean 2 JDBC Database Action 4. java Business Bean 3 Business Logic Layer Data Layer

Struts Action. Form Ÿ Holds state and behavior for user input Ÿ Action. Forms are Java. Beans with reset() and validate() methods for user input Ÿ Action. Form extends org. apache. struts. action. Action. Form Ÿ Typically one Action. Form bean created for each HTML form Ÿ Action. Form has corresponding property for each field on HTML form

Struts Action. Form Example public final class FAQForm extends Action. Form { private String question; private String answer; . . public String get. Question(){ return question; } public void set. Question(String question) { this. question = question; }. . public Action. Errors validate(Action. Mapping mapping, Http. Servlet. Request request) { Action. Errors errors = new Action. Errors(); if ((get. Question() == null) || (get. Question(). length() < 1)) errors. add("question", new Action. Error("errors. question. required")); . . return errors; } } <!-- Logon form bean definition in struts-config. xml --> -bean name="FAQForm" type="faqapp. web. struts. forms. FAQForm" /> <form

Struts Action Ÿ Action class receives the Action. Form(which holds the data) and you perform action/processing here Ÿ Action provides a loose coupling between Web tier and business tier Ÿ Controller decides which Action class, using the mapping info Ÿ Action is invoked by calling perform() method

Struts Action - Example public final class FAQQuery. Action extends Action { public Action. Forward perform(Action. Mapping mapping, Action. Form form, Http. Servlet. Request request, Http. Servlet. Response response) throws IOException, Servlet. Exception { String result = null; . . . try { result = FAQHelper. get. FAQHelper(). query(action. Form, request); request. set. Attribute("action", Constants. QUERY); }. . . return mapping. find. Forward(“result”); } }

Struts Action. Mapping Ÿ Action. Mapping object maps the Actions to URI, or path Ÿ Specified in struts-config. xml <action-mappings> <action path="/faq. Query" type="faqapp. web. struts. actions. FAQQuery. Action" name="FAQQuery. Form" scope="request" validate="true" input="/faq. Query. do"> </action-mappings>

Struts Action. Forward Ÿ Encapsulates the destination of where to send control upon completion of the Action Ÿ The destination(JSP, HTML file , servlet) is detailed in the configuration file – struts-config. xml Ÿ Objects that have logical name for referring and a path property <! -- Forward Definition in struts-config. xml <forward name="success" path="/WEB-INF/FAQQuery. jsp"/> -->

Struts Action. Servlet Ÿ Primary task is to route request URI to an Action class using the configuration information(struts-config. xml) Ÿ Is the director - working behind the scene, administering the behavior of controller Ÿ Configured using web. xml of the application and mapped to receive url patterns such as *. do Ÿ Only one Action. Servlet can be loaded for an application Ÿ Used as a black box in most cases, though it can be extended

Struts struts-config. xml Ÿ Holds declarations of Struts components that define the configuration Ÿ Living blueprint of your application – – – What fields are on the forms Where JSPs are found Knows about every action and the resources needed Ÿ Power and flexibility of Struts is due to extracting out configuration information, across the frame work into struts-config. xml

Struts struts-config. xml - Example <? xml version="1. 0" encoding="ISO-8859 -1" ? > <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1. 0//EN” "http: //jakarta. apache. org/struts/dtds/struts-config_1_0. dtd"> <struts-config> <!-- ===== Form Bean Definitions ============= --> <!-- FAQForm form bean --> <form-bean name="FAQForm" type="faqapp. web. struts. forms. FAQForm“/> <form-bean name="FAQQuery. Form“ type="faqapp. web. struts. forms. FAQQuery. Form" /> <!-- ===== Global Forward Definitions ======== --> <global-forwards type="org. apache. struts. action. Action. Forward"> <forward name="error" path= “ /WEB-INF/Error. jsp" /> <forward name="warning" path= “ /WEB-INF/Warning. jsp" /> </global-forwards> <!-- ===== Action Mapping Definitions ========== --> <action-mappings> <action path="/faq. Query" type="faqapp. web. struts. actions. FAQQuery. Action" name="FAQQuery. Form" scope="request" validate="true" input="/faq. Query. do"> </action-mappings> </struts-config>

Struts A Look into web. xml <servlet> <servlet-name>action</servlet-name> <servlet-class>org. apache. struts. action. Action. Servlet</servlet-class> <init-param> <param-name>application</param-name> <param- value>Application. Resources</param-value> </init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config. xml</param-value> </init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> </servlet>

Jsp 1 Page 1 Web Browser Jsp 22 Jsp pure HTML sent to browser processes custom tags – fill form elements from beans, display internationalized messages request/session Form Jsp Engine Form Bean Application Server incoming requests relevant page called reads on start-up How it all Works Jsp 3 Controller 1 if submit, auto any creates/reuses populates form associated form bean from request params • • • • • Bean 2 View Layer looks up path to determine creates passes control to relevant returns appropriate action/ form bean action to handle forward Strutsconfig. xml Mappings • • path action [form bean] [forwards] Action 2. java Action 1. java Action 3. java interacts lower (adds layersto - beans gets data with to display acts as adaptor betweensaves HTTPdata in request/session)…or and layers from beansbelow via business rules Business Bean 1 Business Bean 2 Business Data Business Bean 3 Business Logic Layer Data Layer

Struts JSP Tag Libraries Ÿ HTML JSP custom tags – – bridge between a JSP view and the other components of a Web application Tags relating to HTML forms, message and error handling, maintaining hyperlinks and internalization Ÿ Bean JSP custom tags – – Tags useful for accessing beans, their properties, and defining beans based on access Create new beans based on the value of request cookies, headers, and parameters in any scope Ÿ Logic tags – Tags for conditional generation of output text, looping, comparison/sub-string matching

Struts Using Struts JSP tags <%@ taglib uri="/tags/struts-bean" prefix="bean" %> <%@ taglib uri="/tags/struts-html" prefix="html" %> <%@ taglib uri="/tags/struts-logic" prefix="logic" %> <HTML> <HEAD> <TITLE>Welcome!</TITLE> <html: base/> </HEAD> <BODY> <logic: present name="user"> <H 3>Welcome <bean: write name="user" property="username"/>!</H 3> </logic: present> <logic: not. Present scope="session" name="user"> <H 3>Welcome World!</H 3> </logic: not. Present> <html: errors/> <UL> <LI><html: link forward="logon">Sign in</html: link></LI> <logic: present name="user"> <LI><html: link forward="logoff">Sign out</html: link></LI> </logic: present> </UL> <IMG src='struts-power. gif' alt='Powered by Struts'> </BODY> </HTML>

Demonstration Develop a Web Application based on Struts and EJBs as the model
- Slides: 31