利用DWR搭建ajax的校验层

利用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的配置。  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值