You are on page 1of 56

Developing & Deploying J2EE Application using

GWT Spring and Hibernate

Version 0.1

Date Author Comments Reviewed By


04/28/2011 Alka/Sapna Initial document preparation
04/28/2011 Alka Sharma Updated the document with
Setting up the environment
and setting up GWT module
and folder structure
04/28/2011 Sapna Updated the document with
deploying the application into
web logic

1
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

Table of Contents
1. Introduction 3
2. Setting up the environment 3
2.1 GWT 3
2.2 Spring 3.x 3
2.3 Hibernate 3.x 3
2.4 Eclipse Galileo 3.5 3
2.5 Application Server 4
3. Setting up GWT module and folder structure 4
3.1 Setting up the GWT 4
3.2 For integrated application (GWT + spring + Hibernate) 8
3.3 Writing UI code, Service (RPC mechanism) and its implementation 15
3.4 Business Logic involves integration of Hibernate with Spring 21
4. Deploying the .WAR file to the Web-logic 28
4.1 Create a User defined web-logic configuration 28
4.2 Setting up the User-defined Admin Console and server as per the requirement 35
4.3 Defining JNDI 37
4.4 Setting the logging in web-logic (log 4j) 49
4.5 Deploying the WAR file into the server 51

2
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

1. Introduction
This document explains the procedure to develop and deploy a J2EE web application which adheres
to the AJAX (GWT), spring and Hibernate Framework.
Advantages of using GWT, spring and Hibernate Framework
 Eradicates  performance issue
 Good UI presentation
 Code maintainability and less lines of codes to get functionality done.
 GUI and Server code can be written separately.
 MVC architecture.
 Efficient logging

2. Setting up the environment

2.1 GWT
 Link to download GWT plug-in for Eclipse Galileo 3.5
 GWT Plug-in - http://dl.google.com/eclipse/plugin/3.5
 Google Web Toolkit SDK 2.0.3
 Google App Engine Java SDK 1.4.2
 Link to download GWT – ext
 http://extjs.com/deploy/ext-2.0.2.zip
 http://code.google.com/webtoolkit/download.html (official site from Google)
2.2 Spring 3.x
 Link to download Spring 3.x
 http://www.springsource.org/download
2.3 Hibernate 3.x
 Link to download Hibernate 3.x
 http://olex.openlogic.com/packages/hibernate
 http://www.hibernate.org/
2.4 Eclipse Galileo 3.5
 Link to download Eclipse Galileo 3.5

3
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

Eclipse version - Galileo 3.5 (Eclipse Java EE IDE for Web Developers)
 http://www.eclipse.org/downloads/
2.5 Application Server
 Weblogic 10.0

3. Setting up GWT module and folder structure

3.1 Setting up the GWT

After installing GWT plug-in one will have following icon in Eclipse

Click on highlighted icon .It opens web application set up wizard

4
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

Enter project Name and package


For instance Project Name demopro and com.bt.demo
Folder structure created in eclipse workspace looks like

5
Developing & Deploying J2EE Application using
Contains images, data,
GWT Spring and Hibernate js and GWT module
xml to start
application locally.

Contains client code


such as GUI Pojo
class, GUI constants
class, GUI utility
class

Contains JUnit/test
classes for GWT service
and impl

Java System
Library (5
and above)

Contains compiled classes (src) and


WEB-INF. Here complied classes
means translation of GWT code into
lightweight Javascript.

Contains all jars


required by
application

Having GWT -ext in application


Make folder inside com.bt.demo public jsext
Copy highlighted folder and files form ext-2.0.2 inside ext folder.
In public folder, one can create img, data folder to have customized image other than provided
by GWT
Data folder is required to keep stubbed response xml received from server.

6
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

Copy highlighted folders and


files to public/js/ext
Note:-public, js, ext folder
need to be created before
copying

7
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

3.2 For integrated application (GWT + spring +


Hibernate)

8
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

Hibernate
resources
files

