Web 应用程序,处理状态的最佳方式
我有一些粗略的页面,我必须为其存储一些状态信息,例如当前页面、每页记录、当前顺序、过滤条件,有时还有更多信息...
我想使用类似于休息风格的友好网址,类似于 http://microformats.org/wiki/rest/urls (GET 用于浏览,POST添加,PUT 编辑,DELETE 删除)
cookie 的问题是,如果我打开多个选项卡,所有选项卡都会共享相同的 cookie,这与会话相同,因为
如果我尝试, 会话 id 存储在 cookie 中为了将这些参数保留在网址中(类似于 GET /clients?page=1&len=10&sort=name&filter=smith),一旦我发出 POST,我就会丢失这些值,
另一个解决方案是存储状态关于隐藏输入,并始终发布带有这些隐藏输入的帖子,但在这种情况下,我不能使用 GET 进行查询...
那么,您如何处理 Web 呈现状态???
--
补充:
更具体地说
,我有一个临时页面,用户可以过滤、更改页面、页面长度和 排序顺序..
发出更新或插入后,如何检索前一页, 页面长度、排序顺序、条件过滤器(表示逻辑 状态)等...考虑到如果用户打开另一个选项卡 两个选项卡都可以使用相同的cookie...
我能想到的唯一解决方案是使用隐藏字段...
I have some crud pages for which I have to store some state information, like current page, records per page, current order, filter conditions, and sometimes way more information...
I'd like to use friendly urls similar to rest style, something like http://microformats.org/wiki/rest/urls (GET for browsing, POST to add, PUT to edit, DELETE to remove)
The problem with cookies is that if I open several tabs, all of them would share the same cookies, it's the same with the session because the session id is stored in a cookie
if I try to keep those params in the url (something like GET /clients?page=1&len=10&sort=name&filter=smith) as soon as I issue a POST I loose those values
the other solution would be to store the state on hidden inputs, and to always issue posts carrying around those hidden inputs, but in that case I can't use GET for queries...
so, how do you handle web presentation state???
--
added:
to be more specific
I have a crud page, the user can filter, change page, page length, and
sort order..
After issuing an update or insert, how can I retrieve the former page,
page length, sort order, criteria filters (that presentation logic
state), etc... taking into account that if the user opens another tab
both tabs would work with the same cookies...
the only solution I can think of is to use hidden fields...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您必须执行您的两项建议。
每当您在服务器端生成页面时,请确保页面上的任何表单都将参数作为隐藏值,并且所有链接都在 URL 中包含参数...
You'd have to do both of your suggestions, I think.
Whenever you generate a page server-side, make sure any of the forms on the page have your params as hidden values, and any links have the params in the URL...
如果您不需要 cookie 和会话,唯一的选择是将它们作为查询字符串参数(在 GET 上)和隐藏字段(在表单 POST 上)传递。
If you don't want cookies and sessions, your only option is to pass them around as query string parameters (on GETs) and hidden fields (on form POSTs).
澄清
为不同页面存储不同的键值对有什么问题?例如,对于页面
/product/4
您可以存储键product-4-order
、product-4-filter
等。或者我是错过你的意图吗?
编辑
另一种选择是不完全刷新当前页面并使用 AJAX 进行所有操作。不过,设置不会在会话之间保留。
on clarification
What's the problem with storing different key-value pairs for different pages? E.g., for page
/product/4
you can store keysproduct-4-order
,product-4-filter
, etc.Or am I missing your intention?
edit
Another alternative is to not refresh the current page completely and do all manipulations with AJAX. Settings won't be persisted between sessions, though.