서블릿 필터를 RESTORE_VIEW 단계 이전에 실행되는 JSF 2.0 단계 수신기로 다시 구현했습니다. 로직을 Phase Listener로 옮김으로써 AJAX 요청에 대한 리다이렉션을 처리하는 JSF의 기능을 이용할 수있었습니다. 기본적으로 JSF 2.0은 적절한 AJAX 응답을 생성하여 클라이언트 측에서 리디렉션을 발생시킵니다. 사용자가 로그인하지 않은 경우 명확하게하기 위해,이 메커니즘은 AJAX와 비 AJAX 요청에 대한 리디렉션을 수행 할 수있다 특히
, 그것은 다음과 같은 응답이 다시 전송됩니다.
<?xml version="1.0" encoding="utf-8"?>
<partial-response>
<redirect url="/contextpath/faces/ajax/redirecttarget.xhtml">
</redirect>
</partial-response>"
코드를 들어 위상 청취자 :
public PhaseId getPhaseId()
{
return PhaseId.RESTORE_VIEW;
}
public void afterPhase(PhaseEvent event)
{
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
HttpSession session = (HttpSession)ec.getSession(false);
if (session==null || session.getAttribute(IS_LOGGED_IN_INDICATOR) == null)
{
try
{
ec.redirect(LOGIN_PAGE_URL);
}
catch(IOException e)
{
// log exception...
}
}
}
위상 수신기가 더 적절합니까? – BestPractices
관련 항목 : http://stackoverflow.com/questions/9305144/using-jsf-2-0-facelets-is-there-a-way-to-attach-a-global-listener-to-all-ajax/9311920 # 9311920 – BalusC