White Paper TitIe Page 2 Deeloping Struts Applications in Oracle JDeeloper 10g Introduction ....................................................................................................... 3 Struts 101............................................................................................................ 3 \hat is Struts............................................................................................... 3 \hat is MV( ............................................................................................... 3 MV( or J2LL.............................................................................................. 4 Anatomv 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 Kev Struts (omponents............................................................................... Actionserlet ............................................................................................. Actionlorms ............................................................................................. Actions....................................................................................................... 8 Struts Integration in JDeeloper 10g.............................................................. 8 Visual Struts Deelopment leatures.......................................................... 8 Visual Struts Page llow Design JDeeloper 10g................................. 8 Struts (oniguration Lditor .................................................................... 9 Struts JSP Visual Design ....................................................................... 10 Struts ADl Integration.............................................................................. 11 Struts ADl (omponents ...................................................................... 11 Drag and Drop ADl Deelopment.................................................... 12 Summarv........................................................................................................... 13
White Paper TitIe Page 3 Deeloping Struts Applications in Oracle JDeeloper 10g INTRODUCTION 1his Oracle Openworld 2004 paper both introduces the Jakarta Struts technologv as well as reiews how it is integrated into the current production ersion o Oracle JDeeloper 10gs deelopment enironment. A detailed. vet 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 technologv or enterprise Jaa \eb application deelopment. STRUTS 101 Let`s start bv reiewing some background inormation on Jakarta Struts and stepping through some o the kev components o Struts technologv. What is Struts? Jakarta Struts. simplv 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-(ontroller MV(, design paradigm and uses the core J2LL technologies: Serlets. JaaSerer Pages JSP, and JSP tag libraries. Because o its intense popularitv. it has become the de acto` standard or J2LL \eb application deelopment. lor more inormation on Struts vou can reer to the website: http:struts.apache.org. Beore continuing on with Struts technologv. let`s quicklv reiew what MV( is. What is MVC? Model-View-(ontroller`. also reerred to as Model 2`. is a design paradigm where an application is diided into three distinct components: the Model. View and (ontroller. 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 (ontroller is the mechanism bv 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 MV( design paradigm is especiallv well suited or \eb applications because it allows deelopersarchitects to diide the application into it`s distinctie components thus allowing changeabilitv or dierent conigurations. 1his makes it possible to hae more than one tvpe o View component or the same Model. lor example an application could hae more than one tvpe o View such as an l1MLbrowser based client along with a PDAmicro-deice client using the same Model. MVC for J2EE Beore Struts. MV( 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`. Anv \eb request with a .do` would be routed to the (ontroller serlet which would then inoke the necessarv page. or View component. As vou guessed. the View components are simplv 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 (onnectiitv JDB(, 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 MV( or J2LL Anatomy of a Struts AppIication A Struts application is simplv 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 (ontroller, ActionSerlet. Struts-(onig.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. Propertv liles,. 1vpicallv 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 manv J2LL deelopers are aware. it is a deinite requirement to keep track o all o the arious components in anv airlv medium to complex J2LL application. 1he Struts-(onig exactlv seres this purpose bv proiding a single source o truth or all components and how thev interact with each other in the Struts-(onig ile. Struts applications can also hae multiple Struts-(onig iles as well. A Struts-(onig ile contains inormation or the ollowing tvpes o Struts components: Actionlorms Also known as lorm Beans, ActionMappings Global lorwards ResourcePropertv lile Locations And other components such as datasources and plugins. In order or the Struts-(onig to be used bv the J2LL application. it has to be registered as an initialization parameter or the Struts (ontroller serlet in the \eb.xml ile o the parent J2LL \eb application. lere is a tvpical \eb.xml entrv or the Struts (ontroller serlet which includes the location o the Struts-(onig ile as well as the serlet mapping.. serlet serlet-nameactionserlet-name serlet-classorg.apache.struts.action.ActionSerletserlet-class init-param param-nameapplicationparam-name param- alueApplicationResourcesparam-alue init-param init-param param-nameconigparam-name param-alue\LB-INlstruts-conig.xmlparam-alue init-param serlet serlet-mapping serlet-nameactionserlet-name url-pattern.dourl-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 tvpical \eb application deelopment. 1he kev 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 bv the introduction o the JaaSerer Pages Standard 1ag Librarv JS1L,. JS1L actuallv proides a similar set o unctions including logic and data presentation. JS1L was also originallv 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 bv proiiding a core set o common use libraries such as tags or iterating and presenting data. It has now moed into the Jaa (ommunitv Process J(P as Jaa Speciication Request JSR, 52. lor more ino on JS1L see: http:jaa.sun.comproductsjspjstl Manv 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: <m taglib uri="/tags/struts-bean" prefix="bean" > <m taglib uri="/tags/struts-html" prefix="html" > <m 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 actuallv replace most o these Struts tags. lor example the <bean:write> tag could be replaced bv the JS1L <c:out> tag.
Key Struts Components Let`s reiew the most important Struts Jaa components. 1hev consist o the ollowing classes: ActionSerlet Actionlorm Action ActionservIet As mentioned beore the ActionSerlet is the ront-end (ontroller serlet which handles all \eb requests based on a speciic URL mapping such as .do, and routes the requests to the appropriate pages. Ouestion: \hv do vou need a (ontroller serlet Answer: 1he (ontroller serlet seres as the \eb application gatekeeper and controls all access to vour application. It also ensures that the application behaes correctlv such as making data aailable to JSP pages ia the context. lor example a request to emplovees.do would trigger a Struts Action to irst querv a database table o emplovees and make the results aailable in the JSP (ontext or rendering in the emplovees.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 tvpical Actionlorm lorm Bean, is simplv a Jaa (lass 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 manv cases l1ML orms are sometimes generated dvnamicallv. 1o handle this. Struts has an XML-based dvnamic lorm bean DvnaActionlorm, that can be used without requiring a Jaa class or a lorm bean. Actions And inallv the Struts Action class seres as a container or anv non-UI sererside processing. Struts Actions are the true building blocks o Struts applications and are tvpicallv called as the action or l1ML orms on JSP pages In order or Struts Actions to be accessible ia a \eb requestsposts thev need to be mapped to a URL using an ActionMapping. 1he ActionMapping. which is deined in the Struts-(onig.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 succesullv 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- (onig ile as a child o the Action node:
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 productiitv o a Struts deeloper bv 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 vet proide the ull Application Deelopment lramework ADl, and it`s integration with Struts. VisuaI Struts Page FIow Design in JDeveIoper 10g Obiouslv the biggest isual Struts deelopment eature in JDeeloper 10g is the Struts Page llow Modeler. 1he Struts Page llow Modeler proides a isual birds-eve` iew o the entire Struts application and can sere as the starting point or all Struts Deelopment. lrom the Struts Page llow Modeler vou can drag and drop pages. Actions. White Paper TitIe Page 9 lorwards and Page Links. As vou drag and drop items onto the Page llow diagram thev 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 isuallv connected rom a Struts Action to another page or Action. 1his will insert a lorward` into the Struts-(onig 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 directlv than using the Page llow Modeler but still preer not to edit raw XML. thev can use the Struts- (oniguration Lditor. 1he Struts (oniguration Lditor proides a productie. dialog based. editor or productielv editing the Struts-(onig ile.
White Paper TitIe Page 10
Inoking the (oniguration editor is done bv right clicking on the \eb- InStruts-(onig.xml and selecting Ldit Struts-(onig.` Struts JSP VisuaI Design JDeeloper 10g`s JSP isual editor is erv similar to other leading l1ML design tools except that it is enhanced speciicallv or JSP isual deelopment. lor example custom JSP tags can be isuallv rendered in the JSP isual editor. 1his means that thev will appear in their runtime state at design time! lor example the Struts l1ML tags actuallv 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 simplv done bv clicking on the tag also reerred to as components, in the isual editor and then editing its attributes properties, in the Propertv 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 actuallv built on standard J2LL technologies including Struts. JS1L. LJBs. and 1oplink. lor \eb application deelopment. ADl relies extensielv on a combination o Struts and JS1L technologies. lor example ADl \eb applications use the Struts (ontroller 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 kev ADl-Struts components that make this possible. Struts ADF Components DataAction In order to acilitate databinding to anv datasource using ADl. a prewired` DataAction is proided in JDeeloper 10g. 1his essentiallv is a standard Struts Action but pre-wired to hae to access to ADl data. \hen executed. the ADl DataAction opens a connection to anv ADl data bv making it aailable in the JSP context. A JSP \eb page can then displav simplv bv 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 anv ADl databinding is needed in the page. \hen a DataPage receies a request. irst the built-in DataAction makes anv ADl data aailable in the JSP context and then orwards to the JSP page where it can then displav 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 (ontrol Palette displavs anv mid-tier data ranging rom LJB`s. 1oplink classes. Plain Old Jaa Objects POJOs, etc. and makes them aailable or easv drag and drop databinding. 1o build a databound table or input orm which which is built with Struts and JS1L tags. one simplv selects the data rom the Palette be it either a single alue or a collection o alues. then selects what tvpe o (omponent to Drag and Drop it as:` and then drags it onto the page. lor example to displav a read onlv l1ML table o data rom an Lmplovees database table. one irst selects the Lmplovees Data (ontrol in the Data (ontrol Palette. speciies Read-Onlv 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 Lmplovees data. White Paper TitIe Page 13
Bv using a combination o dierent tvpes o components rom the Data (ontrol Palette. one can erv quicklv isuallv build a arietv o dierent tvpes o databound Struts \eb applications.
SUMMARY 1he last ew vears 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 kev goal to cater the numerous Struts deelopers and een enhance their abilities bv oering the most productie and easv 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 ullv take adantage o both Struts and it`s MV( \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
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.