<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    期待更好更穩定的開源FrameWork的出現,讓我們一起努力吧!  
    日歷
    <2007年10月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910
    統計
    • 隨筆 - 78
    • 文章 - 1
    • 評論 - 29
    • 引用 - 0

    導航

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案(42)

    文章檔案(37)

    相冊

    搜索

    •  

    積分與排名

    • 積分 - 45522
    • 排名 - 1064

    最新隨筆

    最新評論

    閱讀排行榜

    評論排行榜

     

    The ActionForm Class
    本文出處:http://www.roseindia.net/struts/strutsActionForms.shtml
    In this lesson you will learn about the ActionForm in detail. I will show you a good example of ActionForm. This example will help you understand Struts in detail. We will create user interface to accept the address details and then validate the details on server side. On the successful validation of data, the data will be sent to model (the action class). In the Action class we can add the business processing logic but in this case we are just forwarding it to the sucess.jsp.  

    What is ActionForm?
    An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the ActionForm object is automatically populated on the server side with data entered from a form on the client side.

    We will first create the class AddressForm which extends the ActionForm class. Here is the code of the class:

    AddressForm.java

    package roseindia.net;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts.action.*;


    /**
    @author Deepak Kumar
    * @Web http://www.roseindia.net
    * @Email roseindia_net@yahoo.com
    */

    /**
     * Form bean for the Address Entry Screen.
     *
    */
    public class AddressForm extends ActionForm
    {
      private String name=null;
      private String address=null;
      private String emailAddress=null;

      public void setName(String name){
        this.name=name;
      }

      public String getName(){
        return this.name;
      }

      public void setAddress(String address){
        this.address=address;
      }

      public String getAddress(){
        return this.address;
      }


      public void setEmailAddress(String emailAddress){
        this.emailAddress=emailAddress;
      }

      public String getEmailAddress(){
        return this.emailAddress;
      }


        /**
         * Reset all properties to their default values.
         *
         @param mapping The mapping used to select this instance
         @param request The servlet request we are processing
         */
        public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.name=null;
        this.address=null;
        this.emailAddress=null;
        }

        /**
         * Reset all properties to their default values.
         *
         @param mapping The mapping used to select this instance
         @param request The servlet request we are processing
       @return errors
         */
      public ActionErrors validate
          ActionMapping mapping, HttpServletRequest request ) {
          ActionErrors errors = new ActionErrors();
          
          ifgetName() == null || getName().length() ) {
            errors.add("name",new ActionMessage("error.name.required"));
          }
          ifgetAddress() == null || getAddress().length() ) {
            errors.add("address",new ActionMessage("error.address.required"));
          }
          ifgetEmailAddress() == null || getEmailAddress().length() ) {
            errors.add("emailaddress",new ActionMessage("error.emailaddress.required"));
          }

          return errors;
      }

    }

    The above class populates the Address Form data and validates it. The validate() method is used to validate the inputs. If any or all of the fields on the form are blank, error messages are added to the ActionMapping object.  Note that we are using ActionMessage class, ActionError is now deprecated and will be removed in next version.

    Now we will create the Action class which is the model part of the application. Our action class simply forwards the request the Success.jsp. Here is the code of the AddressAction class:

    AddressAction.java

    package roseindia.net;

    /**
    @author Deepak Kumar
    * @Web http://www.roseindia.net
    * @Email roseindia_net@yahoo.com
    */

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;

    public class AddressAction extends Action
    {
      public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse responsethrows Exception{
          return mapping.findForward("success");
      }
    }

    Now we have to create an entry for form bean in the struts-config.xml. Add the following lines in the struts-config.xml file:

    <form-bean
    name="AddressForm"
    type="roseindia.net.AddressForm"/>

    Add the following line in the struts-config.xml file for handling the action "/Address.do":

    <action
         path="/Address"
         type="roseindia.net.AddressAction"
         name="AddressForm"
         scope="request"
         validate="true"
         input="/pages/Address.jsp">
        <forward name="success" path="/pages/success.jsp"/>
    </action>

    Now create Address.jsp, which is our form for entering the address details. Code for Address.jsp is as follows:

    Address.jsp

       <%@ taglib uri="/tags/struts-bean" prefix="bean" %>
                <%@ taglib uri="/tags/struts-html" prefix="html" %>
                <html:html locale="true">
                <head>
                <title><bean:message key="welcome.title"/></title>
                <html:base/>
                </head>
                <body bgcolor="white">
                <html:form action="/Address">
                <html:errors/>
                <table>
                <tr>
                <td align="center" colspan="2">
                <font size="4">Please Enter the Following Details</font>
                </tr>
                <tr>
                <td align="right">
                Name
                </td>
                <td align="left">
                <html:text property="name" size="30" maxlength="30"/>
                </td>
                </tr>
                <tr>
                <td align="right">
                Address
                </td>
                <td align="left">
                <html:text property="address" size="30" maxlength="30"/>
                </td>
                </tr>
                <tr>
                <td align="right">
                E-mail address
                </td>
                <td align="left">
                <html:text property="emailAddress" size="30" maxlength="30"/>
                </td>
                </tr>
                <tr>
                <td align="right">
                <html:submit>Save</html:submit>
                </td>
                <td align="left">
                <html:cancel>Cancel</html:cancel>
                </td>
                </tr>
                </table>
                </html:form>
                </body>
                </html:html>

    User enter the values in the form and click on the submit form. Form  validation is done on the server side and error message is displays on the jsp page. To display the error on the jsp page <html:errors/> tag is used. The <html:errors/> tag displays all the errors in one go.  To create text box <html:text .../> is used in jsp page. 

    e.g.

    <html:text property="address" size="30" maxlength="30"/>

    Above tag creates text box for entering the address. The address is retrieved from and later stored in the property named address in the form-bean. The tag <html:submit>Save</html:submit> creates the submit button and the tag <html:cancel>Cancel</html:cancel> is used to create the Cancel button.

    Add the following line in the index.jsp to create a link for testing the Address.jsp form:

    <html:link page="/pages/Address.jsp">Test the Address Form</html:link>

    Build the application and click on the Test the Address Form link on the index page to test the newly created screen. You should see the following screen.

    In this lesson you learned how to create data entry form using struts, validate and finally send process the business logic in the model part of the struts.

     

                             



    posted on 2007-06-03 13:01 BlueSky_itwangxinli 閱讀(986) 評論(3)  編輯  收藏
    評論:
    • # re: The ActionForm Class  筱筱 Posted @ 2007-06-03 20:56
      It is a good way to leaning English and Struts.  回復  更多評論   

    • # re: The ActionForm Class  s Posted @ 2007-10-08 10:19
      s  回復  更多評論   

    • # re: The ActionForm Class  s Posted @ 2007-10-08 10:19
      @s
        回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
     
    Copyright © BlueSky_itwangxinli Powered by: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 国产99视频精品免费观看7| 99精品全国免费观看视频| 午夜理伦剧场免费| 日韩中文无码有码免费视频| 久久久久久久综合日本亚洲| 亚洲中文无码永久免| 久草免费手机视频| 亚洲国产婷婷香蕉久久久久久| 亚洲人成电影在线观看青青| 久久av免费天堂小草播放| 最近的免费中文字幕视频| 久久久久亚洲av无码专区| 精精国产www视频在线观看免费| 亚洲人成色77777在线观看大| 国产高潮久久免费观看| 日韩一区二区三区免费体验| 国产大陆亚洲精品国产| 成年女人免费v片| 国产精品亚洲一区二区三区久久| 亚洲精品网站在线观看不卡无广告| 成人无码视频97免费| 亚洲精品无码激情AV| baoyu777永久免费视频| 区久久AAA片69亚洲| 一级一级毛片免费播放| 亚洲AV无码乱码在线观看性色扶 | 久久九九亚洲精品| 99久久免费精品视频| 亚洲VA中文字幕无码毛片| fc2成年免费共享视频网站| 亚洲av无码乱码国产精品fc2| 99国产精品永久免费视频| 亚洲美女aⅴ久久久91| 中文字幕免费视频| 亚洲av日韩综合一区在线观看| 99久久国产精品免费一区二区| 亚洲精品夜夜夜妓女网| 中文字幕免费视频精品一| 国产亚洲国产bv网站在线| 拍拍拍又黄又爽无挡视频免费| 羞羞视频免费网站日本|