利用ajax实现目前web程序中比较流行的MVC框架,就目前来说,在控制层自然少不了对View层的校验,ajax首先可以利用js进行客户端的验证,但是这样子代码比较的混乱,对于维护以及二次开发人员阅读是比较费劲的一件事情,所以我们要搭建一套基于java版本的ajax校验层,这样写代码对代码的维护以及层次的清晰性很好的得到控制,下面我们就利用DWR技术搭建ajax的校验层,他的原理是动态获取客户端客户的输入信息,在客户不知不觉的中完成对关键性字段的验证,从而实现基于服务器端的一种校验操作:
准备需要下载的jar包如下:
comons-validator-1.3.1.jar
jakart-oro-2.0.8.jar
bsf.jar
bsh.jsr
dwr2.0.jar
这些东西可以在apache的官方网站下载到,把这些部署到项目的class中,便可以利用comons-validator-1.3.1.jar框架强大的验证功能对表单进行验证了。
下面是在dwr配置文件中的描述:
<create javascript="urlval" creator="new">
<param name="class"
value="org.apache.commons.validator.UrlValidator" />
<include method="isValid"/>
</create>
这段代码实现在客户端对客户输入的网址的验证功能,启动Tomcat后就与DWR测试界面:如下
Methods For: urlval (org.apache.commons.validator.UrlValidator)
To use this class in your javascript you will need the following script includes:
<script type='text/javascript' src='/java/ajax/interface/urlval.js'></script>
<script type='text/javascript' src='/java/ajax/engine.js'></script>
In addition there is an optional utility script:
<script type='text/javascript' src='/java/ajax/util.js'></script>
Replies from DWR are shown with a yellow background if they are simple or in an alert box otherwise.
The inputs are evaluated as Javascript so strings must be quoted before execution.
There are 10 declared methods:
isValid( ); true
hashCode( );
(Warning: hashCode() is excluded: Method access is denied by rules in dwr.xml. See below)
getClass( );
(Warning: No Converter for java.lang.Class. See below)
(Warning: getClass() is excluded: Method access is denied by rules in dwr.xml. See below)
wait( );
(Warning: overloaded methods are not recommended. See below)
(Warning: wait() is excluded: Method access is denied by rules in dwr.xml. See below)
wait( , );
大家可以实现关于V框架中全部的校验API的配置。
准备需要下载的jar包如下:
comons-validator-1.3.1.jar
jakart-oro-2.0.8.jar
bsf.jar
bsh.jsr
dwr2.0.jar
这些东西可以在apache的官方网站下载到,把这些部署到项目的class中,便可以利用comons-validator-1.3.1.jar框架强大的验证功能对表单进行验证了。
下面是在dwr配置文件中的描述:
<create javascript="urlval" creator="new">
<param name="class"
value="org.apache.commons.validator.UrlValidator" />
<include method="isValid"/>
</create>
这段代码实现在客户端对客户输入的网址的验证功能,启动Tomcat后就与DWR测试界面:如下
Methods For: urlval (org.apache.commons.validator.UrlValidator)
To use this class in your javascript you will need the following script includes:
<script type='text/javascript' src='/java/ajax/interface/urlval.js'></script>
<script type='text/javascript' src='/java/ajax/engine.js'></script>
In addition there is an optional utility script:
<script type='text/javascript' src='/java/ajax/util.js'></script>
Replies from DWR are shown with a yellow background if they are simple or in an alert box otherwise.
The inputs are evaluated as Javascript so strings must be quoted before execution.
There are 10 declared methods:
isValid( ); true
hashCode( );
(Warning: hashCode() is excluded: Method access is denied by rules in dwr.xml. See below)
getClass( );
(Warning: No Converter for java.lang.Class. See below)
(Warning: getClass() is excluded: Method access is denied by rules in dwr.xml. See below)
wait( );
(Warning: overloaded methods are not recommended. See below)
(Warning: wait() is excluded: Method access is denied by rules in dwr.xml. See below)
wait( , );
大家可以实现关于V框架中全部的校验API的配置。