Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Advanced Web Technologies: Struts– Exercise Session Lecturer: Prof. Piero Fraternali, [email protected] Teaching Assistant: Alessandro Bozzon, [email protected] Summary Deployment Environment Some recap on Java Servlets Web Applications MVC2 Struts Exercises Deployment Environment 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 Recap Java Servlet HTTP Request Browser HTTP Response JVM parametri Servlet container risposta Applications (servlets) 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 Web application 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 /WEBINF/classes folder All the resources/files contained in WEB-INF are not accessible from the Web browser Role of the presentation tier in Web based architectures SQL request d i gi t a l HTTP request database SQL response Page templates Business logic 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 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 MVC2 for the Web based on Java Servlet container Controller (servlet) request HTTP Client (Browser) web server response HTTP Model invokes Actions creates invoca View View (page (page template) template) reads State objects MVC components are implemented exploting the Web architecture and Java J2EE API The The The The client is a browser, requests and responses are HTTP controller is a servlet, that processes all the requests model is a set of Java classes (action and state objects) view is a JSP page template Struts 1 architecture Servlet container Struts controller Action servlet struts-config.xml Model perform() Action classes Request processor process() View redirect/forward (page template) View reads (page template) creates State objects 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 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 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 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 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 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) 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 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 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 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 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 Exercises