内容摘要: This paper describes implementation of web -based hotel reservation system whichenables users tobook hotel rooms by means of a web brows...
This paper describes implementation of web -based hotel reservation system whichenables users tobook hotel rooms by means of a web browser. The systemis based on JavaServer Faces technology in thepresentation layer, Spring Framework in the service layerand iBatis library for the data access layer.
I. INTRODUCTION
The Hotel Reservation System is a specific type of softwarethat supports, often by means of a web browser, wholeinfrastructure of the hotel for which the system has beendeployed. Its key features are listed below:To search and to book hotel rooms.Tonotify of every event related to a user by sending the email on a given address during the registration process.
To support definition process of each component of the hotel. The components of the hotel are: rooms,promotions, packages, discounts and se rvices.To complete the reservation process by notifying the system about the fact that the guest came to the hotel and received keys to the booked room.
II. JAVASERVER FACES
The user graphical interface is a crucial part of every computer programme with regard to its responsibility for interaction with the users. This part of application should be flexible,which means susceptible to any needed changes and without any doubts, user – friendly. Regrettably, process of developing such user interface in the main requires usually a profusion of developer's work and time, making the whole development process involved and onerous.The most apting technology to this field is JavaServer Faces (JSF). This holistic, component-based solution for longstanding problems facing software developers has a specific goal: to make web development faster and easier by masking a lot of complexities hidden behind this kind of programming.JSF technology is a server-side user interface framework for Java technology-based web applications. The main components of JSF technology are as follows:
Two JavaServer Pages custom tags library for expressing UI components within a JSP page and for wiring components to server-side objects.The user interface created with JSF technology (represented by myUI on the Fig. 1) runs on the server and renders back to the client (in this case the browser).Because web-based applications, must often appease multiple clients (such as desktop browsers, cell phones, and PDAs),JSF has a powerful architecture for displaying components in different ways. As for the browsers, for which JSF is mostly used, the default rendering technology is HTML 4.0, though the others like WML or SVG are also possible.One of the greatest advantages of JSF technology is that it offers a clean separation between behavior and presentation layers.
III. SPRING FRAMEWORK
Spring is a lightweight, inversion of control container, created to address the complexity of enterprise application development.Spring makes possible to use plain JavaBeans to achieve things that were previously only possible with EJBs [10,11].Any Java application can benefit from Spring in terms of simplicity, test-ability, and loose coupling. The Spring Framework contains a lot of features, which are well-organized in seven modules.
When taken as a whole, these modules give everything that is needed to develop enterprise applications, although there is no must to build the application fully on the Spring framework. One can pick and choose only those modules that suit best to the application and ignore the rest. This strategy was chosen for the Hotel Reservation System Since one of the core value propositions of the framework is that of enabling choice by not forcing one to apply any particular architecture, technology or methodology, Spring also provides convenient mechanisms for integration with number of third -party frameworks (also with JSF and iBatis).
IV. APACHE IBATIS DATAMAPPER
Apache iBatis JDBC – based Data Mapper provides simple and flexible way to transport data between relational database and Java, .Net or Ruby application. Contrary to popular Hibernate library, iBatis does not directly tie classes to tables or fields to columns, but instead, it couples objects with results of stored procedures or SQL statements using simple XML descriptor letting the developer to reach only for required data. The iBatis framework can map nearly any database to any object model and is very tolerant of legacy designs, or even bad designs.Simplicity is the main advantage of the iBatis over other frameworks.
The Hotel Reservation System was primarily built upon JSF technology for the user interface along with auxiliary libraries –Spring and iBatis. The application architecture is presented in Fig. 3.The composition of the system is modular. Each layer is separated with and appropriate interface layer. Such solution makes the architecture not only more error -resistant but also more flexible for changes. The presentation layer consists of the graphical user interface written in the JSF technology andunderlying backing beans objects. It communicates with the Service layer through the service interfaces.Service layer is the central business part of the system where, on the basis of the user input data, proper decisions are made. It also translates input data into explicit form understood for the Data access layer, with which communication takes place by means of Data access interface layer.The Data access layer cooperates with Data Mapper layer and is responsible for data exchange with database.The Data Mapper layer talks via JDBC to database to ask required data. This is a layer where iBatis Data Mapper plays the main role.
V. JSF - SPRING INTEGRATION
In a nutshell, JSF – Spring integration makes Spring managed beans visible as variables to JSF, just as if the Spring beans are configured as JSF managed beans.Before diving into the integration specifics of JavaServer Faces,the general integration step for any web framework must be set forth.All that need to be done, is to declare ContextLoaderListener in the standard JEE servlet web.xml deployment descriptor. This listener initializes the Spring's WebApplicationContext that contains all of the business beans in the application. (Fig. 4).The second step in the integration between Spring and JSF is specific just for JSF. The key class used in this process isDelegatingVariableResolver. To configure this variable the faces-context.xml file must be edited. After opening <faces -config> tag <application> tag must be added and within it the <variable-resolver> pointing to the Spring's DelegatingVariable Resolver must be put. The example faces -config.xml file is presented in. The DelegatingVariableResolver delegates value lookups to the default resolver of the underlying JSF implementation, and then to Spring's business context WebApplicationContext. This allows to easily inject dependencies into one's JSF-managed beans.
Example of faces-config.xml file from Hotel Reservation System application
VI. JSF AND GETMETHOD.
The JSF only recommended method for sending input data from an online form to an application server is the POST method,though sometimes there is a strong need for use the GET method instead. In the Hotel Reservati on System the GET method is used during the registration process for bringing about user account activation.The common way to implement the GET method is to create a special filter which can manipulate the header or contents (or both) of a request or response.To put GET method into operation, in case of JavaServer Faces,the following algorithm must be fulfilled in the body of thefilter:
1. Get parameters from the request.
2. Create appropriate backing bean object accordingly to the
view that will be used further.
3. Store the backing bean object in the current session.
4. Set changed JSF context as current.
5. Create view and store it in the JSF context.
6. Execute backing beans methods.
The Hotel Reservation System was also based on the Spring library, hence some changes to the raw algorithm were necessary.The foremost method for any filter is doFilter() where the proper filtration happens .
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)throws IOException,
ServletException {
FacesContext facesContext =
getFacesContext(request, response);
if (facesContext != null){
request.getRequestDispatcher(
facesContext.getViewRoot().getViewId()).
forward(request, response);
}else{
chain.doFilter(request, response);
}
}
The doFilter method from GET filter implementation As it may be seen on Fig. 6, an additional function implementing the presented algorithm was used to keep the source code clear and easy-to-read.Create appropriate backing bean object.Almost all backing beans from Hotel Reservation System have at least one property of Spring managed -bean origin. Because of that, it is necessary to set this property after backing bean object creation.
WebApplicationContext wac =WebApplicationContextUtils.
getRequiredWebApplicationContext
(servletContext);
RegisterService registerService =
(RegisterService)
wac.getBean("registerService");
ConfirmBean confirmBean = new ConfirmBean();
confirmBean.
setRegisterService(registerService);
Creating backing bean object and setting spring –origin property.Getting Spring managed -bean object requires operating on Spring's WebApplicationContext object that is initialised during the server start-up.Set changed JSFes context as current.When storing backing bean object in the session the whole Faces context becomes changed, therefore it must be set as current in order all modifications take place .
FacesContextFactory contextFactory =(FacesContextFactory)
FactoryFinder.getFactory(
FactoryFinder.FACES_CONTEXT_FACTORY);
LifecycleFactory lifecycleFactory =(LifecycleFactory)FactoryFinder.
getFactory(FactoryFinder.
LIFECYCLE_FACTORY);
Lifecycle lifecycle =lifecycleFactory.
getLifecycle(LifecycleFactory.
DEFAULT_LIFECYCLE);
facesContext = contextFactory.
getFacesContext (servletContext, req,res, lifecycle);
ProtectedFacesContext.
setFacesContextAsCurrentInstance
(facesContext);
Setting JSF context as current Before the actual setting context into current, it is indispensable to grab Lifecycle object. Lifecycle manages the processing of the entire lifecycle of a particular JSF request. It is responsible for executing all of the phases that have been defined by the JSF specification, in the specified order, unless otherwise directed by activities that occurred during the execution of each phase. An instance of Lifecycle is created by calling the getLifecycle() method of LifecycleFactory, for a specified lifecycle identifier. Because this instance is shared across multiple simultaneous requests, it must be implemented in a thread -safe manner. Using the current Lifecycle object along with ServletContext,HttpServletReqest and HttpServletResponse objects the FacesContext can be updated and becomes up to date.
Create view and store it in the JSF context.After making Faces context actual, the new view is supposed to be created and added to this context, nevertheless it is n ot possible to do it with usage of 'new' operator, instead it has to be done in an indirect manner.
UIViewRoot view = facesContext.
getApplication().
getViewHandler().
createView(facesContext,
"/confirm/register.jsp" );
facesContext.setViewRoot(view);
Creating new view and stroing it in Faces context The createView() method of ViewHandler object constructs and return a new UIViewRoot instance initialized with information from the argument FacesContext and viewId . The viewed parameter is always String variable representing (relative to /WEB-INF/ directory) path where the definition file is located.As shown in Fig. 9 the definition of the view is placed in ordinary JSP file and is located in subdirectory of a WEB-INF directory.Executing backing beans methods The last step is the actual execution of methods of the underlying, proper to created view backing bean object. This example of the filter for the GET method was based upon one used in the Hotel Reservation System, which works as a registration confirmation tool, so in the end, the method responsible for confirmation should be invoked:confirmBean.confirmRegistration(userId);
VII. CONCLUSIONS
The Hotel Reservation System is developed application designed accordingly to the modern programming trends with usage of technologies like JavaServer Faces, Spring Framework and iBatis Data Mapper. Its modular architecture makes the application more error-resistant and flexible for any changes therefore easy to add new functionality. The common problem of sending input data through the GET method has also been solved.System is easy to install and use and the three –module composition puts work-division pattern into operation.This research was supported by the Technical University of Lodz Grant K-25/1/2007/Dz.St.