Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–

Similar presentations


Presentation on theme: "Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–"— Presentation transcript:

1 Lecturer: Prof. Piero Fraternali, piero.fraternali@polimi.it Teaching Assistant: Alessandro Bozzon, bozzon@elet.polimi.it Advanced Web Technologies: Struts– Exercise Session

2 Summary Deployment Environment Some recap on Java Servlets Web Applications MVC2 Struts Exercises

3 Deployment Environment

4 Required Tools Java JDK 1.5 ~ 1.6 Eclipse IDE for Java EE Developers http://www.eclipse.org/downloads/ Tomcat 5.5 ~ 6.0.X http://tomcat.apache.org/download-55.cgi Struts 1.3 http://struts.apache.org/download.cgi#struts1310 A database (e.g., Postgres) - optional

5 Recap

6 Servlet: analogous to CGI in the Java world  Servlet container: a Java program providing an execution environment for servlets  It provides Web server features as Java objects HTTP Request HTTP Response Browser Servlet container Applications (servlets) JVM parametri risposta Java Servlet

7 A Web application is a collection of servlets, documents and resources A Web application is sub-folder of the ervlet container’s webapps folder The name of the sub-folder is the name of the web applications  http://localhost:8080/APP_NAME The Web.xml file is a deployment descriptor, as it configures the container Web applications It has a standard structure Locations: /web-app-name/WEB-INF/web.xml Libraries are stored under the /WEB-INF/lib folder Compiled Java classes are gathered under the /WEB- INF/classes folder All the resources/files contained in WEB-INF are not accessible from the Web browser Web application

8 Role of the presentation tier in Web based architectures In Web applications, the middle tier is the most complex software component Includes several functionalities Receives requests and send responses back Creates the user interface Implements the business logic and the interactions towards the data level

9 MVC and Web architectures In its classical version, MVC assumes The possibility of keeping track of the state of the application in the model The possibility to refresh the user interface when a change to the model state is notified Conversely, the HTTP protocol is stateless does not enable to automatically update a page displayed by the browser MVC needs to be adapted to the three-tier architecture employed for Web based applications  MVC 2

10 MVC2 for the Web based on Java MVC components are implemented exploting the Web architecture and Java J2EE API The client is a browser, requests and responses are HTTP The controller is a servlet, that processes all the requests The model is a set of Java classes (action and state objects) The view is a JSP page template

11 Struts 1 architecture Struts is an sophisticated implementation of the controller It is independent from the state implementation (state objects) and the page templates The link between the controller and the model is limited to specific Java classes called action classes

12 Controller: components Action servlet (provided by the framework): Base class: org.apache.struts.action.ActionServlet Implements the main functionalities of the controller Struts-config.xml (provided by the developer) XML configuration file of the controller:  Describes what action class shall be invoked for each request type  Specifies what view shall be invoked after an action is executed  Separates the control logic from the controller source code  Enables to modify the control flow by changing the configuration file Request Processor (provided by the framework): Available in Struts 1.1+. It is a helper class that provides methods needed by the ActionServlet

13 Action classes Interface class between the controller and the model In some cases considered as part of the controller, in others part of the model Goals: Hide details about the instantiation and invocation of business services Provide to the controller (actionServlet) a unique interface to be invoked (method execute() in Struts 1.1+, perform() in Struts 1.0) Receive the execution result from the business service and communicate it to the controller in a standard way

14 Communication controller-AC The controller invokes a unique method: public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception An ActionClass, when invoked, receives: The pair HTTP request/response HTTP (AC does not use the response !!) An ActionForm object that contains the user input (if existing) provided by means of a form An ActionMapping that enables communicating to the Controller the result of the ActionClass execution An ActionClass returns an ActionForward object that tells the Controller what to do

15 Controller configuration The controller makes two decisions: What action to be invoked for a given user request What view to be invoked for a given action termination The controller decides what to do based on mappings specified in the struts-config.xml file HTTP requests must be directed to the ActionServlet, defining in the web.xml file the correct servlet mapping

16 Model The Model contains business services needed to execute the application Struts is completely independent from the implementation of such services Model objects perform two functionalities Represent the application state (state objects) Implement the application logic (business objects)

17 State objects The application state can be represente by means of standard interface objects: JavaBeans Enterprise JavaBeans State can be volatile (i.e. Shopping cart) or durable (i.e.: received orders) Durability of the state requires a data-tier including one or more databases Database access can be implemented with different Java2EE technologies: JDBC Entity Enterprise Java Beans Java Data Objects

18 View Typically the View is composed by one or more JSP page templates that produce the pages displayed by the client browser Page templates publish the content of the model state objects The best practice to organize page templates is to use tag libraries Most used tag libraries Struts Tag Library Java Standard Tag Library (JSTL) Whenever possible, it is better to use Java standard library: JSTL

19 Typical components of a tag library JSP tag libraries contain four types of elements: Bean/Hierarchy tag: to access the content of JavaBeans or JavaBean hierarchies (i.e.: lists of lists) HTML tag: to increase the flexibility of HTML tags (i.e.: forms with memory and error checking) Logic tag: to avoid the use of scripting to express conditions (=, !=, strings comparison,..) and iterations Template tag: to dynamically fetch blocks of content and include them in the page

20 User’s input management Issue: in HTML/HTTP there is an element dedicated to data entry (form), but: User’s input is included in the HTTP request and must be extracted and processed by means of ad hoc code There is no way to express input validation in a standard way and enforce error checking There is no easy way to recall previous input when a form is refreshed (i.e. back button) Struts enables an advanced input management that eases the creation of applications

21 Struts input management principles Struts offers two functions to handle user input Objectification services: data inserted by the user are used to populate a Java object (called FormBean) Validation services: there is a framework to organize functions devoted to checking the correctness of input values and handle errors NOTE: In general, in MVC2 validation can be performed in three ways, not mutually exclusive:  At the client side (e.g., using JavaScript/FLASH/RIA…)  Using specific action classes (FormAction)  Using business services

22 Exercises


Download ppt "Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–"

Similar presentations


Ads by Google