开发者

Browser shows stale JSP page, user has to refresh manually

开发者 https://www.devze.com 2023-04-12 09:54 出处:网络
I\'m working on a website in JSP. There is a page called Agency which is publicly visible and shows the details of an agency. However, when an \"Agency Admin\" is logged in, the same Agency page is re

I'm working on a website in JSP. There is a page called Agency which is publicly visible and shows the details of an agency. However, when an "Agency Admin" is logged in, the same Agency page is rendered with Add/Edit links to modify the data on the page.

The problem is this:

  • A user visits the Agency page (Not logged in at this point)

  • This user logs in as an Agency Admin and navigates to the agency page again

  • The agency page doesn't show t开发者_运维问答he Add/Edit links until the user manually refreshes the browser

This is what the link to the Agency page looks like:

<a href="agency.do?agencyId=<core:out value="${agency.agencyId}" />">
    <core:out value="${agency.name}" />
</a>

I've been using this as a workaround:

<a href="javascript:void(0)" onclick="javascript:window.location = 'home.do?agencyId=<core:out value="${agency.agencyId}" />&rnd=' + Math.random()">
    <core:out value="${agency.name}" />
</a>

I'm appending a random number to the URL using javascript. But using onclick instead of href doesn't feel like the right thing to do. What if the user wants to open the link in a new tab?

I thought this would be a fairly common problem. Any better solution to this?


Your browser or a proxy must cache the page. Try adding those headers to the response of the page which is cached and shouldn't be :

response.setDateHeader("Expires", 0);
response.setHeader("Cache-control", "no-store, no-cache, must-revalidate");
response.setHeader("Pragma", "no-cache");

Regarding the JSTL usage, I would have two remarks:

  1. The usual prefix for the core library is c, not core.
  2. A URL is supposed to be composed using the <c:url> tag. This tag takes care of

    • taking absolute URLs (starting with /) and pre-pending the context root of the app (/agency.do becomes /myApp/agency.do)
    • adding the session ID to the URL if URL rewriting is necessary
    • encoding the URL parameters correctly

For example:

<c:url var="agencyUrl" value="agency.do">
    <c:param name="agencyId" value="${agency.agencyId}" />
</c:url>
<a href="${fn:escapeXml(agencyUrl)}"><c:out value="${agency.name}" /></a>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号