form验证与动态验证框架

form验证返回ActionErrors.

动态验证则实现JAVASCTIPT语句的自动生成与表单验证.

两个项目分别演示TestS为动态验证,而TestS2为form验证

一动态验证:

不需要手动写ActionForm

<?xml version="1.0" encoding="ISO-8859-1" ?>

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


<struts-config>

    <form-beans>
    
    	<form-bean name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
    		<form-property name="name" type="java.lang.String" />
    		<form-property name="pwd" type="java.lang.String"/>
    	</form-bean>
    </form-beans>

    <action-mappings>
		<action path="/login" type="com.lwf.action.LoginAction" name="loginForm" input="/login.jsp">
		<forward name="success" path="/success.jsp"/>
		</action>
    </action-mappings>

    <message-resources parameter="ApplicationResources" />

  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/org/apache/struts/validator/validator-rules.xml,
               /WEB-INF/validation.xml"/>
  </plug-in>

</struts-config>

 

 

validation.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
     "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
     "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation>
    <formset>
        <form name="loginForm">
            <field
                property="name"
                depends="required">
                    <msg name="required" key="ale.msg.name"/>
            </field>
            <field
                property="pwd"
                depends="required">
                    <msg name="required" key="ale.msg.pwd"/>
            </field>
        </form>
    </formset>
</form-validation>

 

 

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html-el" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'input.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
 <html:javascript formName="loginForm"/>
  <body>
    This is my JSP page. <br>
	<html:form action="/login" onsubmit="validateLoginForm(this)">
		<html:text property="name"></html:text>
		<html:text property="pwd"></html:text>
		<html:submit value="submit"></html:submit>
	</html:form>
  </body>
</html>

 

注意,我们只在页面上加了:

 <html:javascript formName="loginForm"/>
<html:form action="/login" onsubmit="validateLoginForm(this)">

 

你可能感兴趣的:(apache,html,jsp,框架,struts)