2013-07-28 46 views
0

關於如何將用戶重新定向回前一頁的任何想法都乾淨而穩健?ZK - 將用戶重定向回前一頁

我只關心重定向他們,如果上一頁在應用程序本身。我曾考慮過在用戶的會話中存儲某種DIY歷史記錄,但這看起來並不乾淨或不健全。我知道我們可以使用Executions.sendRedirect(url)重定向用戶,但我不確定是獲取上一頁網址的最佳方式。

ZK有它自己的Browser History Management但我不認爲這是在嘗試啓動真實瀏覽器重定向時使用。

回答

2

試試這個:

// check if request needs to be sent back 
if (!isValidRequest()) { 

    // get the current execution context 
    Execution exec = Executions.getCurrent(); 

    // retrieve HTTP request, response objects 
    HttpServletRequest request = (HttpServletRequest) exec.getNativeRequest(); 
    HttpServletResponse response = (HttpServletResponse) exec.getNativeResponse(); 

    // get the referring URL from HTTP header 
    String referrer = request.getHeader("Referer"); 

    /* don't need to use this unless doing a forward (server-side) 

    if (referrer != null) { 
    URL ref = new URL(referrer); 
    referrer = ref.getPath().substring(request.getContextPath().length()); 
    } else { 
    referrer = "/index.jsp"; // home page 
    } 
    */ 

    // HTTP 301 redirect (client-side); encoding ensures URL rewriting if required 
    response.sendRedirect(response.encodeRedirectURL(referrer)); 

    exec.setVoided(true); // no need to create UI; about to redirect 
} 
+0

你能解釋這裏發生了什麼?謝謝。 –

+0

@SeanConnolly請檢查代碼註釋。 –