<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
我们先来用ANT搭建起一个工作环境。
首先从apache.org获得ant的可执行二进制win或linux代码,然后根据不同的系统配置好ANT_HOME环境变量和ant的bin所在的path目录;关于两种系统的环境配置可以在我的java环境的搭建文章中找到方法;然后测试你的ant是否可用:在任何一个目录下运行ant会出现一下信息:
C:/>ant
Buildfile: build.xml does not exist!
Build failed
这说明你的ant可以工作了。
下面让我们再从apache.org获得Struts的包文件。找到struts-example包,我们就借用一下这个很好的教学包来修改创建并理解我们的Struts结构。
打开此包,我们可以发现此根文件夹存在一个build.xml文件,这个就是我们前面ant程序所必需的文件,那么就让我们来看看这个文件:
<project name="struts-example.project" default="war-package" basedir=".">这一行定义了你的工程名struts-example.project打包war工程默认打包路径:.为当前所在目录
<property name="app.name" value="struts-example" />
<property name="build.dir" value="WEB-INF/classes" />
<property name="lib.dir" value="build/lib"/>
<property name="build.home" value="build/${app.name}"/>
<property name="src.dir" value="E:/Tomcat/webapps" />
<property name="del.dir" value="E:/Tomcat/webapps/struts-example" />以上几行定义了此build.xml文件内所要用到的变量,我很是推荐这种变量的申明用法,如果你做多了就会发现这也是一种很简单的模式。
<!-- =================================================================== -->
<!-- Compile package -->
<!-- =================================================================== -->
<target name="compile">这一行定义了ant 后面所要的参数,也就是我们将来使用ant compile来编译以下<target></target>之间的目标文件。
<javac srcdir="WEB-INF/src/org/apache/struts/webapp/example"
destdir="${build.dir}" 这一行当中WEB-INF/src/org/apache/struts/webapp/example是源文件所在的目录而destdir="${build.dir}"则是编译好的class文件所放置目录,这里所要指出的是因为源文件package了org/apache/struts/webapp/example所以目录为WEB-INF/classes
debug="on"
deprecation="on"
optimize="off"
>
<classpath>
<pathelement location="WEB-INF/lib/struts.jar" />这一行声明了本次ant命令所要用到的类文件的classpath:WEB-INF/lib/struts.jar
</classpath>
<include name="OkInensModel.java"/>
<include name="OkInensAction.java"/>所要编译的文件,你也可以用一个<include name="*.java"/>代替
</javac>
</target>
以下解释请参考以上的自己解释:
<!-- =================================================================== -->
<!-- Creates the package jar archive -->
<!-- =================================================================== -->
<target name="war">
<jar jarfile="${app.name}.war"
basedir="" includes="**"/>
</target>
<!-- =================================================================== -->
<!-- Creates the distribution -->
<!-- =================================================================== -->
<!-- =================================================================== -->
<!-- Create directories and copy files for joinStruts web app -->
<!-- =================================================================== -->
<target name="copy">
<delete dir="${del.dir}"/>
<!-- copy web application -->
<!-- copy struts configuration files -->
<copy todir="${src.dir}">
<fileset dir=""
includes="${app.name}.war"
/>
</copy>
</target>
<!-- =================================================================== -->
<!-- Packages the distribution to a WAR -->
<!-- =================================================================== -->
</project>
下面让我们来看看此文件夹下的web-inf下其他文件:
web.xml
此文件定义了Struts架构的基本信息,包括*.do的映射配置:
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
action.xml在Struts1.0以后已经被取消
struts-config.xml
此文件为文件映射目录配置文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<!--
This is the Struts configuration file for the example application,
using the proposed new syntax.
NOTE: You would only flesh out the details in the "form-bean"
declarations if you had a generator tool that used them to create
the corresponding Java classes for you. Otherwise, you would
need only the "form-bean" element itself, with the corresponding
"name" and "type" attributes.
-->
<struts-config>
<!-- ========== Data Source Configuration =============================== -->
<!--
<data-sources>
<data-source>
<set-property property="autoCommit"
value="false"/>
<set-property property="description"
value="Example Data Source Configuration"/>
<set-property property="driverClass"
value="org.postgresql.Driver"/>
<set-property property="maxCount"
value="4"/>
<set-property property="minCount"
value="2"/>
<set-property property="password"
value="mypassword"/>
<set-property property="url"
value="jdbc:postgresql://localhost/mydatabase"/>
<set-property property="user"
value="myusername"/>
</data-source>
</data-sources>
-->
<!-- ========== Form Bean Definitions =================================== -->
<form-beans>
<!-- Logon form bean -->
<!--
<form-bean name="logonForm"
type="org.apache.struts.webapp.example.LogonForm"/>
-->
<!--
<form-bean name="logonForm"
type="org.apache.struts.action.DynaActionForm">
-->
<form-bean name="logonForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="username" type="java.lang.String"/>
<form-property name="password" type="java.lang.String"/>
</form-bean>
<!-- Registration form bean -->
<form-bean name="registrationForm"
type="org.apache.struts.webapp.example.RegistrationForm"/>
<!-- Subscription form bean -->
<form-bean name="subscriptionForm"
type="org.apache.struts.webapp.example.SubscriptionForm"/>
</form-beans>
<!-- ========== Global Forward Definitions ============================== -->
<global-forwards>
<forward name="logoff" path="/logoff.do"/>
<forward name="logon" path="/logon.jsp"/>
<forward name="registration" path="/registration.jsp"/>
<forward name="success" path="/mainMenu.jsp"/>
</global-forwards>
<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<!-- Edit user registration -->
<action path="/editRegistration"
type="org.apache.struts.webapp.example.EditRegistrationAction"
attribute="registrationForm"
scope="request"
validate="false">
<forward name="success" path="/registration.jsp"/>
</action>
<action
path="/okInens"
type="org.apache.struts.webapp.example.OkInensAction"
validate="false"
scope="request">
</action>
<!-- Edit mail subscription -->
<action path="/editSubscription"
type="org.apache.struts.webapp.example.EditSubscriptionAction"
attribute="subscriptionForm"
scope="request"
validate="false">
<forward name="failure" path="/mainMenu.jsp"/>如果EditSubscriptionAction返回failure进入mainMenu.jsp
<forward name="success" path="/subscription.jsp"/>如果EditSubscriptionAction返回success进入subscription.jsp
</action>
<!-- Process a user logoff -->
<action path="/logoff"
type="org.apache.struts.webapp.example.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action>
<!-- Process a user logon -->
<action path="/logon"
type="org.apache.struts.webapp.example.LogonAction"
name="logonForm"
scope="session"
input="logon">
<exception
key="expired.password"
type="org.apache.struts.webapp.example.ExpiredPasswordException"
path="/changePassword.jsp"/>
</action>
<!-- Save user registration -->
<action path="/saveRegistration"
type="org.apache.struts.webapp.example.SaveRegistrationAction"
name="registrationForm"
scope="request"
input="registration"/>
<!-- Save mail subscription -->
<action path="/saveSubscription"
type="org.apache.struts.webapp.example.SaveSubscriptionAction"
name="subscriptionForm"
scope="request"
input="subscription">
<forward name="subscription" path="/subscription.jsp"/>
<forward name="success" path="/editRegistration.do?action=Edit"/>
</action>
<!-- Display the "walking tour" documentation -->
<action path="/tour"
forward="/tour.htm">
</action>
<action path="/xs"
forward="/xs.jsp">
</action>
</action-mappings>
<!-- ========== Controller Configuration ================================ -->
<controller>
<!-- The "input" parameter on "action" elements is the name of a
local or global "forward" rather than a module-relative path -->
<set-property property="inputForward" value="true"/>
</controller>
<!-- ========== Message Resources Definitions =========================== -->
<message-resources
parameter="org.apache.struts.webapp.example.ApplicationResources"/>
<message-resources
parameter=
"org.apache.struts.webapp.example.AlternateApplicationResources"
key="alternate">
</message-resources>
<!-- ========== Plug Ins Configuration ================================== -->
<plug-in className=
"org.apache.struts.webapp.example.memory.MemoryDatabasePlugIn">
<set-property property="pathname" value="/WEB-INF/database.xml"/>
</plug-in>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
红色是我自己加改的,你可以自己根据自己的需要添加更改
以下是我的struts-example运行效果:
我的struts-example.war下载
以上只是我的一点理解,如果你想更深刻的理解使用这些,请你自己来进行更深入的学习和使用吧,我想那才是你真正体会并运用了这项技术,在此对apache.org表示最敬意的感谢,他为我们带来了许多开源好技术。
新奇世界 ?iNENS 提供
用Ant,Struts搭建简单工程应用环境
最新推荐文章于 2025-08-07 13:27:41 发布