Friday 24 August 2012

Struts 2 – Weblogic 10.3.4 tutorial.


Struts 2 – Weblogic 10.3.4 tutorial.

Prerequisites:
1 ) JDK 6
2) eclipse
3) weblogic server 10.3.4 or later
4) Ant 6

1)     Download latest struts 2 stable release (http://struts.apache.org/).
2)     Extract struts library. Lets refer to the location where it was extracted as STRUTS_HOME.
3)     Create a java project in eclipse call it whatever you want. Will refer to it here as strutsApp. 








4)     Now we need to create a build file that will assemble an application into a war file. Here is the build file. I copied it and modified from one of my projects.


<?xml version="1.0" encoding="UTF-8"?>
<!-- ======================================================================
     Aug 21, 2011 11:58:33 AM                                                        

     This is our struts project.
                  
     iakoupov                                                               
     ====================================================================== -->
<project name="strutsApp" default="war" basedir=".">
    <description>
            This is the struts project.
    </description>
      
       <property name="wl.autodeploy.dir" value="C:\oracle\Middleware\user_projects\domains\strutsApp\autodeploy"/>
       <property name="strutsApp.war.name" value="strutsApp.war"/>
       <property name="strutsApp.ejb.name" value="strutsAppEJB.jar"/>


       <path id="war.classpath">
              <fileset dir="./WEB-INF/lib">
                     <include name="**/*.jar"/>
              </fileset>
             
              <fileset dir="C:\oracle\Middleware\wlserver_10.3\server\lib">
                     <include name="weblogic.jar"/>
              </fileset>
       </path>
      
       <path id="ejb.classpath">
                    
              <fileset dir="C:\oracle\Middleware\user_projects\domains\strutsApp\lib">
                     <include name="**/*.jar"/>
              </fileset>
      
              <fileset dir="C:\oracle\Middleware\wlserver_10.3\server\lib">
                     <include name="**/*.jar"/>
              </fileset>
             
              <fileset dir="C:\oracle\Middleware\modules">
                     <include name="javax.ejb_3.0.1.jar"/>
                     <include name="javax.jms_1.1.1.jar"/>
              </fileset>
       </path>

    <!-- =================================
          target: war             
         ================================= -->
    <target name="war" depends="" description="This is a struts project.">
      
      
       <antcall target="mkdirs"/>
        <antcall target="compileWar"/>
      
       <war destfile="${wl.autodeploy.dir}\${strutsApp.war.name}"
              webxml="./WEB-INF/web.xml">
              <classes dir="./classes"/>
              <lib dir="./WEB-INF/lib"/>
       </war>
      
      
    </target>
      
       <!-- =================================
                 target: EJB             
                ================================= -->
           <target name="ejb" depends="" description="">
             
             
              <antcall target="mkdirs"/>
               <antcall target="compileEJBs"/>
             
              <jar
                     destfile="${wl.autodeploy.dir}\${strutsApp.ejb.name}"
                     basedir="./classes">
                    
              </jar>
             
           </target>
      
       <target name="mkdirs" description="Creates required directories for compiation">
              <delete dir="./compile"/>
       <delete dir="./classes"/>
       <mkdir dir="./compile"/>
       <mkdir dir="./classes"/>
       </target>
      
       <!-- - - - - - - - - - - - - - - - - -
                 target: compileEJBs                     
                - - - - - - - - - - - - - - - - - -->
           <target name="compileEJBs">
              <javac srcdir="./src"
                     destdir="./classes"
                     classpathref="ejb.classpath"
                     debug="true" debuglevel="lines,vars,source">
                    
              </javac>
             
              <copy todir="./classes">
                  <fileset dir="src"
                           includes="**/*.properties" />
              </copy>
             
           </target>

    <!-- - - - - - - - - - - - - - - - - -
          target: compile                     
         - - - - - - - - - - - - - - - - - -->
    <target name="compileWar">
       <javac srcdir="./src"
              destdir="./classes"
              classpathref="war.classpath"
              debug="true" debuglevel="lines,vars,source">
             
       </javac>
      
       <copy todir="./classes">
           <fileset dir="src"
                    includes="**/*.xsl" />
       </copy>
    </target>

</project>
5)     Notice the autodeploy directory. We need to create a domain in weblogic. Lets do that next.

6)     Open weblogic configuration wizard. Choose create new domain. Choose the options for web services if you want ( I chose jax-ws extension). Put the domain name as strutsApp. Choose console user/passwords. Choose JDK you want. Select AdministrationServer. Choose port (make sure you have no other servers running at that port). Then just click next and done.


7)     Now let’s take one of example applications that came with struts download. Navigate to STRUTS_HOME\apps. Extract struts2-blank.war to some folder. I will extract it to folder named struts2-blank in same directory.

8)     Create a folder under project in eclipse, call it Web-INF. Create a folder inside “WEB-INF” and call it “lib”. Now lets copy all the libraries from STRUTS_HOME\apps\struts2-blank\WEB-INF\lib.     


9)     Now let’s copy some files for this application. The plan is to deploy an example application, from source. So later if we want to make changes, we will do so to the existing app. Practice shows this is always more efficient that writing app from scratch. Copy example folder to root of project in eclipse, then error and index jsps to root, web.xml under web-inf to web-inf in project, copy example and struts xmls from src\java. Create a package under src folder in eclipse and call it example. Copy files from src\java\example to newly created package in eclipse. You should get compilation errors. Right click on project, choose properties, select on the left Java Build Path, click on libraries tab, choose add jars button on right, select project then web-inf then lib and select all the jars from there. Compilation errors should be gone.





10) The key again is to be able to run this example app, and once we do we will make all changes we want. So lets make some changes to build.xml. Here are changes I made:



















<target name="war" depends="" description="This is a struts project.">
     
     
      <antcall target="mkdirs"/>
        <antcall target="compileWar"/>
     
      <war destfile="${wl.autodeploy.dir}\${strutsApp.war.name}"
            webxml="./WEB-INF/web.xml">
            <classes dir="./classes"/>
            <lib dir="./WEB-INF/lib"/>
            <fileset dir="."
               includes="**/*.jsp" />
                       
             <fileset dir="."
                  includes="**/*.html" />
                       
             <fileset dir="."
                  includes="**/*.xhtml" />
      </war>
     
     
    </target>

<target name="compileWar">
      <javac srcdir="./src"
            destdir="./classes"
            classpathref="war.classpath"
            debug="true" debuglevel="lines,vars,source">
           
      </javac>
     
      <copy todir="./classes">
          <fileset dir="src"
                   includes="**/*.xsl" />
           
            <fileset dir="src"
                   includes="**/*.xml" />
           
            <fileset dir="src"
                  includes="**/*.properties" />
      </copy>
</target>




11)  Now lets run the build file. Call default target WAR. After this check the autodeploy directory of weblogic server. Lets say your weblogic is installed under C drive, go to C:\oracle\Middleware\user_projects\domains\strutsApp\autodeploy in there should be a war file created. Open it and check to make sure it has files in proper directories.







12)  Now lets start weblogic and see if this worked. I got errors at first, that was because I included all the jars from struts download in this application. Now that I changed this tutorial and asked to only include jars under example application, errors disappeared. You should see some log4j info logging in the eclipse console log.


13) Point your browser to http://localhost:7002/strutsApp. I have port 7002 – so replace with whatever port you have. You should see a page saying struts is up and running. Hooray!