V041003 Web Tier Architecture MVC and Struts. v041003 First, there were servlets And things were good Faster than CGI programs More scalable with built-in.

Slides:



Advertisements
Similar presentations
Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark.
Advertisements

Apache Struts Technology
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
Web MVC-2: Apache Struts Rimon Mikhaiel
MVC Frameworks Alternatives to coding the MVC pattern.
Struts1 Apache Struts Dr Jim Briggs. Struts2 What is Struts? Struts is an open source framework for building Java web applications Supports MVC/Model.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
Struts Basics SSE USTC Qing Ding. Agenda What is and Why Struts? Struts architecture – Controller: Focus of this presentation – Model – View Struts tag.
1 Build a Web Application on J2EE. 2 J2EE Scenario Client – Web Server – EIS Resources Client – Web Server – EIS Resources Client – Application Server.
DT228/3 Web Development JSP: Directives and Scripting elements.
Objectives:1. Investigate the Web application deployment descriptor 2. Install and deploy Tomcat and struts 3. Design and deploy a struts application Struts.
Integrating Servlets and JavaServer Pages Vijayan Sugumaran School of Business Administration Oakland University Parts of this presentation provided by.
Web programming for project students Dr Jim Briggs.
Apache Struts Technology A MVC Framework for Java Web Applications.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Building an application … with Struts! Presented by Ted Husted [ ]
Struts 2.0 an Overview ( )
Struts. Agenda Preface Struts and its components An example The architecture required for Struts Applications.
Struts:The good, the bad, the ugly A detailed evaluation of Struts 1.1 and Enterprise Web Applications. By Paul Smith Ancept, Inc. (
UNIT-V The MVC architecture and Struts Framework.
Java Frameworks Indy Java Users Group January 29, 2003.
Author: DoanNX Version 2.0/Time: 30’. The ways to solve it There are 2 main ways:  Client-side: Using JavaScript (now one very good option is JQuery).
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Introduction to MVC and the Jakarta Struts Framework By Gyanendra Dwivedi Software Consultant.
JavaServer Faces Jeff Schmitt October 5, Introduction to JSF Presents a standard framework for building presentation tiers for web applications.
Basic Struts Architecture Client Server Database Request Response Control View Model Server Struts Framework.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Jakarta Struts Presented by Object Computing, Inc. (OCI) Written by Greg Elliott
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
8/15/2003System Specialists Corporation How to implement BC4J with the Struts Framework Eugene Sicat Technical Architect/Project Manager System Specialists.
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Java Servlets & Java Server Pages Lecture July 2013.
Chapter 7 Using Custom Tag Libraries and the JSP Standard Tag Library.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages (JSP)
Struts Framework Anna Paščenko. What is Struts?  An open source framework for building Java web applications.
Struts An Open-source Architecture for Web Applications Facilitator: Tripti Shukla.
J2EE Struts 1 3 Struts: Introduction 1 4 Once you’ve coded a lot of JSP applications, you find yourself doing a lot of repetitive things. Also,
CSC 2720 Building Web Applications Frameworks for Building Web Applications.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
Java Enterprise Edition Programming Page 1 of 9Configuring Servlets Web Application Context Name  In multiple web applications, a “context name” is used.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Oracle Application Development Framework Objectives 1-2 J2EE Platform 1-3 Benefits of the J2EE.
13 Copyright © 2004, Oracle. All rights reserved. Adding Validation and Error Handling.
11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions.
Struts Framework Day-2 Ashok Chakravarti. DataSource Usage Sample Struts-config.xml …
Implementation Struts Framework for well-architectured web applications Model-View-Controller design pattern.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
APACHE STRUTS ASHISH SINGH TOMAR ast2124. OUTLINE Introduction The Model-View-Controller Design Pattern Struts’ implementation of the MVC Pattern Additional.
Apache Struts Technology A MVC Framework for Java Web Applications.
Enterprise Java v050228MVC1 Model, View, Controller Web Architecture.
Struts 2 Development. Topics  Roles in Struts Development  Control Flow  Actions  Struts 2 Views and Target  Struts 2 Custom Tags  Validation 
Unit 6-Chapter 2 Struts.
Introduction to Struts
The Model Layer What is Model?
Struts BY: Tejashri Udavant..
Presentation transcript:

v Web Tier Architecture MVC and Struts

v First, there were servlets And things were good Faster than CGI programs More scalable with built-in threading capability Value-added features –Request/Response, Sessions Full Java API access –JDBC, JMS, EJB, JavaMail, etc.

v Hmm, maybe not so good out.printlns in the code became tedious Takes a programmer to write HTML pages –Skill mismatch No automated tool support Mechanisms developed to output HTML pages –Inclusion of pages –htmlKona –Element Construction Set (ECS) There has to be a better way

v Then there were JSPs Page-centric view of the world Limited programmer involvement JSPs became ubiquitous for dynamic HTML page generation

v Model 1 Architecture Browser JSP Java Bean Database Custom Tags EJB

v Model 1 Architecture JSP-centric Suitable for small systems Susceptible to abuse –Excessive Java code in JSPs –Flow control issues

v Hybrid Approach Use servlets for flow-control; JSPs for HTML pages with dynamic content Allows programmer/web designer specialization Hybrid Approach=Model 2 Architecture A.K.A. MVC –Controller=Servlet+Helper Classes –Model=Application Data/Operations –View=Output Rendered for User

v Model 2 Architecture Browser JSP Java Bean Database Custom Tags EJB Servlet

v Model 2 Benefits Allows programmer to implement flow control, system operations in Java/Servlets Allows web page designers to develop HTML/JSP pages –Automated tool support Higher degree of re-use; more maintainable architecture for larger systems

v Struts ‘Standard’ MVC framework Struts 1.0 released in January 2001 MVC architecture with support for: –Configurable site navigation –Form handling –Internationalization –Extensive Custom tag libraries

v Struts Architecture Browser ActionServlet Action Mappings Action Database Bean View (Servlet/JSP/HTML)

v Application Flow All requests are first processed by the action servlet Refers to ActionMappings to determine action to invoke –Calls Actions you’ve implemented for your application –Should be adapters to the rest of the application Action returns view to forward to View renders data placed in request or session scope by Action class

v Hello Struts Application Create an action to place a ‘HelloBean’ in the request scope Create view that renders the HelloBean Configure ActionServlet to map request to the HelloAction Configure and Deploy

v Hello Action package corej2ee.project.strutsHello; import org.apache.struts.action.*; import javax.servlet.http.*; public class HelloAction extends Action { public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) { req.setAttribute("HelloBean", new HelloBean()); return mapping.findForward("showMessage"); }

v Hello Bean package corej2ee.project.strutsHello; public class HelloBean { String message; public HelloBean() { message="Struts Hello"; } public void setMessage(String m) { message=m; } public String getMessage() { return message; }

v showMessage.jsp <jsp:useBean id="HelloBean" scope="request" class="corej2ee.project.strutsHello.HelloBean" /> Message is:

v Configure Action Servlet <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" " <action path="/hello" type="corej2ee.project.strutsHello.HelloAction">

v Configure web.xml strutsHello action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml debug 2 2 action *.do

v /WEB-INF/struts-bean.tld /WEB-INF/struts-html.tld /WEB-INF/struts-logic.tld /WEB-INF/struts-template.tld

v ANT Build <war warfile="${strutsHello_war}" webxml="corej2ee/project/strutsHello/WEB-INF/web.xml">

v Deployment (From Docs) Copy the file lib/struts.jar from the Struts distribution into the WEB-INF/lib directory of your web application. Copy the all of the files that match lib/struts*.tld from the Struts distribution into the WEB-INF directory of your web application. Modify the WEB-INF/web.xml file for your web application to include a element to define the controller servlet, and a element to establish which request URIs are mapped to this servlet. Use the WEB-INF/web.xml file from the Struts example application for a detailed example of the required syntax

v Deployment (Cont.) Modify the WEB-INF/web.xml file of your web application to include the following tag library declarations: /WEB-INF/struts-bean.tld /WEB-INF/struts-html.tld /WEB-INF/struts-logic.tld /WEB-INF/struts-template.tld

v Deployment (Cont) Create a file WEB-INF/struts-config.xml that defines the action mappings and other characteristics of your specific application. You can use the struts-config.xml file from the Struts example application for a detailed example of the required syntax. At the top of each JSP page that will use the Struts custom tags, add line(s) declaring the Struts custom tag libraries used on this particular page, like this:

v Resulting War File C:\COREJ2EE\DEVROOT\BUILD\LIB>jar tvf strutsHello.war 0 Mon Mar 11 17:45:50 EST 2002 META-INF/ 48 Mon Mar 11 17:45:50 EST 2002 META-INF/MANIFEST.MF 0 Mon Mar 11 17:45:50 EST 2002 WEB-INF/ 0 Mon Mar 11 17:45:50 EST 2002 WEB-INF/lib/ Mon Mar 11 17:45:48 EST 2002 WEB-INF/lib/util.jar Fri Jan 11 16:34:44 EST 2002 WEB-INF/lib/struts.jar 0 Mon Mar 11 17:45:50 EST 2002 WEB-INF/classes/ 0 Mon Mar 11 17:45:50 EST 2002 WEB-INF/classes/corej2ee/ 0 Mon Mar 11 17:45:50 EST 2002 WEB-INF/classes/corej2ee/project/ 0 Mon Mar 11 17:45:50 EST 2002 WEB-INF/classes/corej2ee/project/strutsHello/ 742 Mon Mar 11 17:45:50 EST 2002 WEB-INF/classes/corej2ee/project/strutsHello/HelloAction.class 362 Mon Mar 11 17:45:50 EST 2002 WEB-INF/classes/corej2ee/project/strutsHello/HelloBean.class 500 Mon Mar 11 17:27:16 EST 2002 WEB-INF/struts-config.xml 0 Mon Mar 11 17:36:12 EST 2002 WEB-INF/WEB-INF/ 8101 Fri Jan 11 16:34:44 EST 2002 WEB-INF/WEB-INF/struts-bean.tld Fri Jan 11 16:34:44 EST 2002 WEB-INF/WEB-INF/struts-form.tld Fri Jan 11 16:34:44 EST 2002 WEB-INF/WEB-INF/struts-html.tld Fri Jan 11 16:34:44 EST 2002 WEB-INF/WEB-INF/struts-logic.tld 1637 Fri Jan 11 16:34:44 EST 2002 WEB-INF/WEB-INF/struts-template.tld Fri Jan 11 16:34:44 EST 2002 WEB-INF/WEB-INF/struts.tld 224 Mon Mar 11 17:23:14 EST 2002 showMessage.jsp 1516 Mon Mar 11 17:38:20 EST 2002 WEB-INF/web.xml

v041003

Controller The Controller (ActionServlet) decides ‘what needs to be done’ Uses configuration to determine actions that should be called <action path="/hello" type="corej2ee.project.strutsHello.HelloAction"> We configured all URLs ending in *.do to be first processed by the controller

v Actions Invoked by the action servlet and determines ‘how to get it done’ Typically performs operation (database update, retrieval) and places result information in the request or session Returns ActionForward instance to control next page to forward control to Note: Implementation must be thread-safe

v Action class

v Action Forward Typically use logical name for target page Local targets defined for action <action path="/hello" type="corej2ee.project.strutsHello.HelloAction"> public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) { req.setAttribute("HelloBean", new HelloBean()); return mapping.findForward("showMessage"); } Allows view pages to be renamed with little impact

v Action Forward (Cont) Global forwards for application <global-forwards type="org.apache.struts.action.ActionForward"> <forward name="showMessageGlobal" path="/showMessageGlobal.jsp" redirect="false" /> public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) { req.setAttribute("HelloBean", new HelloBean()); return mapping.findForward("showMessageGlobal"); } Defines forwards that can be accessed by all actions

