`
yyzhpq
  • 浏览: 291151 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

第五篇:Struts小试牛刀篇

    博客分类:
  • j2ee
阅读更多

严重警告:如果你跳过了上篇,在本篇遇到了的任何难题,本人概不负责<!----><o:p></o:p>

开场白: 首先我希望你明白Struts是怎么工作的。在jsp页面上,当我们发生了提交动作(你就理解为当按钮按下的时候),页面上是数据(比如:文本框、复选框、单选框、下拉筐等中的内容)会自动写入到form中,当页面需要数据的时候也从form中取得。而action可以从form中取得相关的数据,并可以对这些数据进行处理,并把处理的结果也放到form中。<o:p></o:p>

以前我们这样定义一个文本框:<o:p></o:p>

<input  type="text" name="str1"><o:p></o:p>

现在利用了Struts的标签库,我们这样定义一个文本框<o:p></o:p>

<html:text property="str1" /><o:p></o:p>

这样定义的文本框,当页面上的submit按钮被按下的时候,这个文本框中的内容会自动的被放到from中,并且是放到form的一个str1属性中,为此,form会定义这个属性的get方法和set方法<o:p></o:p>

用例子来说明问题:<o:p></o:p>

工程的名字就叫:user<o:p></o:p>

(多么的希望你知道把你的user放在什么地方,user里面有些什么东西)<o:p></o:p>

这个例子的外观效果是这样的,首先第一个页面有两个文本框:分别输入你的姓(FirstName)和你的名(LastName),还有一个“提交”按纽,当按纽按下的时候,会调用另外的页面把名字姓和名整和起来显示出来。如果姓或者名有一个没有输入数据就“提交”的话,则转向另外的警告页面。不知道我说清楚了“需求”没有,下面让我们来看看效果吧。<o:p></o:p>

你需要什么:请把下面的12的所有东西都放在WEB-INF下,让他们并肩站者、平起平坐,而且以后的程序中,我再不说了,请你自觉的把它们放在你的应用下的WEB-INF<o:p></o:p>

1struts的标签库文件,这些东西如果你又找不到的话,请你还是给我email<o:p></o:p>

struts-bean.tld struts-config.xml.bakstruts-html.tldstruts-logic.tldstruts-nested.tldstruts-template.tldstruts-tiles.tld<o:p></o:p>

2:两个xml配置文件<o:p></o:p>

web.xml的内容如下,如果以后我不特别的说明,web.xml的内容就一下面的为准<o:p></o:p>

<?xml version="1.0" encoding="gb2312"?><o:p></o:p>

<o:p> </o:p>

<!DOCTYPE web-app<o:p></o:p>

     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"<o:p></o:p>

    "http://java.sun.com/dtd/web-app_2_3.dtd"><o:p></o:p>

<o:p> </o:p>

<web-app><o:p></o:p>

<o:p> </o:p>

  <!-- Action Servlet Configuration --><o:p></o:p>

  <servlet><o:p></o:p>

    <servlet-name>action</servlet-name><o:p></o:p>

           <!-- Specify servlet class to use:<o:p></o:p>

                - Struts1.0.x: ActionComponentServlet<o:p></o:p>

                - Struts1.1:   ActionServlet<o:p></o:p>

                - no Struts:   TilesServlet<o:p></o:p>

                --> <o:p></o:p>

       <!-- <servlet-class>org.apache.struts.tiles.ActionComponentServlet</servlet-class> -->   <o:p></o:p>

       <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>   <o:p></o:p>

    <!-- <servlet-class>org.apache.struts.tiles.TilesServlet</servlet-class> --><o:p></o:p>

       <o:p></o:p>

        <!-- Tiles Servlet parameter <o:p></o:p>

                Specify configuration file names. There can be several comma <o:p></o:p>

                separated file names<o:p></o:p>

              --> <o:p></o:p>

       <init-param><o:p></o:p>

          <param-name>definitions-config</param-name><o:p></o:p>

          <param-value>/WEB-INF/tiles-defs.xml,<o:p></o:p>

                              /WEB-INF/tiles-s-defs.xml<o:p></o:p>

             </param-value><o:p></o:p>

    </init-param><o:p></o:p>

        <!-- Tiles Servlet parameter <o:p></o:p>

                Specify Tiles debug level.<o:p></o:p>

                O : no debug information<o:p></o:p>

                1 : debug information<o:p></o:p>

                2 : more debug information<o:p></o:p>

              --> <o:p></o:p>

    <init-param><o:p></o:p>

      <param-name>definitions-debug</param-name><o:p></o:p>

      <param-value>1</param-value><o:p></o:p>

    </init-param><o:p></o:p>

       <o:p></o:p>

        <!-- Tiles Servlet parameter <o:p></o:p>

              Specify Digester debug level. This value is passed to Digester<o:p></o:p>

                O : no debug information<o:p></o:p>

                1 : debug information<o:p></o:p>

                2 : more debug information<o:p></o:p>

              --> <o:p></o:p>

    <init-param><o:p></o:p>

      <param-name>definitions-parser-details</param-name><o:p></o:p>

      <param-value>0</param-value><o:p></o:p>

    </init-param><o:p></o:p>

       <o:p></o:p>

        <!-- Tiles Servlet parameter <o:p></o:p>

              Specify if xml parser should validate the Tiles configuration file.<o:p></o:p>

              true : validate. DTD should be specified in file header.<o:p></o:p>

              false : no validation<o:p></o:p>

              --> <o:p></o:p>

    <init-param><o:p></o:p>

      <param-name>definitions-parser-validate</param-name><o:p></o:p>

      <param-value>true</param-value><o:p></o:p>

    </init-param><o:p></o:p>

<o:p> </o:p>

         <!-- Struts configuration, if Struts is used --><o:p></o:p>

    <init-param><o:p></o:p>

      <param-name>config</param-name><o:p></o:p>

      <param-value>/WEB-INF/struts-config.xml</param-value><o:p></o:p>

    </init-param><o:p></o:p>

    <init-param><o:p></o:p>

      <param-name>validate</param-name><o:p></o:p>

      <param-value>true</param-value><o:p></o:p>

    </init-param><o:p></o:p>

    <init-param><o:p></o:p>

      <param-name>debug</param-name><o:p></o:p>

      <param-value>2</param-value><o:p></o:p>

    </init-param><o:p></o:p>

    <init-param><o:p></o:p>

      <param-name>detail</param-name><o:p></o:p>

      <param-value>2</param-value><o:p></o:p>

    </init-param><o:p></o:p>

       <o:p></o:p>

    <load-on-startup>2</load-on-startup><o:p></o:p>

  </servlet><o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

  <!-- Action Servlet Mapping --><o:p></o:p>

  <servlet-mapping><o:p></o:p>

    <servlet-name>action</servlet-name><o:p></o:p>

    <url-pattern>*.do</url-pattern><o:p></o:p>

  </servlet-mapping><o:p></o:p>

<o:p> </o:p>

<session-config><o:p></o:p>

<session-timeout>60</session-timeout><o:p></o:p>

</session-config><o:p></o:p>

<o:p> </o:p>

  <!-- The Welcome File List --><o:p></o:p>

  <welcome-file-list><o:p></o:p>

    <welcome-file>login.jsp</welcome-file><o:p></o:p>

  </welcome-file-list><o:p></o:p>

<o:p> </o:p>

  <!-- Struts Tag Library Descriptor --><o:p></o:p>

  <taglib><o:p></o:p>

    <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri><o:p></o:p>

    <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location><o:p></o:p>

  </taglib><o:p></o:p>

  <o:p></o:p>

  <taglib><o:p></o:p>

    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri><o:p></o:p>

    <taglib-location>/WEB-INF/struts-html.tld</taglib-location><o:p></o:p>

  </taglib><o:p></o:p>

  <o:p></o:p>

  <taglib><o:p></o:p>

    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri><o:p></o:p>

    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location><o:p></o:p>

  </taglib><o:p></o:p>

  <o:p></o:p>

  <taglib><o:p></o:p>

    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri><o:p></o:p>

    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location><o:p></o:p>

  </taglib><o:p></o:p>

  <o:p></o:p>

  <!--database resource configuration--><o:p></o:p>

  <resource-ref><o:p></o:p>

    <description>zchFive database connection pool </description><o:p></o:p>

    <res-ref-name>jdbc/zchFiveDB</res-ref-name><o:p></o:p>

    <res-type>javax.sql.DataSource</res-type><o:p></o:p>

    <res-auth>Container</res-auth><o:p></o:p>

  </resource-ref><o:p></o:p>

  <o:p></o:p>

</web-app><o:p></o:p>

struts-config.xml的内容如下,这个配置的内容会根据你的应用不同而不同,但格式会相同<o:p></o:p>

<?xml version="1.0" encoding="ISO-8859-1" ?><o:p></o:p>

<o:p> </o:p>

<!DOCTYPE struts-config PUBLIC<o:p></o:p>

          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"<o:p></o:p>

          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"><o:p></o:p>

<!--<o:p></o:p>

     This is the Struts configuration file for the example application,<o:p></o:p>

     using the proposed new syntax.<o:p></o:p>

<o:p> </o:p>

     NOTE:  You would only flesh out the details in the "form-bean"<o:p></o:p>

     declarations if you had a generator tool that used them to create<o:p></o:p>

     the corresponding Java classes for you.  Otherwise, you would<o:p></o:p>

     need only the "form-bean" element itself, with the corresponding<o:p></o:p>

     "name" and "type" attributes.<o:p></o:p>

--><o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

<struts-config><o:p></o:p>

<o:p> </o:p>

  <!-- ========== Form Bean Definitions =================================== --><o:p></o:p>

       <form-beans><o:p></o:p>

              <form-bean name="userForm"      type="UserForm"><o:p></o:p>

              </form-bean> <o:p></o:p>

       </form-beans><o:p></o:p>

<o:p> </o:p>

 <!-- ========== Global Forward Definitions ============================== --><o:p></o:p>

  <global-forwards><o:p></o:p>

<o:p> </o:p>

  </global-forwards><o:p></o:p>

       <o:p></o:p>

  <!-- ========== Action Mapping Definitions ============================== --><o:p></o:p>

    <action-mappings> <o:p></o:p>

      <o:p></o:p>

<action path="/execute"<o:p></o:p>

               type="UserAction"<o:p></o:p>

               name="userForm"<o:p></o:p>

               scope="request"<o:p></o:p>

                  validate="false"><o:p></o:p>

               <forward name="err" path="/error.jsp"/><o:p></o:p>

               <forward name="ok" path="/success.jsp"/><o:p></o:p>

        </action> <o:p></o:p>

  </action-mappings> <o:p></o:p>

</struts-config><o:p></o:p>

输入数据的页面<o:p></o:p>

in.jsp<o:p></o:p>

<%@ page contentType="text/html; charset=gb2312" %><o:p></o:p>

<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %><o:p></o:p>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><o:p></o:p>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><o:p></o:p>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %><o:p></o:p>

<html:html><o:p></o:p>

    <head><o:p></o:p>

        <title>Login</title><o:p></o:p>

    </head><o:p></o:p>

    <o:p></o:p>

    <body><o:p></o:p>

    <center><o:p></o:p>

    <pre><o:p></o:p>

    <html:form action="execute.do" method="post"><o:p></o:p>

                    <h3> 请输入 </h3><o:p></o:p>

         <o:p></o:p>

         FirstName:   <html:text property="str1" size="12"/><o:p></o:p>

         <o:p></o:p>

         LastName:   <html:text property="str2" size="12"/><o:p></o:p>

       <o:p></o:p>

       <html:submit value="提交"/>  <html:reset value="重置"/><o:p></o:p>

 </html:form><o:p></o:p>

     </pre><o:p></o:p>

    </center><o:p></o:p>

    </body><o:p></o:p>

</html:html><o:p></o:p>

显示正确结果的页面<o:p></o:p>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics