服务器端客户端字段状态更改
我在 stackoverflow 中看到了这个问题的一些变体,但我有一个特定的用例,我愿意输入。
我能得到的最简单的是: 1. 在网站上,我有一个带有两个输入字段的表单。当 A 填充“XYZ”时,B 应被禁用,在所有其他情况下,应启用它。 2. 这个“页面”也可以由客户端保存到数据库中。
因此,我需要能够在客户端选项卡退出时(简单、简单的 Javascript)以及首次加载此“表单”时呈现第二个禁用字段(简单,在 JSP 中,在加载页面时操纵字段的属性) 。
但是......我最终在两个地方编码了完全相同的逻辑。现在,考虑一下这是数百个字段和数十个逻辑条件所必需的,有时复杂到将 5 个字段组合在一起,一组特定值来执行此或操作等等。
底线,您会考虑什么方法在服务器端和客户端之间重用(尽可能多)面向 UI 的验证/字段状态更改(考虑到我不想对每个在字段中输入的用户使用 AJAX 调用。)。
谢谢
I've seen some variations of this question throughout stackoverflow, but I have a specific use case I'd appriciate inputs for.
The simplest I can get this to is:
1. On a web site, I have a form with two input fields. When A is populated with 'XYZ' B should be disabled, in all other cases it should be enabled.
2. This "page" can also be saved to the a DB by the client.
So, I need to be able to render the 2nd disabled field both at client tab-out (easy, simple Javascript) and when this "form" is firsy being loaded (Simple, in JSP manipulate the field's attribute when the page is loaded).
But... I ended up having the exact same logic coded in two place. Now, consider this is required for hundreds of fields and tens of logical conditions, some times as complex as combination of 5 fields together, set of specific values to do this or that, etc. etc..
Bottom line , what approach would you consider to reuse (as much as possible) UI oriented validaion/field state alterations between server side and client side (cosider that I do not want to use AJAX call with every user typing in a field.).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于组件的 MVC 框架(例如 JSF (JavaServer Faces))允许您在单个位置进行定义。这是特定用例的启动示例:
基本上就是这样。仅省略了 javabean 代码,但这与这里无关。确实,这里使用了 ajax,但是它特别便宜,而且基本上没有其他方法可以在服务器端和客户端上保持相同的状态。
上面的 XHTML (Facelets) 示例将生成适当的 HTML/JS 代码,该代码将在服务器端和客户端执行工作。唯一的缺点可能是学习曲线和现有页面的大量变化。
A component based MVC framework like JSF (JavaServer Faces) allows you to define that in a single place. Here's a kickoff example of the particular use case:
That's basically all. Only the javabean code is omitted, but that's not so relevant here. True, ajax is used here, but that's partcularly cheap and there's basically no other way to keep the same state on both the server and client side anyway.
The above XHTML (Facelets) example will generate the appropriate HTML/JS code which will do the work at both server and client side. The only disadvantage is maybe the learning curve and lot of changes in existing pages.
我的答案与 BalusC 的答案大致相同。
但首先澄清一下术语:如果您所做的只是启用/禁用 HTML 字段,我们通常会说这是客户端验证,无论它是在 Javascript 还是 JSP 中完成。
是的,在 JSP 情况下,禁用/启用字段的逻辑在服务器上运行,但由于它所做的只是设置一个字段供客户端显示,因此它往往属于“客户端验证”的一般描述
。客户端验证的替代(实际上是“除了”)是在表单字段被发布到服务器时验证表单字段。检查客户端是否确实遵守了您的验证规则(例如,他们可能会禁用 javascript,或者他们可能会使用像 firebug 这样的工具来重新启用您禁用的字段等)。
您可能已经知道所有这些,并且术语不是并不是超级重要,但我认为值得指出。
现在,回到你的问题。
我会在 javascript 中完成这一切,并忽略 JSP 中的逻辑。
您需要做的就是获取当他们跳出“A”时触发的 JavaScript 片段,并在页面加载时运行它。
在 JQuery 中,你会做类似的事情(未经测试)
My answer would be much the same a BalusC's.
But first a clarification of terminology: If all you are doing is enabling/disabling an HTML field, we would typically say that is client side validation, regardless of whether it is done in Javascript or JSP.
Yes, in the JSP case the logic to disable/enable the fields runs on the server, but since all it is doing is setting a field for the client to display, it tends to fall into the general description of "client side validation"
The alternative (well actually 'in addition to') to client side validation is to validate the form fields when they get POSTed to the server. That checks that the client has actually obeyed your validation rules (e.g. they might disable javascript, or they might use a tool like firebug to re-enable the field you disabled, etc.)
You may well know all of that already, and terminology isn't super important, but I thought it was worth pointing out.
Now, back to your question.
I would do it all in javascript, and ignore the logic in the JSP.
All you need to do is take the piece of javascript that fires when they tab out of "A", and run it when the page loads.
In JQuery, you'd do something like (untested)