31

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

Embed Size (px)

Citation preview

Page 1: Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark
Page 2: Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark

Web Development with

Karsten Schulz Terp-NielsenMaster Principal Sales ConsultantOracle Denmark

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

Agenda

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

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

Full Lifecycle Support

Deploy Deploy

Test Test

Build Build

Design Design

DebuggingDebuggingDebuggingDebuggingTuningTuningTuningTuning

DataDataModelingModeling

DataDataModelingModeling

ApplicationApplicationModelingModeling

ApplicationApplicationModelingModeling

CodingCodingCodingCoding

DeploymentDeploymentDeploymentDeployment

ConfigurationConfigurationManagementManagement

ConfigurationConfigurationManagementManagement

IDE IDE PlatformPlatform

IDE IDE PlatformPlatform

InfrastructureInfrastructure

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

JDeveloper Framework Support

J2EE Frameworks– Application Development Framework (ADF)– Apache Struts support– ADF Business Components – Oracle 9i/AS10g Toplink– UiX– Java Server Faces (JSF)

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

Implements standard J2EE best practices Model-View-Controller (MVC) design pattern

Focus on the application, not the “plumbing” Consolidation and evolution of previous frameworks

Oracle ADFEnd-to-end J2EE Framework

Business ServicesBusiness Services

Web and Wireless Clients

Web and Wireless Clients

Rich Clients

ModelModel

ControllerController

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

ADF – Productivity With Choice

View

Controller

Model

BusinessServices

Swing / JClient

JSPADF UIX

JSF

Rich Client Web / Wireless

Struts

ADF Bindings

ADF Data Control

JavaClasse

s

EJBSessio

nBeans

WebServices

ADF Business Components

Service Object

JDBC

EJBFinder

s

TopLink

Queries

ADF Business

ComponentsQuery Object

DataAccess

ADF Business

ComponentsEntity Object

Java Classes

EJB Entity Beans

TopLink Mapping

PersistentBusinessObjects

AD

F M

eta

data

Serv

ices

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

StrutsModel 1 Architecture

Browser access JSP pages – JSPs access JavaBeans 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

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

StrutsModel 1 Architecture

Decentralized controller - in each JSP page

Model 1

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

StrutsModel 1 Architecture

Servlet/JSP

Servlet/JSP

Servlet/JSP

Web Server

Servlet/JSP

No MVC - Statically Linked Pages

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

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

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

State Change

Change Notification

State Query

StrutsModel-View-Controller ~ Model 2

Servlet JavaBean

EnterpriseJavaBean

Controller

JSP

View

HTTPRequest

HTTPResponse

View Selection Database

Model

JDBC DBUser Actions

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

StrutsModel-View-Controller ~ Model 2

Servlet/JSP ControllerWeb Server

Applying MVC to a Page Flow

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

StrutsWhat 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 J2EE MVC applications Bundled with JDeveloper and works on OC4J

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

StrutsComponents

ActionServlet 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

ActionForm Beans – Data (from Form) bundled into Javabean for reuse and perform validation

ActionForward class – stores path to a page where the user is sent to

ActionMapping – holds mapping that tells controller which Actions, ActionForms, ActionsForwards to use

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

StrutsComponents (2)

Struts-config.xml – Glue of the framework - holds declarations of the earlier listed components and other details. ActionServlets 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

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

StrutsStruts Pictorially

Page 1

Controller

Action2.java

Struts-config.

xml

Mappings

Business Logic Layer

Data Layer

View Layer

BusinessBean 1

JSP Engine

Jsp 1

Action3.java

Action4.java

Action1.java

BusinessBean 2

BusinessBean 3

Jsp 3

FormBean

1FormBean

2OtherBean

1

request/session

Web Browser

Database

• path• action• [form bean]• [forwards]

App Server

Jsp 2

JDBC

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

StrutsActionForm

Holds state and behavior for user input ActionForms are JavaBeans with reset() and validate() methods for user input

ActionForm extends org.apache.struts.action.ActionForm

Typically one ActionForm bean created for each HTML form

ActionForm has corresponding property for each field on HTML form

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

StrutsActionForm Examplepublic final class FAQForm extends ActionForm { private String question; private String answer; .. public String getQuestion(){ return question; } public void setQuestion(String question) { this.question = question; } .. public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((getQuestion() == null) || (getQuestion().length() < 1)) errors.add("question", new ActionError("errors.question.required")); .. return errors; }}

<!-- Logon form bean definition in struts-config.xml  --> <form-bean name="FAQForm" type="faqapp.web.struts.forms.FAQForm" />

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

StrutsAction

Action class receives the ActionForm(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

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

StrutsAction - Example

public final class FAQQueryAction extends Action { public ActionForward perform(ActionMapping mapping,

ActionForm form, HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException { String result = null; ... try { result = FAQHelper.getFAQHelper().query(actionForm, request); request.setAttribute("action", Constants.QUERY); } ... return mapping.findForward(“result”); }}

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

StrutsActionMapping

<action-mappings> <action path="/faqQuery" type="faqapp.web.struts.actions.FAQQueryAction" name="FAQQueryForm" scope="request" validate="true" input="/faqQuery.do"></action-mappings>

ActionMapping object maps the Actions to URI, or path

Specified in struts-config.xml

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

StrutsActionForward

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"/>

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

StrutsActionServlet

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 ActionServlet can be loaded for an application

Used as a black box in most cases, though it can be extended

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

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

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

Strutsstruts-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="FAQQueryForm“ type="faqapp.web.struts.forms.FAQQueryForm" /> <!-- ========== Global Forward Definitions ================   --> <global-forwards type="org.apache.struts.action.ActionForward">  <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="/faqQuery" type="faqapp.web.struts.actions.FAQQueryAction" name="FAQQueryForm" scope="request" validate="true" input="/faqQuery.do"></action-mappings>  </struts-config>

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

StrutsA Look into web.xml

<servlet>  <servlet-name>action</servlet-name>   <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param>  <param-name>application</param-name>   <param- value>ApplicationResources</param-value>   </init-param> <init-param>  <param-name>config</param-name>   <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param>  <param-name>debug</param-name>   <param-value>2</param-value>   </init-param> <init-param>  <param-name>detail</param-name>   <param-value>2</param-value>   </init-param> </servlet>

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

gets data to display (adds to beans in request/session)…or saves data from beans via business rules

creates/reuses any associated form bean

createslooks up path to determine action/ form bean

interacts with lower layers - acts as adaptor between HTTP and layers below

if submit, auto populates form bean from request params

Page 1

Controller

Action2.java

Struts-config.

xml

Mappings

Business Logic Layer

Data Layer

View Layer

BusinessBean 1

Jsp Engine

Jsp 1

Action3.java

Action1.java

BusinessBean 2

BusinessBean 3

Jsp 2 Jsp 3

FormBean

1FormBean

2

request/session

Web Browser

Business Data

reads on start-up

incoming requests

• path• action• [form bean]• [forwards]

passes control to relevant action to handlereturns appropriate forward

relevant page called

processes custom tags – fill form elements from beans, display internationalized messages

pure HTML sent to browser

Jsp 2

Action2.java

FormBean

2••

• ••• •

Application Server

Jsp 2 How it all Works

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

StrutsJSP 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

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

StrutsUsing 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"><H3>Welcome <bean:write name="user" property="username"/>!</H3></logic:present><logic:notPresent scope="session" name="user"><H3>Welcome World!</H3></logic:notPresent><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>

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

Demonstration

Develop a Web Application based on Struts and EJBs as the model