我有一些过滤器,例如像“id”这样的参数来检查一些权利(用于加载一些内容)。这些过滤器应忽略所有ajax请求,例如每次小请求后都不需要检查权限(仅限页面加载)
问题是,当我执行ajax-request时,它会抛出一个空指针,因为我没有附加带有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;
}
}
我做错了吗?
答案 0 :(得分:1)
你正在以正确的方式行事。您也可以使用JSF API来检查PartialViewContext是否存在并且它是Ajax请求
if(FacesContext.getCurrentInstance().getPartialViewContext() !=null &&
FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest()) {
}