2011-08-14 83 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会话中放置一个布尔值即可完成此测试。