下面我们重新设计我们的LogonForm 
改为 
public class LogonForm extends ValidatorForm/*ActionForm*/ 
我们注释掉ActionForm的Validate()函数。 
我们配置Struts-config.xml文件,在文件最后增加 
<plug-in className="org.apache.struts.validator.ValidatorPlugIn"> 
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" /> 
    </plug-in> 
  
下面我们修改validation.xml文件 
增加 
<form name="logonForm"> 
            <field 
                property="userName" 
                depends="required"> 
                    <arg0 key="userName" resource="false"/> 
             </field> 
         <field 
            property="password" 
            depends="required"> 
            <arg0 key="password" resource="false"/> 
             
         </field> 
        </form> 
同时在资源文件中增加 
errors.required={0} is required. 
  
重新启动程序,那么登陆程序服务器端验证就起作用了。 
下面我们来添加Client端的Javascript验证。 
  
修改Logon.jsp 
<%@ page language="java" contentType="text/html;charset=UTF-8"%> 
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>  
<%@ taglib uri="/tags/struts-html" prefix="html"%> 
<%@ page errorPage="error.jsp" %> 
  
<html>  
    <head> 
        <title><bean:message key="Logon"/></title> 
    </head> 
    <body> 
    <html:errors property="hbm.session.error"/> 
    <html:errors property="no.user"/> 
    <html:errors property="hbm.session.query"/> 
        <html:form action="/jsp/logon.do" focus="userName" 
         onsubmit="return validateLogonForm(this)"> 
          <bean:message key="Logon.UserName"/> : <html:text property="userName"/><html:errors property="userName"/></br> 
          <bean:message key="Logon.Password"/> : <html:text property="password"/><html:errors property="password"/></br> 
            <html:submit> 
                <bean:message key="Logon.Submit"/> 
            </html:submit> 
        </html:form> 
        <html:javascript dynamicJavascript="true" staticJavascript="true" formName=”logonForm”/> 
         
    <body> 
</html> 
  
  
OK 
下一此按Excel计划开始贯彻前期思想。 
  
  
下载Excel计划,本期源文件 
  
谢谢。  
 
  |