How to Disable back button using Struts2

前端 未结 2 455
余生分开走
余生分开走 2021-01-15 14:10

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 t

相关标签:
2条回答
  • 2021-01-15 14:36

    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

    • Handling-Browser-Back-Forward-Button-in-Struts2
    0 讨论(0)
  • 2021-01-15 14:53

    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.

    0 讨论(0)
提交回复
热议问题