struts 2 中的验证
请解决我对 Struts2 中的验证的以下困惑 -
1)我们可以通过 JavaScript 进行客户端和服务器端验证吗? 2)我们可以通过AJAX进行客户端和服务器端验证吗?
如果可能的话,哪一种有利于客户端验证,哪一种有利于服务器端?
Please solve my following confusions about validation in Struts2 -
1) Can we do client side and server side validation through JavaScript ?
2) Can we do client side and server side validation through AJAX ?
If possible then which one is beneficial for client side validation and which one for server side ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
客户端验证可以通过 Javascript 完成。 服务器端验证是通过验证框架完成的。 您可以通过 AJAX 进行验证,但这需要更多工作。
我们发现客户端验证最好留给简单的情况,例如缺少必填字段和非法字符。 任何比这更复杂的事情都需要 AJAX 和/或服务器端。 此外,我们发现有关 Struts 2 客户端验证的文档不太有用,因此您可能需要考虑在您选择的 Javascript 框架(Dojo、YUI、Scriptaculous JQuery 等)中进行验证。
Client side validation can be done through Javascript. Server side validation is done through the validation framework. You can do validation through AJAX but it is a bit more work.
We have found that client side validation is best left to simple cases such as missing mandatory fields and illegal characters. Anything more complex than that needs to be AJAX and/or server side. Also we found the documentation on the Struts 2 client side validation to be less than useful so you may want to consider the validation in your Javascript framework of choice (Dojo, YUI, Scriptaculous JQuery etc).
是的,您可以同时使用 javascript 和 ajax,并且经常会同时使用这两种方法 - 甚至是为了验证同一条数据。 (如果它通过了客户端验证,您随后会进行 ajax 验证。)
当您不希望需要验证的数据出现在每个人都可以看到的 javascript 中时,使用 ajax 的情况很明显。 客户端验证通常仅限于更简单的事情。
所有 ajax 的缺点是它通常会大大增加页面的复杂性。
Yes, you can use both javascript and ajax, and quite often you'll use both - even to validate the same piece of data. (If it passes client side validation you do ajax validation afterwards.)
There's a clear case for using ajax when you do not want the data you need to validate to be present in javascript, where everyone can see it. Client-side validations are usually limited to simpler things.
The disadvantage of all things ajax is that it often increases page complexity quite a lot.
你知道完整的休眠插件吗?
这个插件有一个很棒的验证功能...
http://cwiki。 apache.org/S2PLUGINS/full-hibernate-plugin.html
更多信息请访问 vaannila.com
Do you know Full Hibernate Plugin?
There is a great validation feature in this plugin...
http://cwiki.apache.org/S2PLUGINS/full-hibernate-plugin.html
more at vaannila.com
拥有 JavaScript 和服务器端验证是件好事。 原因是,如果用户在浏览器上禁用 javascript,那么您的客户端验证将无法工作,并且如果条目未经过验证,您的代码将引发异常。
Its good to have javascript and server side validations. The reason is that if a user disables javascript on their browser, then your client side validation would not work and exceptions would be thrown by your code if the entries are not validated.