browser back + viewscope beans

前端 未结 2 1379
逝去的感伤
逝去的感伤 2020-12-16 01:02

What the problem is : What happens when clicking on the browser back button --> opens up a page whose viewscoped-managedbean is already destroyed --> submit

2条回答
  •  一整个雨季
    2020-12-16 01:04

    The disadvantage of disabling the browser cache of a page is that the user will see an browsers error page if he use browser back to navigate to previous page. So another solutions is to identify if the page comes from the server or from the browser cache using javascript:

    First create a simple backing bean which serves a unique id (in my case current system time):

    @Named("browserCacheController")
    @RequestScoped
    public class BrowserCacheController implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * Returns a unique increasing id for each request
     * @return
     */
    public long getCacheID() {
        return System.currentTimeMillis();
    }   
    }
    

    So now you can test if a page is served from the server or browser and redirecting the user if the current page comes from browser cache. See the following javascript code placed into a jsf page which should not be cached by browser:

    
    

    The script can also be placed into facelet to make the jsf code more clean.

提交回复
热议问题