v View Renders beans placed into session or request by actions We used jsp:useBean in showMessage.jsp Many custom tags are available in Struts to simplify this task –Covered in more detail later

v Form Handling Most web applications use HTML Forms Struts can automatically populate a Java Bean (Form Bean) with data submitted –Similar to jsp:setProperty with an ‘*’ parameter Form Beans are associated with an action –Form bean is updated before action is invoked Form Beans are configured in struts- config.xml

v Form Processing Flow Action Servlet Form Bean Action Results JSP Page 1. submit 2. populate 3. perform 4. use 5. create 6. forward 8. HTML results 7. use

v Document Form Bean package corej2ee.project.strutsHello; import org.apache.struts.action.ActionForm; public class DocumentFormBean extends ActionForm { String documentCategory; String documentContent; public void setDocumentCategory(String cat) { documentCategory=cat; } public String getDocumentCategory() { return documentCategory; } public void setDocumentContent(String content) { documentContent=content; } public String getDocumentContent() { return documentContent; }

v struts-config.xml <form-bean name="DocumentForm" type="corej2ee.project.strutsHello.DocumentFormBean" /> …….. <action path="/submitDocument" type="corej2ee.project.strutsHello.DocumentAction" name="DocumentForm" scope="request" input="/showSubmitDocument.jsp" validate="false" />

v Document Action Gets the populated form bean and sets an attribute so success page can display values public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) { DocumentFormBean dfb=(DocumentFormBean) form; req.setAttribute("message", dfb.toString()); return mapping.findForward("success"); }

