Upload
horace-hardy
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
Lecturer: Prof. Piero Fraternali, [email protected] Assistant: Alessandro Bozzon, [email protected]
Advanced Web Technologies:
Struts– Exercise Session
Required Tools
Java JDK 1.5 ~ 1.6Eclipse 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
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
HTTPRequest
HTTPResponse
Browser
Servlet container
Applications(servlets)
JVMparametri
risposta
Java Servlet
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 folderCompiled 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
Role of the presentation tier in Web based architectures
In Web applications, the middle tier is the most complex software componentIncludes several functionalities Receives requests and send responses back Creates the user interface Implements the business logic and the interactions towards the data level
database
latigid
SQL request
SQL response
HTTP request
Page templates
Business logic
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
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
View(page template)
responseHTTP
requestHTTP
webserver
Servlet container
Controller(servlet)
Model
View(page template)
Actions
State objects
Client(Browser)
invokes
createsinvoca
reads
Struts 1 architecture
Struts is an sophisticated implementation of the controllerIt is independent from the state implementation (state objects) and the page templatesThe link between the controller and the model is limited to specific Java classes called action classes
View(page template)
Servlet container
Struts controller
Model
View(page template)
Actionclasses
State objects
perform()
creates
redirect/forward
reads
Action servlet
Requestprocessor
struts-config.xml process()
Controller: componentsAction 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 classesInterface class between the controller and the modelIn some cases considered as part of the controller, in others part of the modelGoals: 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 fileHTTP requests must be directed to the ActionServlet, defining in the web.xml file the correct servlet mapping
ModelThe Model contains business services needed to execute the applicationStruts is completely independent from the implementation of such servicesModel 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 databasesDatabase 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 browserPage templates publish the content of the model state objectsThe best practice to organize page templates is to use tag librariesMost 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