2011-08-14 86 views
0

我有一個授權框架顯示在每個頁面上,我想保持該頁面顯示,即使用戶將選擇登錄(使用jstl標籤,我會簡單地把這個框架用戶信息,並鏈接到購物大車)。我怎麼能做到這一點?我有一些想法,但他們都打破了我的控制器設計。重新加載JSP頁面

public class FrontController extends HttpServlet { 

private ActionContainer actionContainer = ActionContainer.getInstance(); 

public FrontController() { 
    super(); 
} 
/** 
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
@SuppressWarnings("unchecked") 
protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 

    String page = null; 

    try { 
     Action action = actionContainer.getAction(request); 
     page = action.execute(request, response); 
     RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(page); 
     dispatcher.forward(request, response); 

    } catch (ActionNotFoundException e) { 
     RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(PageNames.ERR_PAGE); 
     request.setAttribute(AttributeNames.ERR_MESSAGE_ATTRIBUTE, "Unknown Action command: " + e.getMessage()); 
     dispatcher.forward(request, response); 
    } catch (Exception e) { 
     RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(PageNames.ERR_PAGE); 
     request.setAttribute(AttributeNames.ERR_MESSAGE_ATTRIBUTE, "Exception:\n" + e.getMessage()); 
     dispatcher.forward(request, response); 
    } 
} 

@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
} 

@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
} 

/** 
* Returns a short description of the servlet. 
* @return a String containing servlet description 
*/ 
@Override 
public String getServletInfo() { 
    return "Front Controller"; 
} 

@Override 
public void init() throws ServletException { 
    super.init(); 
    Locale.setDefault(new Locale("ru","RU")); 
}  

}

我在想重定向到特別適用於這種情況下頁面,這將重定向到原來的頁面,或檢查網頁字符串空,從控制器原始頁面重新加載寫的,但我無法清楚地理解如何做到這一點。

回答

0

你的問題還不夠清楚。但我想你是問如何在用戶登錄後用其他(用戶名,歡迎消息和購物車詳細信息)替換頁面上的某個組件(登錄按鈕)。

如果我瞭解您的要求,那麼在用戶登錄後我會做什麼設置cookie(或localStorage中的值)。通過HttpServletResponse的addCookie方法將Cookie添加到Set-Cookie響應標頭中。這裏有一個例子:

Cookie userCookie = new Cookie("user", "uid1234"); 
    response.addCookie(userCookie); 

然後在你的控制器只需檢查「用戶」值設置與否,並採取適當的行動。 A tutorial on servlets with cookies

如果您需要處理前端組件,並且在不重新加載頁面的情況下更改表單上的值,我會建議使用javascript來執行此操作,您可以使用新的DOM元素(用戶名,歡迎消息,購物車等等)。

如果您的iFrame正在進行登錄,那麼在成功登錄時,請在頂部窗口中調用一個函數,該函數在從cookie中讀取更新值後進行更新。

如果你想在前端完全處理這個問題,比如Javascript,那麼我會跳過使用cookies並改用localStorage。在Stackover和Internet上有很多關於localStorage的幫助,但我會建議使用YUI Storage Lite庫,這使得從localStorage存儲和加載數據變得非常簡單。

Regards,

0

將當前URL包含爲驗證表單的隱藏字段。在處理認證的操作中,一旦用戶通過認證,重定向到此URL。

在JSP中,測試用戶是否已通過身份驗證幷包含身份驗證表單或購物車。一旦用戶通過身份驗證,只需在HTTP會話中放置一個布爾值即可完成此測試。