How to handle back browser button problem using sp

2020-02-08 06:36发布

How to handle back browser button problem using spring?.

In my application user login properly and when user click on back button page state is not maintained. So do i maintain the page state even the user click on back button / forward button

Thanks

3条回答
淡お忘
2楼-- · 2020-02-08 06:59
再贱就再见
3楼-- · 2020-02-08 06:59

Configure an interceptor inside Servlet Context as this:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**/*"/>
        <beans:bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
            <beans:property name="cacheSeconds" value="0"/>
            <beans:property name="useExpiresHeader" value="true"/>
            <beans:property name="useCacheControlHeader" value="true"/>
            <beans:property name="useCacheControlNoStore" value="true"/>
        </beans:bean>
    </mvc:interceptor>
</mvc:interceptors>

Note: Don't forget to remove your browser cache while testing your application.

查看更多
beautiful°
4楼-- · 2020-02-08 07:02

Apparently the pages are been requested from the browser cache. You'll need to disable the client-side caching of the pages in question. You can do this by creating a Filter which listens on an url-pattern of the pages you'd like to disable the cache for, such as *.jsp. Do the following in the doFilter() method:

HttpServletResponse httpres = (HttpServletResponse) response;
httpres.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
httpres.setHeader("Pragma", "no-cache"); // HTTP 1.0.
httpres.setDateHeader("Expires", 0); // Proxies.
chain.doFilter(request, response);

This way, the client side application will be instructed to not cache the requests matching the url-pattern of this filter. Pressing the back button would then force a real request from the server, with the proposed fresh data. To retain certain server-side data between the requests, you'll need to grab the session scope or use GET requests only.

Oh, don't forget to clear the browser cache first after implementing and before testing ;)

查看更多
登录 后发表回答