Mat*_*ndy 11 java ajax jsf http-headers jsf-2
我的验证器需要知道它是完整请求还是ajax请求.在我当前的解决方案中,我检查X-Requested-With元素的http请求标头:
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getRequest();
if (req.getHeader("X-Requested-With") != null) {
// do something
} else {
// do something else
}
...
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来实现这一目标?对于不同的浏览器/ javascript库,我的解决方案是否"安全"?
更新:
刚刚发现,如果ajax请求来自Primefaces组件库(标记),则仅存在X-Requested-With标头<p:ajax>.
如果我使用普通的JSF,它就不存在了<f:ajax>.所以我的方法不适用 <f:ajax>.
使用<f:ajax>有一个不同的标题:
Faces-Request:partial/ajax
Run Code Online (Sandbox Code Playgroud)
Osw提出的解决方案适用于<f:ajax> 和 <p:ajax>:
Osw*_*Osw 17
我不会依赖http标头.从来没有尝试过,但你可以做到以下几点:
PartialViewContext pvc = facesContext.getPartialViewContext();
if(pvc.isAjaxRequest()) {
// ...
} else {
// ...
}
Run Code Online (Sandbox Code Playgroud)
另一个选择是使用isPartialRequest()而不是isAjaxRequest()
| 归档时间: |
|
| 查看次数: |
8085 次 |
| 最近记录: |