Struts中的javascript验证.

Struts中的javascript验证.

1、login.jsp的页面
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ 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>test javascript</title>
 </head>
 <body>
  <html:form action="/login.do" onsubmit="return validateLoginForm(this)">
   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>
  <html:javascript formName="loginForm"/>
 </body>
</html>

2、在struts-config.xml配置动态FormBean(DynaActionForm)并加载插件
   <form-beans >
   
      <form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm">
      <form-property name="password" type="java.lang.String" />
      <form-property name="username" type="java.lang.String" />
    </form-bean>

  </form-beans>

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

3、建验证文件validation.xml (下面只允许为字母,test.username为资源文件的字符串)

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
<form-validation>
 <formset>
  <form name="loginForm">
   <field property="username" depends="required,mask">
     <arg0 key="test.username"></arg0>
     <var>
      <var-name>mask</var-name>
      <var-value>^[a-zA-Z]*$</var-value>
     </var>
   </field>
  </form>
 </formset>
</form-validation>

4、显示获得输入的内容show.jsp
    //用get取得DynaActionForm里面的信息!,属性名和config.xml中的属性名相同!
      //out.println(request.getAttribute("loginForm").getClass());
      DynaActionForm loginForm=(DynaActionForm)request.getAttribute("loginForm");
      out.println("username ="+loginForm.get("username"));
      out.println("password ="+loginForm.get("password"));  

你可能感兴趣的:(JavaScript,java,html,bean,struts,action)