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:
假设我有一个管理用户的应用程序。你可以添加新用户,删除他们,编辑细节等等。每个用户都有一个na ID,在URL上有这样的详细页面:
..../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?
现在,如果ID为123的用户不存在会发生什么?我认为自然反应是404标准误差。与您在URL(如/user/dtail.jsf)中输入错误时输出的值完全相同。所以问题是:有这样的方法吗?
Or maybe is this reaction (404) appropriate?
或者这个反应(404)合适吗?
Thanks.
谢谢。
3 个解决方案
#1
14
Just attach a validator to the id view parameter and if validation fails, set error code 404 on the response.
只需将验证器附加到id视图参数,如果验证失败,则在响应上设置错误代码404。
e.g.
如。
Consider this simple Facelet:
考虑一下这个简单的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:
和下面的后备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;
}
}
#2
1
(I ended up here searching for something similar but here's another pattern I used on a similar problem)
(我最后在这里寻找类似的东西但这里有我在类似问题上使用的另一种模式)
The Validation/ExternalContext response above is a very good way to handle it, alternatively (since you are already inside the context) you can handle the error when parsing in the parameters from the request and deal with it internally. I think it is more of how you want to handle it in your flow than a "here's a better solution"
上面的Validation/ExternalContext响应是一种非常好的处理方法,或者(因为您已经在上下文中)您可以在从请求解析参数时处理错误,并在内部处理错误。我认为这更多的是你想如何在你的流程中处理它,而不是“这是一个更好的解决方案”
//Inside "SomeManagedBean"
public String getParam()
{
String value = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("key");
if(value == null)
return "key not Exist";
else
return value;
}
//JSF 2.0 Source (something.xhtml) ... ...
//JSF 2.0源代码(某些东西。xhtml)……
I think the above is generally easier to work with inside the framework (you don't have to send out to an error page and disrupt the flow), but really it is simply an architectural decision. Both solutions are similar, it is just a question of breaking the flow or internal handling. Either way the ExternalContext is your friend.
我认为在框架中使用上面的内容通常更容易(您不必发送到错误页面并中断流),但实际上这只是一个架构上的决定。两种解决方案都是相似的,这只是一个打破流或内部处理的问题。不管怎样,外部环境是你的朋友。
#3
1
As mentioned, you can get the context from within a managed bean. I tried the validator approach, but for me it wasn't applicable since I was using two parameters to initialize my bean, and I do want to throw a 404. For me .setResponseStatus didn't throw the 404-page. Just gave a non-informative browser page. So I used the responseSendError instead.
如前所述,您可以从托管bean中获取上下文。我尝试了验证器方法,但是对我来说它不适用,因为我使用了两个参数来初始化我的bean,而且我确实想抛出一个404。对于我来说,setresponsestatus并没有抛出404页。只是提供了一个非信息性的浏览器页面。我用responseSendError代替。
#1
14
Just attach a validator to the id view parameter and if validation fails, set error code 404 on the response.
只需将验证器附加到id视图参数,如果验证失败,则在响应上设置错误代码404。
e.g.
如。
Consider this simple Facelet:
考虑一下这个简单的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:
和下面的后备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;
}
}
#2
1
(I ended up here searching for something similar but here's another pattern I used on a similar problem)
(我最后在这里寻找类似的东西但这里有我在类似问题上使用的另一种模式)
The Validation/ExternalContext response above is a very good way to handle it, alternatively (since you are already inside the context) you can handle the error when parsing in the parameters from the request and deal with it internally. I think it is more of how you want to handle it in your flow than a "here's a better solution"
上面的Validation/ExternalContext响应是一种非常好的处理方法,或者(因为您已经在上下文中)您可以在从请求解析参数时处理错误,并在内部处理错误。我认为这更多的是你想如何在你的流程中处理它,而不是“这是一个更好的解决方案”
//Inside "SomeManagedBean"
public String getParam()
{
String value = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("key");
if(value == null)
return "key not Exist";
else
return value;
}
//JSF 2.0 Source (something.xhtml) ... ...
//JSF 2.0源代码(某些东西。xhtml)……
I think the above is generally easier to work with inside the framework (you don't have to send out to an error page and disrupt the flow), but really it is simply an architectural decision. Both solutions are similar, it is just a question of breaking the flow or internal handling. Either way the ExternalContext is your friend.
我认为在框架中使用上面的内容通常更容易(您不必发送到错误页面并中断流),但实际上这只是一个架构上的决定。两种解决方案都是相似的,这只是一个打破流或内部处理的问题。不管怎样,外部环境是你的朋友。
#3
1
As mentioned, you can get the context from within a managed bean. I tried the validator approach, but for me it wasn't applicable since I was using two parameters to initialize my bean, and I do want to throw a 404. For me .setResponseStatus didn't throw the 404-page. Just gave a non-informative browser page. So I used the responseSendError instead.
如前所述,您可以从托管bean中获取上下文。我尝试了验证器方法,但是对我来说它不适用,因为我使用了两个参数来初始化我的bean,而且我确实想抛出一个404。对于我来说,setresponsestatus并没有抛出404页。只是提供了一个非信息性的浏览器页面。我用responseSendError代替。