笑看風云

          一切從頭開始
          posts - 28, comments - 1, trackbacks - 0, articles - 2

          表單錯誤信息的顯示

          Posted on 2008-07-10 14:05 笑看風云 閱讀(184) 評論(0)  編輯  收藏 所屬分類: struts
          1.login.jsp
          <%@ page language="java" pageEncoding="UTF-8"%>
          <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
          <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
           
          <html> 
              
          <head>
                  
          <title>login</title>
              
          </head>
              
          <body>
                  
          <html:form action="/login">
                   
          <html:errors property="handle"/><br/>
                      username : 
          <html:text property="username"/><html:errors property="username"/><br/>
                      password : 
          <html:password property="password"/><html:errors property="password"/><br/>
                      
          <html:submit/><html:cancel/>
                  
          </html:form>
              
          </body>
          </html>
          2.loginAction
          /*
           * Generated by MyEclipse Struts
           * Template path: templates/java/JavaClass.vtl
           
          */

          package com.my.struts.action;

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

          import org.apache.struts.Globals;
          import org.apache.struts.action.Action;
          import org.apache.struts.action.ActionErrors;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.action.ActionMessage;
          import org.apache.struts.action.ActionMessages;

          import com.my.struts.form.LoginForm;

          /** 
           * MyEclipse Struts
           * Creation date: 06-18-2008
           * 
           * XDoclet definition:
           * @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
           * @struts.action-forward name="ok" path="/login/login_suc.jsp"
           
          */

          public class LoginAction extends Action {
              
          /*
               * Generated Methods
               
          */


              
          /** 
               * Method execute
               * 
          @param mapping
               * 
          @param form
               * 
          @param request
               * 
          @param response
               * 
          @return ActionForward
               
          */

              
          public ActionForward execute(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response)
          throws Exception {
                  LoginForm loginForm 
          = (LoginForm) form;// TODO Auto-generated method stub
                  String username=loginForm.getUsername();
                  String password
          =loginForm.getPassword();
                  
          if(username.equals("admin")&&password.equals("111111"))
                   
          return mapping.findForward("ok");
                  
          //--------------------------------------------------------------------------------
                  
                  
          //第一種方法
                  ActionErrors errors=new ActionErrors();
                  ActionMessage msg;
                  msg
          =new ActionMessage("login.failed");
                  errors.add(
          "handle",msg);
                  saveErrors(request, errors);
                  
          //--------------------------------------------------------------------------------
                  
                  
          //第二種方法(把login.jsp中<html:errors property="handle"/><br/>改為<html:errors /><br/>)
                  
          //        ActionMessages msg=this.getMessages(request);
          //        msg.add(Globals.ERROR_KEY, new ActionMessage("login.failed2"));
          //        this.addErrors(request, msg);
                  
          //--------------------------------------------------------------------------------
                  
                  
          return mapping.getInputForward();
              }

          }

          3.ApplicationResources.properties
          # Resources for parameter 'com.my.struts.ApplicationResources'
          # Project MessageTest
          login.failed
          =<font color="#FF0044">錯誤的用戶名或密碼</font>
          login.failed2
          =<font color="#FF0044">出錯啦!</font>

          當然啦,也可以能過loginform里的validate來實現:
          ActionErrors errors = new ActionErrors(); // create ActionErrors instance  
            ActionMessage msg;
          if (username==null||username.length() < 1) {
              msg = new ActionMessage("error.notnull", username);
              errors.add("username", msg);
             }
             if (password==null||password.length() < 1) {
              msg = new ActionMessage("error.notnull", password);
              errors.add("password", msg);
             }
            return errors;

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


          網站導航:
           
          主站蜘蛛池模板: 岳普湖县| 天祝| 繁昌县| 宜阳县| 内黄县| 时尚| 景德镇市| 栾城县| 武安市| 横峰县| 玛曲县| 高要市| 吴桥县| 肃北| 长兴县| 明溪县| 永新县| 林州市| 龙门县| 长春市| 孟村| 丰台区| 龙陵县| 静宁县| 琼海市| 镇赉县| 湘西| 大渡口区| 通化市| 白水县| 新田县| 扬中市| 曲靖市| 东光县| 进贤县| 金秀| 杭锦后旗| 前郭尔| 白山市| 洪湖市| 苍梧县|