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:

 

 

struts2running

 

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.

 

 

<project name="Struts 2 Tutorial" basedir="../" default="all">

    <! -- Project settings -->

    <property name="project.title" value="RoseIndia Struts 2 Tutorials"/>

        <property name="project.jar.file" value="Struts2tutorial.jar"/>

 

          <path id="class.path">

 

               <fileset dir="lib">

 

                 <include name="**/*.jar"/>

 

               </fileset>

 

               <fileset dir="libext">

 

                 <include name="**/*.jar"/>

 

               </fileset>

 

          </path>

 

               <!-- Classpath for Project -->

 

               <path id="compile.classpath">

 

                       <pathelement path ="lib/commons-beanutils.jar"/>

 

                       <pathelement path ="lib/commons-digester.jar"/>

 

                       <pathelement path ="lib/struts.jar"/>

 

                       <pathelement path ="libext/servlet-api.jar"/>

 

                       <pathelement path ="libext/catalina-ant.jar"/>

 

                       <pathelement path ="classes"/>

 

                       <pathelement path ="${classpath}"/>

 

               </path>

 

               <!-- Check timestamp on files -->

 

               <target name="prepare">

 

                       <tstamp/>

                       <copy

                               file="src/struts.xml"

                               todir="src/classes"/>

 

 

               </target>

               <!-- Copy any resource or configuration files -->

 

               <target name="resources">

 

                       <copy todir="src/classes" includeEmptyDirs="no">

 

                               <fileset dir="src/java">

 

                               <patternset>

 

                                      <include name="**/*.conf"/>

 

                                      <include name="**/*.properties"/>

 

                                      <include name="**/*.xml"/>

 

                               </patternset>

 

                               </fileset>

 

                       </copy>

 

               </target>

 

               <!-- Normal build of application -->

 

               <target name="compile" depends="prepare,resources">

 

                       <javac srcdir="src" destdir="src/classes"

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

 

                               <classpath refid="class.path"/>

 

                       </javac>

 

                       <jar

 

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

 

                       basedir="src/classes"/>

 

               </target>

               <!-- Remove classes directory for clean build -->

 

               <target name="clean"

 

                 description="Prepare for clean build">

 

                 <delete dir="classes"/>

 

                 <mkdir  dir="classes"/>

 

               </target>

 

               <!-- Build Javadoc documentation -->

 

               <target name="javadoc"

 

                description="Generate JavaDoc API docs">

 

                       <delete dir="./doc/api"/>

 

                       <mkdir dir="./doc/api"/>

 

                       <javadoc sourcepath="./src/java"

 

                               destdir="./doc/api"

 

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

 

                               packagenames="*"

 

                               author="true"

 

                               private="true"

 

                               version="true"

 

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

 

                               doctitle="&lt;h1&gt;${project.title}

                          Documentation (Version ${project.version})&lt;/h1&gt;"

 

                               bottom="Copyright &#169; 2002">

 

                               <classpath refid="compile.classpath"/>

 

                       </javadoc>

 

               </target>

 

               <!-- Build entire project -->

 

               <target name="project" depends="clean,prepare,compile"/>

 

               <!-- Create binary distribution -->

 

               <target name="dist"

 

                       description="Create binary distribution">

 

                 <mkdir

 

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

 

                 <jar

 

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

 

                       basedir="./classes"/>

 

                 <copy

 

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

 

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

 

       

 

                 <war

 

                       basedir="../"

 

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

 

                       webxml="web.xml">

 

                       <exclude name="${distpath.project}/${project.distname}.war"/>

 

                  </war>

 

    </target>

 

 

    <!-- Build project and create distribution-->

 

        <target name="all" depends="project"/>

 

        </project>

 

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
192.168.85.1 - - [26/Jun/2022:06:07:07 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 14 192.168.85.1 - - [26/Jun/2022:06:08:06 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 15 192.168.85.1 - - [26/Jun/2022:06:08:16 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 1227 192.168.85.1 - - [26/Jun/2022:06:10:15 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 79 192.168.85.1 - - [26/Jun/2022:06:13:25 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 404 752 192.168.85.1 - - [26/Jun/2022:06:16:42 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:16:57 -0400] "GET //struts2-showcase/hhh.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:18:55 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:19:02 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:19:09 -0400] "GET //struts2-showcase/hhh1.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:19:34 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 400 192.168.85.1 - - [26/Jun/2022:06:20:37 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 5 192.168.85.1 - - [26/Jun/2022:06:20:42 -0400] "GET //struts2-showcase/hhh1.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:20:46 -0400] "GET //struts2-showcase/hhh.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:20:51 -0400] "GET /struts2-showcase/hhh.jsp HTTP/1.1" 403 642
07-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值