v Success.jsp success.jsp Message is:

v showSubmitDocument.jsp Category Content

v041003

Struts custom HTML tags Can simplify JSP pages that present forms Particularly helpful for handling incomplete form submissions –We’ll see this after the Internationalization section Let’s work on showSubmitDocument.jsp…

v Category: Content: Submit Reset

v Internationalization Key Java Classes –java.util.Locale –java.util.ResourceBundle –java.util.PropertyResourceBundle –java.text.MessageFormat Struts classes –org.apache.struts.util.MessageResources –Allows caller to retrieve resource for a specified locale

v Steps Define property files for each supported locale Configure ActionServlet with base name of application’s resource bundle Optionally, configure action servlet to set Locale object based on browser request headers Set locale object in session for user –session.setAttribute(Action.LOCALE_KEY, locale); Display resource strings

v Resource Bundles Must be placed in the web application’s classpath StrutsHello.properties –prompt.hello=hello StrutsHello_fr.properties –prompt.hello=salut

v Request Header Configuration Action Servlet can automatically set Locale object in user’s session based on browser request header Set the servlet initialization parameter ‘locale’ to true –Stored in session at Action.LOCALE_KEY –Only if locale object is not already there

v Configure web.xml action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml debug 2 application StrutsHello 2 Set resources

