Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Deeloping Struts Applications in

Oracle JDeeloper 10g



.v Oracte !bite Paer
^orevber 2001


White Paper TitIe Page 2
Deeloping Struts Applications
in Oracle JDeeloper 10g
Introduction ....................................................................................................... 3
Struts 101............................................................................................................ 3
\hat is Struts............................................................................................... 3
\hat is MVC ............................................................................................... 3
MVC or J2LL.............................................................................................. 4
Anatomy o a Struts Application................................................................ 4
1he Struts-conig ile.................................................................................... 5
1he Struts JSP 1ag Libraries....................................................................... 6
Struts 1ags and JS1L............................................................................... 6
Struts 1ag Usage....................................................................................... 6
Key Struts Components...............................................................................
Actionserlet .............................................................................................
Actionlorms .............................................................................................
Actions....................................................................................................... 8
Struts Integration in JDeeloper 10g.............................................................. 8
Visual Struts Deelopment leatures.......................................................... 8
Visual Struts Page llow Design JDeeloper 10g................................. 8
Struts Coniguration Lditor .................................................................... 9
Struts JSP Visual Design ....................................................................... 10
Struts ADl Integration.............................................................................. 11
Struts ADl Components ...................................................................... 11
Drag and Drop ADl Deelopment.................................................... 12
Summary........................................................................................................... 13


