前端验证

来源:互联网 发布:淘宝全球购客服电话 编辑:程序博客网 时间:2024/05/23 00:04

=====================================

Login1.sjp

<%@ page language="java" pageEncoding="gb2312"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

 

<html>

    <head>

       <title>JSP for LoginForm form</title>

    </head>

    <body>

       <html:form action="/login">

           <bean:message key="info.input" arg0="account"/>

           <html:text property="account"/><html:errors property="account"/>         

           <br/>

           <bean:message key="info.input" arg0="password"/>

           <html:password property="password"/><html:errors property="password"/>

           <br/>

           <html:submit/><html:cancel/>

       </html:form><HR>

       <html:errors property="login"/>

    </body>

</html>

 

====================================

Login.jsp

 

<%@ page language="java" pageEncoding="gb2312"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

 

<html>

    <head>

       <title>JSP for LoginForm form</title>

    </head>

    <body>

       普通方法:

       <html:form action="/login">

           Please input account : <html:text property="account"/><br/>

           Please input password : <html:password property="password"/><br/>

           <html:submit/><html:cancel/>

       </html:form><HR>

       用到资源文件方法:

       <html:form action="/login">

           <bean:message key="info.input.account"/> <html:text property="account"/><br/>

           <bean:message key="info.input.password"/> <html:password property="password"/><br/>

           <html:submit/><html:cancel/>

       </html:form><HR>

       用到资源文件,传参数方法:

       <html:form action="/login">

           <bean:message key="info.input" arg0="account"/> <html:text property="account"/><br/>

           <bean:message key="info.input" arg0="password"/> <html:password property="password"/><br/>

           <html:submit/><html:cancel/>

       </html:form><HR>

       用到资源文件,传参数,用中文:

       <html:form action="/login">

           <bean:message key="info.input" arg0="账号" bundle="CH"/> <html:text property="account"/><br/>

           <bean:message key="info.input" arg0="密码" bundle="CH"/> <html:password property="password"/><br/>

           <html:submit/><html:cancel/>

       </html:form><HR>

    </body>

</html>

 

 

 

============================================

 

LoginForm.java

 

 

package prj7_1.form;

 

import javax.servlet.http.HttpServletRequest;

 

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

 

public class LoginForm extends ActionForm {

   

    private String password;

    private String account;

 

    //validate函数里面进行前端错误验证

    public ActionErrors validate(ActionMapping mapping,

           HttpServletRequest request) {

       ActionErrors errors = new ActionErrors();//错误集合,专门容纳ActionError,当内部有ActionMessage时,认为发生了前端错误

       if(account.length()==0){//账号为空

           ActionMessage error = new ActionMessage("error.null","account");//第一个参数是消息内容key,第二个参数是取代{0}的值

           errors.add("account",error);//第一个参数是对这个error加一个属性标记        

       }

       else if(account.length()>10 || account.length()<6 ){//账号长度不对

           ActionMessage error = new ActionMessage("error.length","account","6","10");

           errors.add("account",error);   

       }

       if(password.length()==0){//账号为空

           ActionMessage error = new ActionMessage("error.null","password");//第一个参数是消息内容key,第二个参数是取代{0}的值

           errors.add("password",error);//第一个参数是对这个error加一个属性标记       

       }

       else if(password.length()>10 || password.length()<6 ){//账号长度不对

           ActionMessage error = new ActionMessage("error.length","password","6","10");

           errors.add("password",error);  

       }

       return errors;

    }

 

    public void reset(ActionMapping mapping, HttpServletRequest request) {

       // TODO Auto-generated method stub

    }

 

    public String getPassword() {

       return password;

    }

 

    public void setPassword(String password) {

       this.password = password;

    }

 

    public String getAccount() {

       return account;

    }

 

    public void setAccount(String account) {

       this.account = account;

    }

}

 

=================================================

 

 

LoginAction.java

package prj7_1.action;

 

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;

import org.apache.struts.action.ActionMessage;

import org.apache.struts.action.ActionMessages;

 

import prj7_1.form.LoginForm;

 

public class LoginAction extends Action {

 

       public ActionForward execute(ActionMapping mapping, ActionForm form,

                     HttpServletRequest request, HttpServletResponse response) {

              LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub

              String account = loginForm.getAccount();

              if(account.equals("JohnKate")){

                     ActionMessages errors = new ActionMessages();

                     ActionMessage error = new ActionMessage("error.login",account);

                     errors.add("login",error);//第一个参数是对这个error加一个属性标记  

                     this.saveErrors(request, errors);    //errors保存起来

                     return mapping.getInputForward();

              }

              return null;

       }

}

=======================================

 

Struts-config.xm

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

 

<struts-config>

  <data-sources />

  <form-beans >

    <form-bean name="loginForm" type="prj7_1.form.LoginForm" />

 

  </form-beans>

 

  <global-exceptions />

  <global-forwards />

  <action-mappings >

  <!-- input表示当前端出现错误,要跳转到的页面 -->

    <action

      input="/login1.jsp"

      name="loginForm"

      path="/login"

      type="prj7_1.action.LoginAction" />

 

  </action-mappings>

 

 

  <!-- 默认的资源文件 -->

  <message-resources parameter="prj7_1.ApplicationResources" />

  <!-- 定义其他资源文件:特意指定资源文件key -->

  <message-resources key="CH" parameter="prj7_1.ApplicationCHResources"></message-resources>

</struts-config>

 

 

=========================================

 

ApplicationCHResources.properties

 

info.input=<font color=red>/u8bf7/u60a8/u8f93/u5165 {0} :</font>

 

 

 

===========================================

 

 

ApplicationResources.properties

 

 

# 这是资源文件:格式:key=value形式

info.input.account=Please input account :

info.input.password=Please input password :

#可以传参数,参数用{0},...{4}表示

info.input=<font color=red>Please input {0} :</font>

 

error.null={0} cannot be null!

error.length=length of {0} must between {1} and {2}!

error.login={0} is in black list!!