包括在站点中的多个页面上持久检索的 json 数据...最佳实践?
在此站点上,每个页面都由多个 .jsp 文件组成。有一个包含大部分页面内容的主页,但导航由 nav.jsp 处理,顶部标题部分由 top-section.jsp、footer.jsp 等处理。
页面的内容由数据填充通过 json 从中间件服务器检索。对于主页(即包含大量页面特定数据的页面部分),数据是通过 ajax 调用检索的,该调用使用登录的用户 ID 来获取所需的内容。
我的问题是,如何在 top-section.jsp 中包含一些检索到的数据。此页面很简单(我在这里使用硬编码数据,但实际上此数据应该特定于登录用户):
<div id="userinfo">Harald Niven | <a href="nivenhelp?id=12345">Help</a></div>
每个页面中都包含这一点,因此我想获取名称(“Harald Niven”和帮助网址)一次,然后在网站的每个页面上使用它。假设与中间件的通信是通过返回 json 的 ajax 调用,那么最好的方法是什么?
当我为每个页面加载数据时,这很简单,我有一个用于该页面的自定义 js 文件来获取所需的数据,但是,这个顶部部分永远不会改变,因此每次执行 ajax 请求并解析响应似乎很浪费。 .. 我怎样才能抓住它一次并在用户登录的整个过程中保留它?
编辑:由于此应用程序的要求,会话/cookie 已退出,我无法写入数据库。只能以非常有限的方式读取它(前端人员访问数据库的任何请求都需要大量的批准和解释,所以如果我可以避免走这条路,我想)。
On this site each page is made up of multiple .jsp files. There is a main page that contains the bulk of the page content but then navigation is handled by nav.jsp, top title section is handled by top-section.jsp, footer.jsp, etc.
The content of the pages is populated by data retrieved via json from a middle ware server. For the main pages (i.e. the section of the page that contains the bulk of the page specific data) data is retrieved by an ajax call that uses the logged in users id to get what it needs.
My question, how do I include some retrieved data in the, say, top-section.jsp. This page is simply (and I'm using hard coded data here but in reality this data should be specific to the logged in user):
<div id="userinfo">Harald Niven | <a href="nivenhelp?id=12345">Help</a></div>
This little bit is included in each page so I would like to grab the name ("Harald Niven" and the help url) once and then use it across every page in the site. Assuming that communication with the middle ware is via ajax calls that return json, what would be the best way to do this?
When I load data for each page it is simple, I have a custom js file for that page that grabs the needed data, however, this top section never changes so it seems wasteful to do an ajax request and parse out the response every time... how can I grab this once and persist it across the entire time the user is logged in?
Edit: sessions/cookies are out due to requirements of this app and I cannot write to the database... only read from it in a very restricted manner (any requests by the front end folks for accessing the database require a huge pain in the butt amount of approval and explanation so if I can avoid going this route I want to).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@rg88,
您需要实现会话管理,理想情况下在所有网站中人们使用不同的数据库来跟踪用户。
因此,您需要将从 json 对象检索的用户登录信息与 sessionID 一起存储在跟踪数据库中。
否则,如果您想采用简单的方法,您可以将其存储在 cookie 中,但这不是一个好的做法。
@rg88 ,
You need to implement the session management, ideally in all the websites people use a different database to track the users.
So you need to store the user login info retrived from json object in a track database along with sessionID.
Otherwise if you want to go for simple approach , you can store it in the cookies which is not a good practice.