2012-07-24 44 views
0

使用this解決方法我嘗試登出。使用servlet在Java EE應用程序中登錄用戶

servlet代碼:

@WebServlet(name = "LogoutServlet", urlPatterns = {"/logout"}) 
public class LogoutServlet extends HttpServlet { 
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(user.class); 

    @Override 
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // Destroys the session for this user. 
     if (request.getSession(false) != null) { 
      request.getSession(false).invalidate(); 
     } 

     // Redirects back to the initial page. 
     logger.warn(request.getContextPath()); 
     response.sendRedirect(request.getContextPath()); 

    } 
} 

查看代碼:

<h:form> 
     <h:commandButton value="Logout" action="/logout"/> 
</h:form> 

錯誤:

Unable to find matching navigation case with from-view-id '/Admin/appManager.xhtml' for action '/logout' with outcome '/logout' 

我不認爲servle t以「/ logout」url模式接受請求。我做了什麼不正確?

回答

1

在JSF中,action不是下一個要調用的servlet的URL。而不是它,它通過faces-config或直接從後臺bean定義導航規則。

該消息告訴您,您的應用沒有與您的.xhtml頁面的動作logout匹配。

,你有一個ManagedBeanBackingBean與方法logout()和返回的URL「再見」地址我會做類似

<h:commandButton value="Logout" action="#{backingBean.logout()}"/> 

注意:如果您想從servlet執行操作,您應該使用常規html標記(<a>,<button>而不是JSF組件)來鏈接到它。

+0

在所有頁面中使用具有註銷方法的單個支持bean是否公平?我可能有很多頁面,並且每個頁面調用相同的後備Bean方法 – kinkajou 2012-07-24 06:03:21

+0

是的。在任何頁面中,您都可以使用多個支持bean;無論是數據還是行動。相反,您可以使用任何頁面上的任何bean。 – SJuan76 2012-07-24 06:05:25

+0

+1 helpful :)你可以用標籤和servlet提供簡單的例子嗎? – kinkajou 2012-07-24 06:10:44

相關問題