为什么使用隐藏字段?
我经常看到 Web 应用程序中使用了很多隐藏字段。我所编写的代码是为了使用大量隐藏字段以及来回发送给它们的可见字段的数据值而编写的。虽然我不明白为什么使用隐藏字段。我几乎总能想出在不使用隐藏字段的情况下解决相同问题的方法。隐藏字段如何帮助设计?
谁能告诉我隐藏字段到底有什么好处?为什么使用隐藏字段?
I have always seen a lot of hidden fields used in web applications. I have worked with code which is written to use a lot of hidden fields and the data values from the visible fields sent back and forth to them. Though I fail to understand why the hidden fields are used. I can almost always think of ways to resolve the same problem without the use of hidden fields. How do hidden fields help in design?
Can anyone tell me what exactly is the advantage that hidden fields provide? Why are hidden fields used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
隐藏字段只是最简单的方法,这就是它们被大量使用的原因。
替代方案:
主要关注点
主要优点:
Hidden fields is just the easiest way, that is why they are used quite a bit.
Alternatives:
Main concerns:
Main advantages:
假设您要编辑一个对象。现在将 ID 放入隐藏字段会很有帮助。当然,您绝不能依赖该值(即确保用户在
插入
/更新
时拥有适当的权限)。尽管如此,这仍然是一个非常方便的解决方案。在可见字段(例如只读文本框)中显示 ID 是可能的,但会令用户烦恼。
将 ID 存储在会话/cookie 中是禁止的,因为它不允许同时打开多个编辑窗口并施加生命周期限制(会话超时会导致编辑操作中断,非常烦人)。
使用URL 是可以的,但是违反了设计规则,即修改数据时使用POST。此外,由于它对用户可见,因此会创建更丑陋的 URL。
Suppose you want to edit an object. Now it's helpful to put the ID into a hidden field. Of course, you must never rely on that value (i.e. make sure the user has appropriate rights upon
insert
/update
).Still, this is a very convenient solution. Showing the ID in a visible field (e.g. read-only text box) is possible, but irritating to the user.
Storing the ID in a session / cookie is prohibitive, because it disallows multiple opened edit windows at the same time and imposes lifetime restrictions (session timeout leads to a broken edit operation, very annoying).
Using the URL is possible, but breaks design rules, i.e. use POST when modifying data. Also, since it is visible to the user it creates uglier URLs.
我看到/使用的最典型用途是 ID 和其他实际上不需要出于任何其他原因出现在页面上的东西,除了在某个时刻需要发送回服务器之外。
-编辑,应该包含更多详细
信息-例如,您有一些想要更新的对象 - UI 发回值的集合,此时服务器可能知道也可能不知道“嘿,这是一个客户对象”因此,您向服务器发出请求并说“嘿,给我 ID 7”,现在您就拥有了系统所知道的客户对象。更新已被应用、验证,无论什么,现在您的 UI 都会获得完整的结果。
我想一个很好的借口/论据是使用 linq。尝试在 linq 中更新对象而不先从数据库获取它。它并不真正知道它是可以跟踪的东西,直到您获得完整的对象。
Most typical use I see/use is for IDs and other things that really don't need to be on the page for any other reason than its needed at some point to be sent back to the server.
-edit, should've included more detail-
say for instance you have some object you want to update -- the UI sends back a collection of values and the server at that point may or may not know "hey this is a customer object" so you fire off a request to the server and say "hey, give me ID 7" and now you have your customer object as the system knows it. The updates are applied, validated, whatever and now your UI gets the completed result.
I guess a good excuse/argument is using linq. Try to update an object in linq without getting it from the DB first. It has no real idea that it's something it can keep track of until you get the full object.
原因之一是在客户端代码(javascript)和服务器端之间传递数据的便捷方式。
heres one reason, convenient way of passing data between client code (javascript) and server side.
有很多有用的场景。
一种是在页面上“存储”一些不应该由用户输入的数据。例如,在生成页面时存储用户ID,然后该值将随表单自动提交回服务器。
另一种情况是安全。向页面添加一些隐藏令牌并检查其在服务器上是否存在。这将有助于识别表单是否是通过浏览器提交的,还是由刚刚发布到您网站上某个网址的机器人提交的。
There are many useful scenarios.
One is to "store" some data on a page which should not be entered by a user. For example, store the user ID when generate a page, then this value will be auto-submitted with the form back to the server.
One other scenario is security. Add some hidden token to the page and check its existence on the server. This will help identify whether a form was submitted via the browser or by some bot which just posted to some url on your site.
它将内容保留在 URL 之外(如查询字符串中),因此保持干净。它还将不一定需要在其中的内容保留在会话之外。
除此之外,我想不出还有太多其他好处。
It keeps things out of the URL (as in the querystring) so it keeps that clean. It also keeps things out of Session that may not necessarily need to be in there.
Other than that, I can't think of too many other benefits.
它们通常用于在交互过程中存储状态。可以使用 Cookie 来代替,但有些人禁用它们。还可以使用单个隐藏字段来指向服务器端状态,但会存在会话粘性问题。
They are generally used to store state as an interaction progresses. Cookies could be used instead, but some people disable them. Could also use a single hidden field to point at server-side state, but then there are session-stickiness issues.
如果您在表单中使用隐藏字段,则通过包含新控件会增加表单的负担。
如果不需要采取隐藏字段,则不应采取它,因为从安全角度考虑它不适合。使用隐藏字段不属于良好的编程范围。因为它也会影响应用程序的性能。
If you are using hidden field in the form, you are increasing the burden of form by including a new control.
If there is no need to take hidden field, you should't take it because it is not suitable on the bases of security point. using hidden field does not come under the good programming. Because it also affect the performance of application.