How do I throw an error 404
if the ID was not found without sending a 302
redirect.
In my ViewScop I do a select and would like to return an error 404
with showing the content of the 404
error page (404.xhtml
).
I tried the following which gives me justs a 302
redirect to the 404.xhtml
:
@PostConstruct
public void initialize() {
data = service.select(id.getValue());
if (data == null) {
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().setResponseStatus(404);
context.responseComplete();
try {
context.getExternalContext().redirect("/404.xhtml");
} catch (IOException ex) {
}
}
}
Actually if I comment the redirect out, I get the correct error code, but still the called xhtml
file gets rendered.
What is the best approach here? There are a lot of answers in SO but I didn't find one which was working for me.
EDIT: Here some other answers from SO, which was suggested by @Ben where with I found the correct answer:
- How to "throw" JSF2 404 error? - just set's the 404 without showing the content of the 404 page.
- How to send a person to a 404 page if f:viewParam / converter returns null? - similar answer from
@BalusC, again doesn't show the 404 page which I defined in the
web.xml
. - How to throw 404 from bean in jsf - That is accurately what I was looking for!