如何“抛出” JSF2 404错误? [英] How to "throw" JSF2 404 error?
问题描述
假设我有一个管理用户的应用程序。您可以添加新用户,删除它们,编辑详细信息等。每个用户都有na ID,并在URL上有详细页面,如下所示:
.... / user / detail.jsf?id = 123
现在,如果会发生什么ID为123的用户不存在?我认为自然反应将是404标准误差。当您在URL中输入一些拼写错误时(例如/user/dtail.jsf)与输出完全相同。所以问题是:有没有这样的方法?
或者这个反应(404)是否合适?
谢谢。
只需将验证器附加到id视图参数,如果验证失败,请在响应中设置错误代码404。 / p>
例如
考虑这个简单的Facelet:
< html xmlns =http://www.w3.org/1999/xhtml
xmlns:h =http://java.sun.com/jsf/ html
xmlns:f =http://java.sun.com/jsf/core>
< f:metadata>
< f:viewParam id =idname =idvalue =#{myBean.id}validator =#{myBean.validate}/>
< / f:元数据>
< h:body>
< h:outputText value =#{myBean.id}/>
< / h:body>
< / html>
以下支持bean:
@ManagedBean
@ViewScoped
public class MyBean {
private Long id;
public void validate(FacesContext context,UIComponent component,Object object){
//做一些验证
//如果失败:
context.getExternalContext() .setResponseStatus(404);
context.responseComplete();
}
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
}
Let's say that I have an application which manages users. You can add new user, delete them, edit detail etc. Each user has na ID and has detail page on URL like this:
..../user/detail.jsf?id=123
Now, what should happen if user with ID 123 does not exists? I think that natural reaction would be 404 standard error. Exactly the same as is outputed when you make some typo in URL (like /user/dtail.jsf). So the question is: is there such method?
Or maybe is this reaction (404) appropriate?
Thanks.
Just attach a validator to the id view parameter and if validation fails, set error code 404 on the response.
e.g.
Consider this simple Facelet:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:metadata>
<f:viewParam id="id" name="id" value="#{myBean.id}" validator="#{myBean.validate}"/>
</f:metadata>
<h:body>
<h:outputText value="#{myBean.id}"/>
</h:body>
</html>
And the following backing bean:
@ManagedBean
@ViewScoped
public class MyBean {
private Long id;
public void validate(FacesContext context, UIComponent component, Object object) {
// Do some validation
// And if failed:
context.getExternalContext().setResponseStatus(404);
context.responseComplete();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
这篇关于如何“抛出” JSF2 404错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!