Browser shows stale JSP page, user has to refresh manually
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:
- The usual prefix for the core library is
c
, notcore
. 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
- taking absolute URLs (starting with
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>
精彩评论