White Paper TitIe Page 3
Deeloping Struts Applications
in Oracle JDeeloper 10g
INTRODUCTION
1his Oracle Openworld 2004 paper both introduces the Jakarta Struts technology
as well as reiews how it is integrated into the current production ersion o Oracle
JDeeloper 10gs deelopment enironment. A detailed, yet high-leel technical
introduction to Jakarta Struts is irst oered along with a reiew o how JDeeloper
10g proides a isual, intuitie Struts deelopment enironment as it`s core
technology or enterprise Jaa \eb application deelopment.
STRUTS 101
Let`s start by reiewing some background inormation on Jakarta Struts and
stepping through some o the key components o Struts technology.
What is Struts?
Jakarta Struts, simply reerred as Struts`, was created under the auspices o the
Jakarta Open Source Project with the Apache oundation. It was created as a
ramework or building enterprise J2LL \eb applications. It ollows a Model-
View-Controller ,MVC, design paradigm and uses the core J2LL technologies,
Serlets, JaaSerer Pages ,JSP, and JSP tag libraries.
Because o its intense popularity, it has become the de acto` standard or J2LL
\eb application deelopment. lor more inormation on Struts you can reer to the
website: http:,,struts.apache.org.
Beore continuing on with Struts technology, let`s quickly reiew what MVC is.
What is MVC?
Model-View-Controller`, also reerred to as Model 2`, is a design paradigm
where an application is diided into three distinct components, the Model, View
and Controller.
1he Model represent the application`s data as well as the logic required to interact
with the data. lor example a set o Jaa classes that interact with a database
ollowing a concise set o business rules could be considered the Model.
White Paper TitIe Page 4
1he View represents the User-Interace ,UI, o the application. It is how the user
interacts with the application. 1his could be a \eb page, or a traditional thick client
UI.
1he Controller is the mechanism by which user input is receied. 1his could either
be the code required to process user interactions such as clicking on buttons in a
client application, or or \eb applications it could be the serer-side process that
handles all incoming \eb requests.
1he MVC design paradigm is especially well suited or \eb applications because it
allows deelopers,architects to diide the application into it`s distinctie
components thus allowing changeability or dierent conigurations. 1his makes it
possible to hae more than one type o View component or the same Model. lor
example an application could hae more than one type o View such as an
l1ML,browser based client along with a PDA,micro-deice client using the same
Model.
MVC for J2EE
Beore Struts, MVC applications were built or J2LL using the ollowing J2LL
components:
1he Controller is implemented as a ront-end dispatcher serlet which is mapped
to a particular URL pattern such as .do`. Any \eb request with a .do` would be
routed to the Controller serlet which would then inoke the necessary page, or
View component.
As you guessed, the View components are simply JSP \eb pages.
1he Model is the business logic that manages all data interaction. Lxamples range
rom plain old Jaa classes ,POJO, or Jaa Database Connectiity ,JDBC, code to
1oplink or Lnterprise Jaa Beans ,LJB, persistence code.
1he nice thing about Struts is that it proides an out-o-the-box implementation o
MVC or J2LL
Anatomy of a Struts AppIication
A Struts application is simply a standard J2LL \eb application with the ollowing
extra components:
Struts Jaa libraries which include the JSP tag handler classes as well as the
core Struts classes, Action, Actionlorm and the ,Controller,
ActionSerlet.
Struts-Conig.xml ile. 1his seres as a master coniguration ile or all o
the components in the Struts application. It also stores the naigational
rules ,page low, o the application.
Property lile,s,. 1ypically used or handling multi-lingual messages or the
application.
White Paper TitIe Page 5
The Struts-config fiIe
Let`s drill down a bit on the Struts conig ile. 1he struts-conig.xml ile is an XML
ile that is used to keep track o all o the separate components o the entire
application. As many J2LL deelopers are aware, it is a deinite requirement to
keep track o all o the arious components in any airly medium to complex J2LL
application. 1he Struts-Conig exactly seres this purpose by proiding a single
source o truth or all components and how they interact with each other in the
Struts-Conig ile. Struts applications can also hae multiple Struts-Conig iles as
well.
A Struts-Conig ile contains inormation or the ollowing types o Struts
components:
Actionlorms ,Also known as lorm Beans,
ActionMappings
Global lorwards
Resource,Property lile Locations
And other components such as datasources and plugins.
In order or the Struts-Conig to be used by the J2LL application, it has to be
registered as an initialization parameter or the Struts Controller serlet in the
\eb.xml ile o the parent J2LL \eb application.
lere is a typical \eb.xml entry or the Struts Controller serlet which includes the
location o the Struts-Conig ile as well as the serlet mapping..
serlet
serlet-nameaction,serlet-name
serlet-classorg.apache.struts.action.ActionSerlet,serlet-class
init-param
param-nameapplication,param-name
param- alueApplicationResources,param-alue
,init-param
init-param
param-nameconig,param-name
param-alue,\LB-INl,struts-conig.xml,param-alue
,init-param
,serlet
serlet-mapping
serlet-nameaction,serlet-name
url-pattern.do,url-pattern
,serlet-mapping
White Paper TitIe Page 6
The Struts JSP Tag Libraries
Struts also proides a set o useul custom JSP tag libraries which aid in typical \eb
application deelopment. 1he key tag libraries are:
Struts l1ML - 1ags that enable l1ML application UI deelopment.
Includes text input ields, orm ields along with buttons including
checkboxwes, radiobuttons and submit buttons.
Struts Bean - 1ags useul or accessing beans, their properties,and deining
beans based on access
Struts Logic - 1ags or ealuating logic on the \eb page.
Struts Tags and JSTL
It should be noted that the Struts tag libraries are beginning to be supplanted by the
introduction o the JaaSerer Pages Standard 1ag Library ,JS1L,. JS1L actually
proides a similar set o unctions including logic and data presentation. JS1L was
also originally deeloped under the auspices o the Jakarta Apache oundation and
it`s goal was to reduce the need or J2LL deelopers to hae to build their custom
tag libraries by proiiding a core set o common use libraries such as tags or
iterating and presenting data. It has now moed into the Jaa Community Process
JCP as Jaa Speciication Request ,JSR, 52. lor more ino on JS1L see:
http:,,jaa.sun.com,products,jsp,jstl,
Many J2LL and architects are now opting to use a combination o both JS1L and
Struts tags in their deelopment projects.
Struts Tag Usage
lere is an example o a JSP page using seeral Struts tags:
< taglib uri="/tags/struts-bean" prefix="bean" >
< taglib uri="/tags/struts-html" prefix="html" >
< taglib uri="/tags/struts-logic" prefix="logic" >
l1ML
lLAD
1I1LL\elcome!,1I1LL
<html:base/>
,lLAD
BOD\
<logic:present name="user">
l3\elcome <bean:write name="user" property="username"/>!,l3
</logic:present>
<logic:notPresent scope="session" name="user">
l3\elcome \orld!,l3
</logic:notPresent>
<html:errors/>
White Paper TitIe Page 7
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
<html:image src="struts-power.gif" alt="Powered by struts"/>
,BOD\
,l1ML
l\I: JS1L equialent tags could actually replace most o these Struts tags. lor
example the <bean:write> tag could be replaced by the JS1L <c:out> tag.

Key Struts Components
Let`s reiew the most important Struts Jaa components. 1hey consist o the
ollowing classes:
ActionSerlet
Actionlorm
Action
ActionservIet
As mentioned beore the ActionSerlet is the ront-end Controller serlet which
handles all \eb requests based on a speciic URL mapping ,such as .do, and
routes the requests to the appropriate pages.
Question: \hy do you need a Controller serlet
Answer: 1he Controller serlet seres as the \eb application gatekeeper and
controls all access to your application. It also ensures that the application behaes
correctly such as making data aailable to JSP pages ia the context. lor example a
request to employees.do would trigger a Struts Action to irst query a database
table o employees and make the results aailable in the JSP Context or rendering
in the employees.jsp page.
ActionForms
Actionlorms, which are also reerred to as lorm Beans`, proide a direct
mapping o l1ML orm elements to Jaa bean ields. A typical Actionlorm ,lorm
Bean, is simply a Jaa Class that extends the Struts Actionlorm and has seeral
ields that represent an l1ML orm. An l1ML orm in a JSP page can hae Struts
text ields which are mapped to the ields in the lorm bean.
White Paper TitIe Page 8
In many cases l1ML orms are sometimes generated dynamically. 1o handle this,
Struts has an XML-based dynamic lorm bean ,DynaActionlorm, that can be used
without requiring a Jaa class or a lorm bean.
Actions
And inally the Struts Action class seres as a container or any non-UI sererside
processing. Struts Actions are the true building blocks o Struts applications and are
typically called as the action or l1ML orms on JSP pages
In order or Struts Actions to be accessible ia a \eb requests,posts they need to
be mapped to a URL using an ActionMapping. 1he ActionMapping, which is
deined in the Struts-Conig.xml, associates a URL path such
as/getLmployeeList.do to the Action class GetLmployees.
Once a Struts Action has been executed, it needs a place to go. lor example when
the Action has succesully processed an incoming set o login ields, it needs to
orward` the user to an appropriate \eb page that could indicate a successul
login. 1his is done with an Action forward which is also deined in the Struts-
Conig ile as a child o the Action node:

