JAVA及相關技術學習

          Servlet

           

          數據驗證

          幾乎所有和用戶交互的應用都需要數據驗證,而從頭設計并開發完善的數據驗證機制往往很費時。幸運的是,Struts框架提供了現成的、易于使用的數據驗證功能。Struts框架的數據驗證可分為兩種類型:表單驗證和業務邏輯驗證,在本例中,它們分別運用于以下場合:
          表單驗證:如果用戶沒有在表單中輸入姓名就提交表單,將生成表單驗證錯誤。
          業務邏輯驗證:如果用戶在表單中輸入姓名為“Monster”,按照本應用的業務規則,則不允許向“Monster”打招呼,因此將生成業務邏輯錯誤。
          第一種類型的驗證,即表單驗證由Action Bean來負責處理。
          這個工作可以用以上的HelloForm.java的validate()方法負責完成這一任務:

          public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
          {
             ActionErrors errors=new ActionErrors();
             if((userName==null)||(userName.length())<1)
              error.add("username",new ActionMessage("hello.no.username.error"));

          return errors;
          }
          第二種類型的驗證,即業務邏輯驗證由Action來負責處理。

          例:
          package hello;

          import javax.servlet.RequestDispatcher;
          import javax.servlet.Servlet.ServeltException;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.apache.struts.action.Action;
          import org.apache.struts.action.ActionMessage;
          import org.apache.struts.action.ActionMessages;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.util.MessageResource;

          public final class HelloAction extends Action
          {
             public ActionForward execute(ActionMapping mapping,
                                          ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response)throws Exception
                                          {
                                              MessageResources message=getResources(request);
                                              Action Messages errors=new ActionMessages();
                                              String userName=(String)((HelloForm)form).getUserName();
                                              //ActionMessages errors=new ActionMessage();
                                              //String userName=(String)((HelloForm)form).getUserName();
                                              String badUserName="Monster";

                                        if(userName.equalslgnoreCase(badUserName))
                                        {
                                            errors.add("username",new ActionMessage("hello.dont.talk.to.monster",badUserName));
                                            saveErrors(request,errors);
                                        }
                                         return (new ActionForward(mapping.getInput()));
                                        }
                                        PersonBean pd=new PersonBean();         
                                        pd.setUserName(userName);
                                        pd.saveToPersistentStore();
                                        request.setAttribute(Constants.PERSON_KEY,pd);
                                        request.removeAttribute(mapping.getAttribute());
                                        return(mapping.findForward("SayHello"));
          }
          }

          posted on 2009-03-11 14:29 亂せ英豪 閱讀(355) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 济南市| 济宁市| 威远县| 普兰县| 宝应县| 益阳市| 呼伦贝尔市| 鹤庆县| 百色市| 巨鹿县| 蒙山县| 民勤县| 汝城县| 固镇县| 三亚市| 固原市| 六安市| 自贡市| 宁陕县| 太仓市| 文成县| 辽阳市| 明溪县| 昌黎县| 信阳市| 马山县| 石城县| 辽中县| 长治市| 武威市| 汝南县| 额济纳旗| 历史| 阿图什市| 米易县| 应用必备| 海城市| 乌什县| 项城市| 衡阳市| 石屏县|