我有一些过滤器,例如抓取像"id"这样的参数来检查某些权利(用于加载某些内容)。这些过滤器应该忽略所有ajax请求,因为不需要在每个小请求之后检查权限(只在页面加载时)
问题是,当我执行ajax请求时,它会抛出一个空指针,因为我没有添加ajax请求的ID。我发现,它仍然工作,当我使用和它失败时,当我使用(都执行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;
}
}
我做错了什么吗?
你做得对。你也可以使用JSF API检查PartialViewContext是否存在并且它是一个Ajax请求
if(FacesContext.getCurrentInstance().getPartialViewContext() !=null &&
FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest()) {
}