急,急,急,请教高手struts验证的问题!

03-06-13 tony_2008

我的jsp文件如下:

<html:form action="/oa/fawen" focus="wenjianbiaoti" onsubmit="return validateFawenForm(this);">

</html:form>

<html:javascript formName="fawenForm"

dynamicJavascript="true"

staticJavascript="false"/>

<script language="Javascript1.1" src="staticJavascript.jsp"></script>

validator.xml文件中的片断如下:

<form name="fawenForm">

<field property="wenjianbiaoti" depends="required,minlength,maxlength">

<arg0 key="fawenForm.wenjianbiaoti"/>

<arg1 key="${var:minlength}" name="minlength" resource="false"/>

<arg2 key="${var:maxlength}" name="maxlength" resource="false"/>

<var>

<var-name>maxlength</var-name>

<var-value>20</var-value>

</var>

<var>

<var-name>minlength</var-name>

<var-value>6</var-value>

</var>

</field>

</form>

struts-config.xml文件中的片断如下:

<form-bean name="fawenForm" type="com.eway.oa.fawen.FawenForm"/>

<action-mappings>

<action input="fawen" name="fawenForm" path="/oa/fawen" scope="request"

type="com.eway.oa.fawen.FawenAction" validate="true">

<forward name="success" path="/oa/fawen/fawen.jsp" redirect="false"/>

</action>

</action-mappings>

jsp文件执行后已经生成了验证的脚本,生成的HTML代码如下:

<form name="fawenForm" method="post" action="/oa/fawen.do" onsubmit="return validateFawenForm(this);">

</form>

<script type="text/javascript" language="JavaScript">

<!--

var focusControl = document.forms["fawenForm"].elements["wenjianbiaoti"];

if (focusControl.type != "hidden") {

focusControl.focus();

}

// -->

</script>

<script type="text/javascript" language="Javascript1.1">

<!-- Begin

var bCancel = false;

function validateFawenForm(form) {

if (bCancel)

return true;

else

return validateMaxLength(form) && validateMinLength(form) && validateRequired(form);

}

function maxlength () {

this.aa = new Array("wenjianbiaoti", " cannot be greater than 20 characters.", new Function ("varName", "this.maxlength='20'; this.minlength='6'; return this[varName];"));

}

function minlength () {

this.aa = new Array("wenjianbiaoti", " cannot be less than 6 characters.", new Function ("varName", "this.maxlength='20'; this.minlength='6'; return this[varName];"));

}

function required () {

this.aa = new Array("wenjianbiaoti", " is required.", new Function ("varName", "this.maxlength='20'; this.minlength='6'; return this[varName];"));

}

//End -->

</script>

但是此脚本却不执行!请问高手这是怎么回事?

fishandfly
2003-06-13 19:45

你包含它的staticScript.jsp吗?

validator需要一个可以动态生成脚本的jsp,好像叫上面的名字,你看一下validator的例子,它的jsp中有!

另外你的代码没有贴全啊!

tony_2008
2003-06-13 20:11

<html:form action="/oa/fawen" focus="wenjianbiaoti" onsubmit="return validateFawenForm(this);">
此处是一个table
</html:form>
<html:javascript formName="fawenForm"
        dynamicJavascript="true"
         staticJavascript="false"/>
 <script language="Javascript1.1" src="staticJavascript.jsp"></script>


validation.xml文件的源代码如下:

<form name="fawenForm">
                <field property="wenjianbiaoti"  depends="required,minlength,maxlength">
                	<arg0   key="fawenForm.wenjianbiaoti"/>
                	<arg1   key="${var:minlength}" name="minlength"  resource="false"/>
                	<arg2   key="${var:maxlength}" name="maxlength"  resource="false"/>
                <var>
                    <var-name>maxlength</var-name>
                    <var-value>20</var-value>
                </var>
                <var>
                    <var-name>minlength</var-name>
                    <var-value>6</var-value>
                </var>
            </field>
        </form>


struts-config.xml文件配置如下:


//注:此处用的是静态form而不是动态的,因为我用了OJB去映射这个FORM
<form-bean name="fawenForm" type="com.eway.oa.fawen.FawenForm"/>

<action input="fawen" name="fawenForm" path="/oa/fawen" scope="request"
                type="com.eway.oa.fawen.FawenAction" validate="true">
            <forward name="success" path="/oa/fawen/fawen.jsp" redirect="false"/>
</action>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames"
                      value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>



运行jsp文件后生成的静态HTML代码如下:


<form name="fawenForm" method="post" action="/oa/fawen.do" onsubmit="return validateFawenForm(this);">
此处是生成的table
</form>
<script type="text/javascript" language="JavaScript">
  <!--
  var focusControl = document.forms["fawenForm"].elements["wenjianbiaoti"];

  if (focusControl.type != "hidden") {
     focusControl.focus();
  } 
  // -->
</script>

<script type="text/javascript" language="Javascript1.1"> 

<!-- Begin 

     var bCancel = false; 

    function validateFawenForm(form) {                                                                   
        if (bCancel) 
      return true; 
        else 
       return validateMaxLength(form) && validateMinLength(form) && validateRequired(form); 
   } 

    function maxlength () { 
     this.aa = new Array("wenjianbiaoti", " cannot be greater than 20 characters.", new Function ("varName", "this.maxlength='20'; this.minlength='6';  return this[varName];"));
    } 

    function minlength () { 
     this.aa = new Array("wenjianbiaoti", " cannot be less than 6 characters.", new Function ("varName", "this.maxlength='20'; this.minlength='6';  return this[varName];"));
    } 

    function required () { 
     this.aa = new Array("wenjianbiaoti", " is required.", new Function ("varName", "this.maxlength='20'; this.minlength='6';  return this[varName];"));
    } 


//End --> 
</script>


 <script language="Javascript1.1" src="staticJavascript.jsp"></script>
问题是:jsp文件运行后,点击提交按钮,直接就将数据插入的数据库了,而没有执行脚本校验?
<p>

banq
2003-06-13 20:32

你浏览器打开这个页面时,用查看源码,检查页面的Javascript有无

validateFawenForm这个函数?

fishandfly
2003-06-13 22:32

我看你的定义没什么问题啊!

你这样测试一下!

1。保存运行jsp后生成的html文件->a.html

2.修改a.html中的javascript

function validateFawenForm(form){                                                                
        if (bCancel) 
      return true; 
        else 
       alert("ok");
       return validateMaxLength(form) && validateMinLength(form) && validateRequired(form); 
   } 
<p>

然后在ie中直接显示a.html,提交form,如果能够显示ok提示框,证明你的校验的配置没有问题,2可以通过。否则检查你的校验配置,注意大小写,还有你用了静态的form,是否extends ActionForm.

3.如果2可以通过,那么错误应该在javascript上了,你打开staticJavascript.jsp,我记得里面仅有一个taglib执行,类似

<html:javascript ...dynamicJavascript="true" staticJavascript="false"/>
<p>

改成

<html:javascript ...dynamicJavascript="false" staticJavascript="true"/>
<p>

或者

<html:javascript .../>
<p>

试验一下,这样会生成所有的用于校验的javascript函数。

估计就是这两个原因吧!我碰到过第3种情况,也不晓得为什么(因为我的actionform和我的value object用了一个类,所以actionform中有很多没用的属性,我估计是他影响的,你好像也有同样的情况啊!)。

3Go 1 2 3 下一页