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)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 元阳县| 民权县| 阿荣旗| 华池县| 宜昌市| 西贡区| 乌鲁木齐市| 永兴县| 包头市| 喀什市| 江津市| 沙河市| 茂名市| 中江县| 神池县| 高密市| 洛浦县| 金寨县| 汪清县| 桦甸市| 五莲县| 汝南县| 嘉祥县| 内江市| 柘荣县| 进贤县| 潜山县| 双城市| 靖边县| 海南省| 深圳市| 桦甸市| 资阳市| 淮安市| 庆阳市| 抚州市| 江门市| 安顺市| 渝中区| 眉山市| 新田县|