ApplicationContext.xml
contains Hibernate
configuration for local
environment; weblogic
environment and Bean
definition.

9
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

10
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

Can be set as
welcome page and
loading message

WAR to
deploy

Build.xml to build
war for deployment 11
on weblogic 10.0
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

GWT Module xml


Developing GWT Module xml – start of Application

Give Application main/entry


point class name (located in
client package)

Above xml, gets created using New Web Application Project


In this need to inherit module getting used in application like gwt-user.jar (available in xml -default)

How to inherit GWTExt?


 gwtext.jar should be in classpath of project, expanding jar in eclipse.

12
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

So in inherit name
would be
com.gwtext.GwtExt

Therefore to inherit this module, need to write as

Developing welcome html page for application

BRASInterface.html BRASInterface.css

Above html and css, gets created using New Web Application Project
Modifications done by developer are:

Instead of below default code

13
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

Can be replacing by loading message, loading image, title can be customized according to app name

14
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

3.3 Writing UI code, Service (RPC mechanism) and its


implementation
 Requirement:
 To fetch records from Oracle Database, pass the response to GUI, read it and display
on GUI.
 Technology used:
 GWT RPC mechanism
 Spring – application context, wiring and bean factory
 Hibernate – for table mapping and querying database

For GWT RPC service, serviceAysnc and serviceImpl are required.

 Naming convention
Service is GenerateBRASHistoryService so append Async to get GenerateBRASHistoryServiceAsync and
its implementation would be GenerateBRASHistoryServiceImpl, append impl after service.

 Method convention
Need to write logic for fetching records from database for instance consider this method:

Signature for above method in Service class

Signature for above method in ServiceAsync.class

 Why such naming convention?


Service class will have method to be implemented and specific return type, one wants to return to GUI.
ServiceAsync class will always have return type void and have same signature, it will add AsyncCallback
ServiceImpl class will implement above method and return type would be as expected by Service class

15
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

GenerateBRASHistoryService.java (located in shared. service package)

GenerateBRASHistoryServiceAsync.java (located in shared. service package)

16
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

GenerateBRASHistoryServiceImpl.java (located in server. service)

17
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

How Service, ServiceAsync and ServiceImpl are tied together with web.xml of application and Spring?
 Integration has been done by using spring4gwt-0.0.1 jar
 Servlet name as indicated in web.xml is mapped to services and html is set as welcome file for
app.

18
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

And these URL pattern has mapped to Service class using Spring annotation
@RemoteServiceRelativePath as shown in below code

Flow control goes to ServiceImpl class using spring annotation @Service

Flow goes to business logic implementation and completing processing it returns to ServiceSync class
which gets called in GUI as below:

where AsyncCallback is anonymous class which would handle failure and success of incoming request

19
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

20
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

3.4 Business Logic involves integration of Hibernate


with Spring
In applicationContext database and hibernate config as follows(local environment)

Datasource and Hibernate


configuration for local settings

Give location of
Hibernate resources

In applicationContext database and hibernate cofig as follows (weblogic environment)

21
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

Datasource and Hibernate


configuration for weblogic settings

Give location of
Hibernate resources

At weblogic 10.0 JNDI created

datasource bean is referenced in sessionFactory bean block and this sessionFactory gets called in
serviceImpl class using Spring annotation @Autowired

And applicationContext gets called from web.xml

22
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

DAO provides sessionFactory to serviceImpl class using Spring annotation @Repositry, @Autowired

Now remaining with fetching of records using Hibernate mapping and DTO.
DTO is pojo class and mapping file does ORM – Object Relation Mapping with pojo class and tables in
Oracle Database.DTO gets used in serviceImpl class to write query as Java object.

23
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

One of Hibernate mapping xml located resources/Hibernate source folder

24
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

25
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Response at GUI looks like

Paging in GWT
(feature)
Obtaining application context in code (feature of Spring)
Using ApplicationContextProvider Class and bean definition in applicationContext

26
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

