Translate

Wednesday 17 April 2013

Integrating Eclipse Dynamic Web Project with Struts 2 and Maven and DWR



Note: To see code snippet in this post, use either Google Chrome or Mozilla Firefox.
I have walked through all google results (figuratively) to find a way to integrate Struts2 with DWR and make integration for maven with Dynamic Web Project. Because most of the tutorial will shows you how to create Struts2 or DWR project with either maven or dynamic web project. Why not using both? So, my goal here is to create a dynamic web project with maven to manage its library dependency and making an orchestra between struts2 framework and DWR to work together.

Here my friend take the guide to set up a dynamic web project in eclipse and use maven for dependency management. You will learn how to create a struts 2 project and also integrate direct web remoting  (dwr) library in it. I assume you already have following setup in your environment:
  1. Eclipse IDE 3.7 (Indigo) because I avoid using Juno since it has some significant bugs
  2. M2E plugin installed in your Eclipse 3.7
  3. DWR 3.0 RC library (I avoid using one provided from maven repository due to some error)
  4. JDK 1.7 (I am using JDK 7)
  5. Tomcat 7
Create New Dynamic Web Project
  1. Click and Select on File > New > Dynamic Web Project
  2. Put Project Name TestWeb and Click Next. Then Change the default build output folder into WEB-INF
  3. Click Next then check the option Generate Web.xml and click finish


Creating required folder for Maven
Before converting to Maven Project, we need to prepare what is missing for standard maven directory structure. Because Maven will have standard directory as:
  • src/main/java : all your java code here
  • src/main/webapp : all your web files here including WEB-INF folder
  • src/main/resources : this is optional, but lets create this folder to store struts properties and xml configuration
Take a look at our folder structure created from Eclipse Dynamic Web project. We will remove the WebContent folder and create the folder above in the existing src folder. 

The picture is original web folder and some instruction which folder to remove and what folders to create on which folder. To create new folder, simply click on the TestWeb root folder, and select New > Folder. Create the main folder first, and then under that main folder, you create java, resources, and webapp folder. 

So the final result of your folder structure will be like this:

Converting to Maven Project
After the maven's required folder is created, convert the project by right-clicking on the TestWeb root project folder, select Configure > Convert to Maven Project. On the window dialog Create New POM don't forget to select WAR as packaging method.


The package Name can be whatever you would like to prefer, so it is not mandatory to follow the naming with com.aryabisma prefix. Click Finish and the final result must be like this with errors:


Modifying Project Properties setting 
Next we need to modify some project properties setting to remove errors and make the project works. 
Right-Click on TestWeb root project folder then select Properties
  1. Change Project Facets Java to 1.7, then apply
  2. Change the Deployment Assembly with the following setup

  3. Change Java Compiler with JDK Compliance level 1.7, don't forget to uncheck the compliance first.
  4. Create folder WEB-INF/lib in src/main/webapp so that it become src/main/webapp/WEB-INF/lib and put DWR library there.
  5. After that, open project properties again and select Java Build Path, then open tab Library. select Add Jars then navigate to the dwr.jar we put on src/main/webapp/WEB-INF/lib. Hit Ok
  6. Remove the resources folder in the source tab under Java Build Path properties in Project Properties. So the result will be like this.
  7. I almost forgot to tell you that we need to change the JRE System Library int jdk 7, the one that we use for eclipse. So in the Project Property dialog, open tab Libraries, remove the JRE system library (1.5), then click on Add Library, select Jre System Library, and  select Workspace defaule JRE (1.7) 
Add Struts 2 and DWR Dependency
We already have the initial setup for Dynamic Web Project with Maven dependency management, now we add struts library and its dependencies. Put these code in your pom.xml 
pom.xml

Create Configuration File for Web deployment, and DWR
Now create a dwr.xml on the src/main/webapp/WEB-INF folder. Put these line in that file.

dwr.xml

Next, create web.xml on the same location with dwr.xml. Copy these line into that file.

web.xml



Now your webapp structure should be something like this.


Create Java class and jsp for DWR 
Create package com.aryabisma.asyncjava and make AsyncProcessor.java file with these line

AsyncProcessor.java

If you modify the package name and the java class name, you should modify the dwr.xml creator configuration to match with your new naming. Now Create the index.jsp file on src/main/webapp folder to make a call to method in AsyncProcessor.java as defined to be AsyncProcessor javascript in dwr.xml. Put these line in that index.jsp.

index.jsp

Create Java class and jsp for Struts2
Create package com.aryabisma.struts2 and make LoginAction.java file with these line.

ActionLogin.java

Create Login.jsp file with these line to make a form with action that mapped to ActionLogin.java controller

Login.jsp

Create Welcome.jsp for result page after the login action.

Welcome.jsp

Create ApplicationResources.propertiesunder src/main/resources folder. This file hold all the labeling and message text used in Login.jsp and ActionLogin.java, so that you have centralized place to manage all the labeling and message text.

ApplicationResources.properties

Create struts.xml to save mapping of action along with its success and error result.

struts.xml

Final Project Structures and Deploying to Tomcat Server
Now, your final project structures should be similar like this at least.



It's time to build the project and deploy it into Tomcat Server. To build the project, right click on the TestWeb root folder, then select Run As > Maven Install. you can deploy it manually to tomcat server or select Run As > Run on Server in eclipse. The final result must be similar to this demo page.

FYI: I still have problem with the integration due to struts filtering. So that, whenever you enable DWR debug as true, and try to see the exposed methods at /dwr/ web path, you will hit 404 no action mapped. But, if you remove the struts2 filter it will works and you can see the exposed dwr method.

No comments:

Post a Comment