我有一些過濾器,像「id」這樣的參數來檢查一些權限(用於加載一些內容)。這些過濾器應該忽略所有的ajax請求,因爲例如在每個小請求後都不必檢查權限(僅在頁面加載時)JSF Ajax請求過濾器(應該被忽略!)
問題是,當我執行ajax請求時,它會拋出一個空指針,因爲我沒有追加ID與Ajax請求。我發現,它仍然有效,當我使用和它失敗,當我使用(都執行Ajax請求)。
這是我的過濾器:
public class ExtendedAccessFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
//ignore filter if it is an ajax-request (DOES NOT WORK if not p:commandButton!)
if(isAJAXRequest(req)){
chain.doFilter(request, response);
System.out.println("ABORT FILTER, AJAX");
return;
}
//Nullpointer thrown here (because no Id is submitted)
int requestedId = Integer.parseInt(request.getParameter("id"));
}
private boolean isAJAXRequest(HttpServletRequest request) {
boolean check = false;
String facesRequest = request.getHeader("Faces-Request");
if (facesRequest != null && facesRequest.equals("partial/ajax")) {
check = true;
}
return check;
}
}
難道我做錯了什麼?
你就不能只是做你的過濾器時'用request.getParameter(「ID」)!= null'? –
這是完全預期的行爲。畢竟,Ajax請求是請求,並且也將被過濾。 –
@simon;那麼所有其他頁面也會繞過這個機制。我只想要忽略ajax – Niko