Raghavender Reddy
Raghavender Reddy

Reputation: 180

How to Disable back button using Struts2

I am using struts.serve.static=true and struts.serve.static.browserCache=false, but the back button is working even after logout. When i click on the back button it is going to the previous screen. How do i solve this issue?

Upvotes: 2

Views: 5680

Answers (2)

Harshad Vyawahare
Harshad Vyawahare

Reputation: 608

Creating a custom interceptor to add the header to every response is an easier way than adding response.setHeader to every jsp (If you are using Struts2).

Please check this link for a beautiful example which works fine.

Upvotes: 1

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

The above constants will be used by S2 to tell browser if they need to cache static content.

struts.serve.static=true

Above property is used by FilterDispatcher

  • If true then Struts serves static content from inside its jar.
  • If false then the static content must be available at /struts

also struts.serve.static.browserCache=true is used by FilterDispatcher and will work only if struts.serve.static=true.

  • If true -> Struts will write out header for static contents such that they will be cached by web browsers (using Date, Cache-Content, Pragma, Expires) headers).
  • If false -> Struts will write out header for static contents such that they are NOT to be cached by web browser (using Cache-Content, Pragma, Expires headers) In short both these constants are a way to tell browser if it need to cache static content being provided by S2 or not.

Regarding browser back button we can not disable browser back button as its a part of Browser API and when you are hitting the back button browser is serving the content from its cache without hitting the server.

You can ask the browser not to cache the content by using cache control header but its upon the browser to respect them or not. use following code in your JSP

response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Expires", "0");

Alternatively you can create an Interceptor and configure it with your desired action so that the headers can be set. Please go through the following thread for more details as how to control the cache in S2

Upvotes: 2

Related Questions