v Display Resource Strings MessageResources.getMessage(…) Bean Custom tag –

v SetLocaleAction public class SetLocaleAction extends Action { public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) { HttpSession s=req.getSession(true); if("fr".equals(req.getParameter("L"))) { s.putValue(Action.LOCALE_KEY, Locale.FRANCE); } else { s.putValue(Action.LOCALE_KEY, Locale.US); } return mapping.findForward("SubmitDocument"); }

v Basic Page to set Locale I18n message: Category: Content: Submit Reset Set Locale:

v041003

Error Processing Form Bean can implement validate() method –Return ActionErrors if input is not valid –Controller will forward back to the input form that submitted the form –Input Form page redisplays form with errors highlighted Should always do as much validation as possible in the browser with JavaScript –Better performance

v DocumentFormBean public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { if("badCategory".equals(documentCategory)) { ActionErrors ae=new ActionErrors(); ae.add("documentCategory", new ActionError("prompt.badcategory", "")); return ae; } else { return null; }

v prompt.hello=hello prompt.badcategory=You must enter a valid category prompt.hello=salut prompt.badcategory=You must be an American - bad category

v041003

Utilities Connection Pool –Can create a datasource –DataSource ds=servlet.findDataSource() Digester –XML processing utility File Upload Capability

v Struts Summary Well-thought out Model 2 Framework Strengths –Form Handling –Internationalization –Configurable navigation

v Resources uts