第五篇:Struts小试牛刀篇 
严重警告:如果你跳过了上篇,在本篇遇到了的任何难题,本人概不负责 
开场白: 首先我希望你明白Struts是怎么工作的。在jsp页面上,当我们发生了提交动作(你就理解为当按钮按下的时候),页面上是数据(比如:文本框、复选框、单选框、下拉筐等中的内容)会自动写入到form中,当页面需要数据的时候也从form中取得。而action可以从form中取得相关的数据,并可以对这些数据进行处理,并把处理的结果也放到form中。 
以前我们这样定义一个文本框: 
<input  type="text" name="str1"> 
现在利用了Struts的标签库,我们这样定义一个文本框 
<html:text property="str1" /> 
这样定义的文本框,当页面上的submit按钮被按下的时候,这个文本框中的内容会自动的被放到from中,并且是放到form的一个str1属性中,为此,form会定义这个属性的get方法和set方法 
用例子来说明问题: 
工程的名字就叫:user 
(多么的希望你知道把你的user放在什么地方,user里面有些什么东西) 
这个例子的外观效果是这样的,首先第一个页面有两个文本框:分别输入你的姓(FirstName)和你的名(LastName),还有一个“提交”按纽,当按纽按下的时候,会调用另外的页面把名字姓和名整和起来显示出来。如果姓或者名有一个没有输入数据就“提交”的话,则转向另外的警告页面。不知道我说清楚了“需求”没有,下面让我们来看看效果吧。 
你需要什么:请把下面的1和2的所有东西都放在WEB-INF下,让他们并肩站者、平起平坐,而且以后的程序中,我再不说了,请你自觉的把它们放在你的应用下的WEB-INF下 
1:struts的标签库文件,这些东西如果你又找不到的话,请你还是给我email吧 
struts-bean.tld、 struts-config.xml.bak、struts-html.tld、struts-logic.tld、struts-nested.tld、struts-template.tld、struts-tiles.tld 
2:两个xml配置文件 
web.xml的内容如下,如果以后我不特别的说明,web.xml的内容就一下面的为准 
<?xml version="1.0" encoding="gb2312"?> 
  
<!DOCTYPE web-app 
     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd"> 
  
<web-app> 
  
  <!-- Action Servlet Configuration --> 
  <servlet> 
    <servlet-name>action</servlet-name> 
           <!-- Specify servlet class to use: 
                - Struts1.0.x: ActionComponentServlet 
                - Struts1.1:   ActionServlet 
                - no Struts:   TilesServlet 
                -->  
       <!-- <servlet-class>org.apache.struts.tiles.ActionComponentServlet</servlet-class> -->    
       <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>    
    <!-- <servlet-class>org.apache.struts.tiles.TilesServlet</servlet-class> --> 
        
        <!-- Tiles Servlet parameter  
                Specify configuration file names. There can be several comma  
                separated file names 
              -->  
       <init-param> 
          <param-name>definitions-config</param-name> 
          <param-value>/WEB-INF/tiles-defs.xml, 
                              /WEB-INF/tiles-s-defs.xml 
             </param-value> 
    </init-param> 
        <!-- Tiles Servlet parameter  
                Specify Tiles debug level. 
                O : no debug information 
                1 : debug information 
                2 : more debug information 
              -->  
    <init-param> 
      <param-name>definitions-debug</param-name> 
      <param-value>1</param-value> 
    </init-param> 
        
        <!-- Tiles Servlet parameter  
              Specify Digester debug level. This value is passed to Digester 
                O : no debug information 
                1 : debug information 
                2 : more debug information 
              -->  
    <init-param> 
      <param-name>definitions-parser-details</param-name> 
      <param-value>0</param-value> 
    </init-param> 
        
        <!-- Tiles Servlet parameter  
              Specify if xml parser should validate the Tiles configuration file. 
              true : validate. DTD should be specified in file header. 
              false : no validation 
              -->  
    <init-param> 
      <param-name>definitions-parser-validate</param-name> 
      <param-value>true</param-value> 
    </init-param> 
  
         <!-- Struts configuration, if Struts is used --> 
    <init-param> 
      <param-name>config</param-name> 
      <param-value>/WEB-INF/struts-config.xml</param-value> 
    </init-param> 
    <init-param> 
      <param-name>validate</param-name> 
      <param-value>true</param-value> 
    </init-param> 
    <init-param> 
      <param-name>debug</param-name> 
      <param-value>2</param-value> 
    </init-param> 
    <init-param> 
      <param-name>detail</param-name> 
      <param-value>2</param-value> 
    </init-param> 
        
    <load-on-startup>2</load-on-startup> 
  </servlet> 
  
  
  <!-- Action Servlet Mapping --> 
  <servlet-mapping> 
    <servlet-name>action</servlet-name> 
    <url-pattern>*.do</url-pattern> 
  </servlet-mapping> 
  
<session-config> 
<session-timeout>60</session-timeout> 
</session-config> 
  
  <!-- The Welcome File List --> 
  <welcome-file-list> 
    <welcome-file>login.jsp</welcome-file> 
  </welcome-file-list> 
  
  <!-- Struts Tag Library Descriptor --> 
  <taglib> 
    <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri> 
    <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location> 
  </taglib> 
   
  <taglib> 
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> 
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location> 
  </taglib> 
   
  <taglib> 
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> 
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> 
  </taglib> 
   
  <taglib> 
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri> 
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> 
  </taglib> 
   
  <!--database resource configuration--> 
  <resource-ref> 
    <description>zchFive database connection pool </description> 
    <res-ref-name>jdbc/zchFiveDB</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
  </resource-ref> 
   
</web-app> 
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> 
  
  <!-- ========== Form Bean Definitions =================================== --> 
       <form-beans> 
              <form-bean name="userForm"      type="UserForm"> 
              </form-bean>  
       </form-beans> 
  
 <!-- ========== Global Forward Definitions ============================== --> 
  <global-forwards> 
  
  </global-forwards> 
        
  <!-- ========== Action Mapping Definitions ============================== --> 
    <action-mappings>  
       
<action path="/execute" 
               type="UserAction" 
               name="userForm" 
               scope="request" 
                  validate="false"> 
               <forward name="err" path="/error.jsp"/> 
               <forward name="ok" path="/success.jsp"/> 
        </action>  
  </action-mappings>  
</struts-config> 
(.........)  
 
  |