Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 v041003 Web Tier Architecture MVC and Struts

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

3 v041003 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

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

5 v041003 Model 1 Architecture Browser JSP Java Bean Database Custom Tags EJB

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

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

8 v041003 Model 2 Architecture Browser JSP Java Bean Database Custom Tags EJB Servlet 1 2 3 4 5 6

9 v041003 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

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

11 v041003 Struts Architecture Browser ActionServlet Action Mappings Action Database Bean View (Servlet/JSP/HTML) 1 2 3 4 5 6 7 8

12 v041003 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

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

14 v041003 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"); }

15 v041003 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; }

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

17 v041003 Configure Action Servlet <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> <action path="/hello" type="corej2ee.project.strutsHello.HelloAction">

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

19 v041003 /WEB-INF/struts-bean.tld /WEB-INF/struts-html.tld /WEB-INF/struts-logic.tld /WEB-INF/struts-template.tld

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

21 v041003 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

22 v041003 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

23 v041003 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:

24 v041003 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/ 28410 Mon Mar 11 17:45:48 EST 2002 WEB-INF/lib/util.jar 330440 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 38075 Fri Jan 11 16:34:44 EST 2002 WEB-INF/WEB-INF/struts-form.tld 48610 Fri Jan 11 16:34:44 EST 2002 WEB-INF/WEB-INF/struts-html.tld 12639 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 53043 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

25 v041003

26 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

27 v041003 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

28 v041003 Action class

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

30 v041003 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

31 v041003 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

32 v041003 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

33 v041003 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

34 v041003 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; }

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

36 v041003 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"); }

37 v041003 Success.jsp success.jsp Message is:

38 v041003 showSubmitDocument.jsp Category Content

39 v041003

40

41 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…

42 v041003 Category: Content: Submit Reset

43 v041003 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

44 v041003 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

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

46 v041003 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

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

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

49 v041003 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"); }

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

51 v041003

52

53 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

54 v041003 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; }

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

56 v041003

57

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

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

60 v041003 Resources http://jakarta.apache.org/struts/ http://jguru.com/faq/Struts http://jguru.com/forums/home.jsp?topic=Str uts


Download ppt "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."

Similar presentations


Ads by Google