1 / 31

Web Development with Struts: A Comprehensive Guide

Learn how to develop web applications using Struts, a popular Java framework. This guide covers all aspects of web development, from design to deployment, and includes tutorials on JDeveloper, ADF, and more.

nlanctot
Download Presentation

Web Development with Struts: A Comprehensive Guide

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Agenda • JDeveloper 10g overview • Application Developer Framework (ADF) • Web Development with Struts

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

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

  5. Oracle ADFEnd-to-end J2EE Framework • Implements standard J2EE best practices • Model-View-Controller (MVC) design pattern • Focus on the application, not the “plumbing” • Consolidation and evolution of previous frameworks Rich Clients Web and Wireless Clients Controller Model Business Services

  6. ADF Business ComponentsService Object ADF Business ComponentsQuery Object ADF Business ComponentsEntity Object ADF – Productivity With Choice Rich Client Web / Wireless View Swing / JClient JSP ADF UIX JSF Controller Struts ADF Bindings Model ADF Data Control BusinessServices JavaClasses EJBSessionBeans WebServices ADF Metadata Services JDBC TopLinkQueries EJBFinders DataAccess PersistentBusinessObjects Java Classes EJB Entity Beans TopLink Mapping

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

  8. StrutsModel 1 Architecture Model 1 Decentralized controller - in each JSP page

  9. StrutsModel 1 Architecture No MVC - Statically Linked Pages Servlet/JSP Servlet/JSP Servlet/JSP Servlet/JSP Web Server

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

  11. DB StrutsModel-View-Controller ~ Model 2 Controller HTTP State Change JavaBean Request Servlet View Selection User Actions JDBC Database HTTP Enterprise Response State Query JavaBean JSP Change Notification View Model

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

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

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

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

  16. StrutsStruts Pictorially Web Browser Jsp 1 Jsp 2 Jsp 3 Page 1 View Layer App Server request/session JSP Engine Form Bean 1 Form Bean 2 Other Bean 1 Controller Struts- config. xml Mappings Action2 .java Action3 .java Action4 .java Action1 .java • path • action • [form bean] • [forwards] Business Logic Layer Business Bean 1 Business Bean 2 Business Bean 3 JDBC Data Layer Database

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

  18. StrutsActionForm Example public 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-beanname="FAQForm" type="faqapp.web.struts.forms.FAQForm" />

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

  20. 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”); } }

  21. StrutsActionMapping • ActionMapping object maps the Actions to URI, or path • Specified in struts-config.xml <action-mappings> <action path="/faqQuery" type="faqapp.web.struts.actions.FAQQueryAction" name="FAQQueryForm" scope="request" validate="true" input="/faqQuery.do"> </action-mappings>

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

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

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

  25. 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-beanname="FAQForm" type="faqapp.web.struts.forms.FAQForm“/> <form-beanname="FAQQueryForm“ type="faqapp.web.struts.forms.FAQQueryForm" /> <!--========== Global Forward Definitions ================ --> <global-forwards type="org.apache.struts.action.ActionForward"> <forwardname="error" path= “ /WEB-INF/Error.jsp" /> <forwardname="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>

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

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

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

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

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

More Related