And in code get the appContext using ApplicationContextProvider.getApplicationContext();

 Injecting HttpServletRequest in ServiceImpl class using Spring (feature of Spring )


For this web.xml contains

and serviceImpl class one can have request using @autowired

 Debugging by using log4j


Keep Log4j.xml in source folder of project and have

27
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

4. Deploying the .WAR file to the Web-logic

4.1 Create a User defined web-logic configuration


We create a user defined web-logic configuration, so that we could have our requirement
specific configurations.

 Go to Start -> All Programs -> BEA -> Tools -> Configuration Wizard

28
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Click on Configuration Wizard

29
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Click on Next and then select “Generate a domain …”

30
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Enter the user-defined username/password

31
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Select the option “Sun SDK 1.5.0_11…”

Note: We could also use any version of JDK.

32
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Select “NO”

33
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Enter the name and location for the domain and then click on “Create”

34
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

4.2 Setting up the User-defined Admin Console and


server as per the requirement

 Start the Admin Server

35
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Once the server is in RUNNING MODE, open the Admin server Console and enter your
credentials

36
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

37
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

4.3 Defining JNDI


 Click on Services -> JDBC -> Data Sources
 Click on Lock & Edit button on upper left hand corner ( to enable the button)

 Click on New Button

38
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Enter the details like


 Name ( JDBC DataSource Name)
 JNDI Name ( this name would be referred in ApplicationContext.xml)
 Database Type
 Database driver

& click on Next

39
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Choose the transactional options & click on Next

40
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Enter the following details:

 Database Name
 Host Name
 Port
 Database user name
 Password

41
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Check the details enter and click on Test Configuration button

 Note: In the Test Table Name column, enter a valid SQL statement prefixed with
“SQL”

On success

42
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Choose the target server and click on Finish

 On receiving the confirmation message, click on “Activate Changes” button.

43
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 The changes would be activated and specified if restart of the server is required or not.

44
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 To monitor, if the Data source is working, click on Data Source Name and select the
Monitoring tab

45
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Click on the Monitoring tab and then select the Testing sub-tab. Select the Admin server
and then click on Test Data Source button

We get the above mentioned error. To avoid the above error, follow the below steps

46
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Click on Lock and Edit


 Select the Configuration tab.
 Click on the Advanced icon
 Check the “Test Connections on Reserve”
 Click on Save button
 Activate the changes

47
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Again do the steps involved in “ix”, we get the confirmation message

48
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

4.4 Setting the logging in web-logic (log 4j)


 In the left-hand corner, up –> click on the View changes and restart hyperlink
 Select the Restart Checklist tab
 Click on the Server name
 In the Configuration tab, select the Server start sub-tab

In Class-path column – add the entries of jars


 antlr-2.7.6.jar;
 D:\bea\user_projects\BRASInterface_domain\BRASInterface\lib\log4j-
1.2.15.jar;
 D:\bea\user_projects\BRASInterface_domain\BRASInterface\lib\wllog4j.jar;

Note: These jars should placed in the user defined project lib directory

In the Arguments section, add the below entry


 -Dweblogic.log.Log4jLoggingEnabled=true

49
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Click on Save button. (This action will require a restart of server)

 In the main tab of Server, select the logging tab


 Click on the Advanced icon
 Select the logging implementation as Log4j

Click on Save button. (This action will require a restart of server)

50
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

4.5 Deploying the WAR file into the server

 Click on the Deployments link with in the Domain Structure

51
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 On Click of the Install button in the screen, select the location of the WAR file & click on Next

52
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Choose targeting style ( since we are deploying a web-application, we choose the first option) &
click on Next

53
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Click on Finish

 Click on Activate changes.

54
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 Now, the application is in “Prepared” state


 Select the Application, and click on the Start drop-down, there select the “Servicing all requests”
option.

 The application is now in Active state and able to access

55
Developing & Deploying J2EE Application using
GWT Spring and Hibernate

 To test the application


 Select the application
 Click on Testing tab
 Click on the URL available – The application would open up in a browser.

56

You might also like