My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians


joi, 2 aprilie 2015

[OmniFaces Utilities (2.0)] Send a HTTP response error with the given status and message

[OmniFaces utilities] The responseSendError() method sends a HTTP response error with the given status and message. This will end up in either a custom <error-page> whose <error-code> matches the given status, or in a servlet container specific default error page if there is none. The message will be available in the error page as a request attribute with name javax.servlet.error.message. The FacesContext#responseComplete() will implicitly be called after sending the error. The HTTP response status is supposed to be in the range 4nn-5nn. You can use the constant field values of HttpServletResponse for this.

Note The caller should preferably not catch the IOException thrown by this method,but just re-declare it in the action method.The Servlet container will handle it.

Suppose that we access a page as: shop.xhtml?product=id, where id is the unique product identifier (e.g. a number between 1-100). We have this:

    <f:viewParam name="product"

 Now, this id must be validated before showing the product, and if it is an invalid product then return a 404 page error. For this, we can use the Faces#responseSendError(), as below:

import org.omnifaces.util.Faces;
@FacesValidator(value = "productValidator")
public class ProductValidator implements Validator {

  public void validate(FacesContext context, UIComponent component, Object value)
                                                       throws ValidatorException {

    int id = Integer.parseInt((String) value);
    if (check_id) {
        try {
            Faces.responseSendError(404, "The product with unique product identifier, " + id + " cannot be found !");
            // or using, HttpServletResponse#SC_NOT_FOUND
                "The product with unique product identifier, " + id + " cannot be found !");
        } catch (IOException ex) {
          throw new FacesException(ex);

How the user sees the error message ? Well, by default it will see it in a Servlet container specific default error page, as below:
Or, you may provide an error page and configure it in web.xml. A simple error.xhtml page:
  Error Desscription: #{facesContext.externalContext.requestMap.get("javax.servlet.error.message")}<br/>               
  Servlet Name: #{facesContext.externalContext.requestMap.get("javax.servlet.error.servlet_name")}

In web.xml, it must be configured as:


Now, the result of an invalid id will be:

Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors