7.Struts 2 Hello World - Developing Hello World Application

In this section we will develop Hello World application based on Struts 2 Framework. Our Struts 2 Hello World application is your first step towards developing applications based on Struts 2 Framework. We are here providing step by step guide for developing Hello World application in Struts 2 framework.

Tutorial covers basic steps like creating directory structure, developing build.xml file to build the application using ant build tool. Then we explained Java, JSP and configuration files that are required in this application.

Creating directory structure for the project

Step1: Extract struts 2 download and copy struts2-blank-2.0.6.war(If you are using latest version of struts 2 then version may be different for you) to your tomcats webapps directory. Rename struts2-blank-2.0.6.war to struts2tutorial and unzip it in the tomcats webapps directory. Now start the tomcat and type http://localhost:8080/struts2tutorial/ into your browser. You browser should show look like:

 

 

 

Congratulations you have successfully installed struts 2 blank application to start with.

 

Step 2: Now delete the content of struts2tutorial/WEB-INF/src and struts2tutorial/WEB-INF/classes directories, as we don't need these files that comes with struts 2 blank application.

 

Step 3: Create build.xml file in the struts2tutorial/WEB-INF/src and paste the following content in the build.xml file.

 

 

   

   

       

 

         

 

              

 

                

 

              

 

              

 

                

 

              

 

         

 

              

 

              

 

                      

 

                      

 

                      

 

                      

 

                      

 

                      

 

                      

 

              

 

              

 

              

 

                      

                      

                               file="src/struts.xml"

                               todir="src/classes"/>

 

 

              

              

 

              

 

                      

 

                              

 

                              

 

                                     

 

                                     

 

                                     

 

                              

 

                              

 

                      

 

              

 

              

 

              

 

                      

                         debug="true" debuglevel="lines,vars,source">

 

                              

 

                      

 

                      

 

                       jarfile="lib/${project.jar.file}"

 

                       basedir="src/classes"/>

 

              

              

 

              

 

                 description="Prepare for clean build">

 

                

 

                   dir="classes"/>

 

              

 

              

 

              

 

                description="Generate JavaDoc API docs">

 

                      

 

                      

 

                      

 

                               destdir="./doc/api"

 

                               classpath="${servlet.jar}:${jdbc20ext.jar}"

 

                               packagenames="*"

 

                               author="true"

 

                               private="true"

 

                               version="true"

 

                               windowtitle="${project.title} API Documentation"

 

                               doctitle="<h1>${project.title}

                          Documentation (Version ${project.version})</h1>"

 

                               bottom="Copyright © 2002">

 

                              

 

                      

 

              

 

              

 

              

 

              

 

              

 

                       description="Create binary distribution">

 

                

 

                       dir="${distpath.project}"/>

 

                

 

                       jarfile="${distpath.project}/${project.distname}.jar"

 

                       basedir="./classes"/>

 

                

 

                       file="${distpath.project}/${project.distname}.jar"

 

                       todir="${distpath.project}"/>

 

       

 

                

 

                       basedir="../"

 

                       warfile="${distpath.project}/${project.distname}.war"

 

                       webxml="web.xml">

 

                      

 

                 

 

   

 

 

   

 

       

 

       

 

Step 4: Create directory libext under the struts2tutorial/WEB-INF/ then you copy latest Servlets api jar (in our case servlet-api.jar) file over there. This library file will be used to compile Servlets in our application.

 

Step 5: Now create directories java and classes under struts2tutorial/WEB-INF/src. The directory struts2tutorial/WEB-INF/src/java will be used to put all the java sources file and directory struts2tutorial/WEB-INF/src/classes will be used by ant build utility to store compiled java files.

 

Now we have successfully created the directory structure and ant build file for our Struts 2 Hello World Application. In the next section we will create JSP, Java file and the configuration file and then test our Struts 2 Hello World application.

你可能感兴趣的:(7.Struts 2 Hello World - Developing Hello World Application)