六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 204|回复: 0

少了一点儿(客户端静态验证)

[复制链接]

升级  13.33%

18

主题

18

主题

18

主题

秀才

Rank: 2

积分
70
 楼主| 发表于 2013-2-8 00:35:39 | 显示全部楼层 |阅读模式
<!----><!----><!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"\@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:36.0pt;mso-footer-margin:36.0pt;mso-paper-source:0;}div.Section1{page:Section1;}--><!---->
这里主要是两个问题,一个是如何得到action的uri对应的classname,比较难,看了struts2源代码,找了好多方法,因为是annotation的方式验证,所以都不太成功,不知有没有找这个的代码。最后在Form.java里看到,evaluateExtraParamsServletRequest方法里,addParameter("actionClass",clazz),发现其实是可以直接得到classname的。在ValidtaionUtils里加代码(其实并不好,只是为了减少VM中的代码,毕竟VM里写逻辑并不太好写)<span style="" />

public String toJQueryValidRule(Annotation annotation) {<span style="" />

       String rule = "";<span style="" />

       String vname = annotation.annotationType().getSimpleName();<span style="" />

       String v = (String) Validators.get(vname);<span style="" />

       if (vname.equals("RequiredStringValidator")) {<span style="" />

           // RequiredStringValidator.没什么特殊的<span style="" />

           rule = rule + v + ": true";<span style="" />

       } else if (vname.equals("IntRangeFieldValidator")) {<span style="" />

           // min max<span style="" />

           <span style="" />

           rule = rule + "min:"<span style="" />

                  + ((IntRangeFieldValidator) annotation).min() + ",";<span style="" />

           rule = rule + "max:"<span style="" />

                  + ((IntRangeFieldValidator) annotation).max() + ",";<span style="" />

           rule = rule + v + ": true";<span style="" />

       }else if(vname.equals("RequiredFieldValidator")) {<span style="" />

           rule = rule + "required: true";<span style="" />

       }<span style="" />

       return rule;<span style="" />

    }

这里也只是根据思路的例子,只验证了required和intrange,其它的需要根据jquery再写。<span style="" />

而且这么写也不一定是好的方式,只是个思路吧。<span style="" />

 

其它的话,就是在页面头部加入jquery的js,用sitmesh加也行,这里就直接写了。三个部分:<span style="" />

<script src="/struts2/js/jquery.form.js"type="text/javascript"></script>

<script src="/struts2/js/jquery.validate.js"type="text/javascript"></script>

<script src="/struts2/js/messages_cn.js"type="text/javascript"></script>

这样,就可以中文了,如果不想再改什么的话,就不用写messages部分是,当然,它还可以用class的方式进行验证,不好加,但我想如何代码写得好的话,还可以减少不少代码。<span style="" />

 

这样,我们就可以用jquery或其它的框架,在浏览器端静态验证了。<span style="" />

 

当然,所有都只是个思路,还没有考虑以xml方式写validation的方式,以及前面说的用css的class名字来进行表示的方式,以及改变style以改变显示验证的样式什么的。<span style="" />
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表