action name~"login" path~",loginAction" input~"login.jsp"
type~"strutsun.iew.LoginAction"
orward name~"success" path~",success.jsp",
,action

Now that we hae reiewed the core components o a Struts application, let`s moe
on to how Struts has been integrated into JDeeloper 10g.
STRUTS INTEGRATION IN JDEVELOPER 10G
VisuaI Struts DeveIopment Features
1he goal o the Struts integration into JDeeloper 10g is to improe the
productiity o a Struts deeloper by proiding an intuitie, isual deelopment
enironment or all acets o Struts deelopment. It should be noted that at the
time o writing this paper, both the current production ersion o JDeeloper 10g,
ersion 905.2, as well as the new JDeeloper 10.1.3 Deeloper Preiew oer isual
Struts deelopment. loweer the new preiew ersion does not yet proide the ull
Application Deelopment lramework ,ADl, and it`s integration with Struts.
VisuaI Struts Page FIow Design in JDeveIoper 10g
Obiously the biggest isual Struts deelopment eature in JDeeloper 10g is the
Struts Page llow Modeler.
1he Struts Page llow Modeler proides a isual birds-eye` iew o the entire
Struts application and can sere as the starting point or all Struts Deelopment.
lrom the Struts Page llow Modeler you can drag and drop pages, Actions,
White Paper TitIe Page 9
lorwards and Page Links. As you drag and drop items onto the Page llow diagram
they appear in their unrealized` state. \ou can then double-click on them to
generate code or them.
It is possible to link two pages together using the Page Link` connector. \hen
attached between two pages, it will insert a Struts Link tag into the starting page
with a reerence to the target page.
A Struts lorward` can be isually connected rom a Struts Action to another page
or Action. 1his will insert a lorward` into the Struts-Conig ile or the starting
Action with a reerence to the target path.


Struts Configuration Editor
lor those who wish to edit the Struts-conig ile more directly than using the Page
llow Modeler but still preer not to edit raw XML, they can use the Struts-
Coniguration Lditor. 1he Struts Coniguration Lditor proides a productie,
dialog based, editor or productiely editing the Struts-Conig ile.

White Paper TitIe Page 10


Inoking the Coniguration editor is done by right clicking on the ,\eb-
In,Struts-Conig.xml and selecting Ldit Struts-Conig.`
Struts JSP VisuaI Design
JDeeloper 10g`s JSP isual editor is ery similar to other leading l1ML design
tools except that it is enhanced speciically or JSP isual deelopment. lor
example custom JSP tags can be isually rendered in the JSP isual editor. 1his
means that they will appear in their runtime state at design time!
lor example the Struts l1ML tags actually appear in their runtime l1ML state in
the JSP isual editor. 1he Struts text ields render as l1ML text ields, Struts
buttons render as buttons etc. Setting tag attributes is simply done by clicking on
the tag ,also reerred to as components, in the isual editor and then editing its
attributes ,properties, in the Property Inspector on the lower right.
White Paper TitIe Page 11


Struts ADF Integration
1he current production erson o JDeeloper 10g proides a comprehensie
deelopment enironment that promotes isual, rapid deelopment o J2LL
applications using the Application Deelopment lramework ,ADl,. ADl is
actually built on standard J2LL technologies including Struts, JS1L, LJBs, and
1oplink. lor \eb application deelopment, ADl relies extensiely on a
combination o Struts and JS1L technologies. lor example ADl \eb applications
use the Struts Controller or the page low naigation and Struts Actions to gather
ADl data. ADl databound \eb pages use a combination o Struts and JS1L tags
to render and oer data editing. Let`s examine the key ADl-Struts components
that make this possible.
Struts ADF Components
DataAction
In order to acilitate databinding to any datasource using ADl, a prewired`
DataAction is proided in JDeeloper 10g. 1his essentially is a standard Struts
Action but pre-wired to hae to access to ADl data. \hen executed, the ADl
DataAction opens a connection to any ADl data by making it aailable in the JSP
context. A JSP \eb page can then display simply by using an LL expression in a
JS1L tag.
White Paper TitIe Page 12

,Note: JS1L proides a custom tags which use a compact notation known as
Lxpression Language ,LL, or accessing J2LL application data.,
DataPage
Another ADl enhancement to Struts is the ADl DataPage component. 1his is just
a combination o a JSP page and an ADl DataAction. DataPages are used when
any ADl databinding is needed in the page.
\hen a DataPage receies a request, irst the built-in DataAction makes any ADl
data aailable in the JSP context and then orwards to the JSP page where it can
then display ADl data using LL expressions.

,l\I: JSP 2.0 has built-in support or Lxpression Language, thus no longer
requiring a custom JS1L tag to render an LL expression.,
Drag and Drop ADF DeveIopment
Building databound Struts applications is a snap with JDeeloper 10g`s ADl isual
databinding tools.
1he Data Control Palette displays any mid-tier data ranging rom LJB`s, 1oplink
classes, Plain Old Jaa Objects ,POJOs, etc. and makes them aailable or easy drag
and drop databinding.
1o build a databound table or input orm which which is built with Struts and JS1L
tags, one simply selects the data rom the Palette be it either a single alue or a
collection o alues, then selects what type o Component to Drag and Drop it
as:` and then drags it onto the page.
lor example to display a read only l1ML table o data rom an Lmployees
database table, one irst selects the Lmployees Data Control in the Data Control
Palette, speciies Read-Only 1able` as the drop as` component in the dropdown
list and then drags it onto the JSP page.
\hat results is an l1ML table with embedded JS1L and Struts tags to render the
Lmployees data.
White Paper TitIe Page 13


