论坛首页 入门技术论坛

在struts中使用validator构架对标单进行验证时有点小问题,请教各位高人。望指点!!

浏览 4053 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-08-02   最后修改:2008-11-21

小弟近来在struts中使用validator构架对标单进行验证时有点小问题,请教各位高人。望指点!!

下面是我建的validatorForm类:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package xsl.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorActionForm;

/**
 * MyEclipse Struts
 * Creation date: 08-02-2007
 *
 * XDoclet definition:
 * @struts.form name="validatorForm"
 */
public class ValidatorForm extends ValidatorActionForm {
 /*
  * Generated fields
  */

 /** username property */
 private String username;

 /** password property */
 private String password;

 /*
  * Generated Methods
  */

 /**
  * Method validate
  * @param mapping
  * @param request
  * @return ActionErrors
  */

 /**
  * Method reset
  * @param mapping
  * @param request
  */
 
 /**
  * Returns the username.
  * @return String
  */
 public String getUsername() {
  return username;
 }

 /**
  * Set the username.
  * @param username The username to set
  */
 public void setUsername(String username) {
  this.username = username;
 }

 /**
  * Returns the password.
  * @return String
  */
 public String getPassword() {
  return password;
 }

 /**
  * Set the password.
  * @param password The password to set
  */
 public void setPassword(String password) {
  this.password = password;
 }
}

下面是我建的validatorAction类;

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package xsl.struts.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 xsl.struts.form.ValidatorForm;

/**
 * MyEclipse Struts
 * Creation date: 08-02-2007
 *
 * XDoclet definition:
 * @struts.action path="/validator" name="validatorForm" input="/web/login.jsp" scope="request" validate="true"
 */
public class ValidatorAction 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) {
  ValidatorForm validatorForm = (ValidatorForm) form;// TODO Auto-generated method stub
  return mapping.findForward("success");
 }
}

下面是struts-config.xml文件的配置:

<?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="validatorForm" type="xsl.struts.form.ValidatorForm" />
  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="validatorForm"
      input="/web/login.jsp"
      name="validatorForm"
      path="/validator"
      scope="request"
     
      type="xsl.struts.action.ValidatorAction" >
      <forward name="success" path="/web/success.jsp"></forward>
     </action>
  </action-mappings>
 
  <message-resources parameter="xsl.struts.ApplicationResources" />
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> 
  <set-property property="pathnames"
  value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
  </plug-in>
</struts-config>

 下面是validation.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules 
Configuration 1.1.3//EN" "validator_1_1_3.dtd">
<form-validation>
<formset>
<form name="validatorForm">
<field property="username"
depends="required,mask,maxlength,minlength"
>
<arg0 key="username.required"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<arg1 name="minlength" key="${var:minlength}" resource="false"/>
<msg name="mask"
key="error.invalid"/>
<var>
<var-name>minlength</var-name>
<var-value>4</var-value>
</var>
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*$</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>9</var-value>
</var>
</field>
<field property="password"
depends="required,mask">
<arg0 key="password.required"/>
<msg name="mask"
key="error.password.invalid"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*$</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
下面是资源文件:

# Resources for parameter 'com.wk.struts.ApplicationResources'
# Project struts

username.required=username
password.required=password
username.length=5
error.password.invalid=the password can only have number and letter
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
error.invalid={0} is invalid
errors.maxlength={0} can not be greater than {1} characters.
errors.range={0} is not in the range {1} through {2}.

页面是:

<%@ page language="java"%>
<%@ 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 ValidatorForm form</title>
 </head>
 <body>
  <html:form action="/validator">
   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>

 请问各位,为什么不能进行验证,而直接跳转到success.jsp页面了呢?

   发表时间:2007-08-02  
好象没看到你添加validator的插件进去,
另外,试一下,使用继承ValidatorForm,不要继承ValidatorActionForm
0 请登录后投票
   发表时间:2007-08-03  
MISS -> validator的插件
0 请登录后投票
   发表时间:2007-08-03  
谢谢两位的回复,问题已经解决,但还是有个小问题:为什么不能继承validatorActionForm 呢?
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics