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;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…