By using a combination o dierent types o components rom the Data Control
Palette, one can ery quickly isually build a ariety o dierent types o databound
Struts \eb applications.

SUMMARY
1he last ew years hae proen that Jakarta Struts is a De lacto standard with more
and more businesses depending on it or an oerall J2LL \eb deelopment
ramework. Oracle JDeeloper 10g understands this and has made it a key goal to
cater the numerous Struts deelopers and een enhance their abilities by oering
the most productie and easy to use isual Struts deelopment experience. In
addition to oering superior core Struts isual deelopment support, JDeeloper
has also integrated it`s ADl ramework with Struts so that it can ully take
adantage o both Struts and it`s MVC \eb application design paradigm as well as
the powerul databinding capabilities o the Oracle Application Deelopment
lramework.


DeveIoping Struts AppIications in JdeveIoper 10g
November 2004
Chris SchaIk

OracIe Corporation
WorId Headquarters
500 OracIe Parkway
Redwood Shores, CA 94065
U.S.A.

WorIdwide Inquiries:
Phone: +1.650.506.7000
Fax: +1.650.506.7200
www.oracIe.com

Copyright 2004, OracIe. AII rights reserved.
This document is provided for information purposes onIy
and the contents hereof are subject to change without notice.
This document is not warranted to be error-free, nor subject to
any other warranties or conditions, whether expressed oraIIy
or impIied in Iaw, incIuding impIied warranties and conditions of
merchantabiIity or fitness for a particuIar purpose. We specificaIIy
discIaim any IiabiIity with respect to this document and no
contractuaI obIigations are formed either directIy or indirectIy
by this document. This document may not be reproduced or
transmitted in any form or by any means, eIectronic or mechanicaI,
for any purpose, without our prior written permission.
OracIe is a registered trademark of OracIe Corporation and/or its
affiIiates. Other names may be trademarks of their respective owners.

You might also like