JSF 1.x generic exception handling

后端 未结 6 1055
耶瑟儿~
耶瑟儿~ 2021-02-02 14:25

If I have an exception in my business layer (e.g. a SQL exception in my JDBC connection bean), how can I propagate it with a custom message to a global error.jsp pa

6条回答
  •  情深已故
    2021-02-02 15:03

    JSF 1.x doesn't provide any implicit error handling of this type, though you can redirect to an error page using navigation rules (assuming a form post)...

    
    
    Handle a generic error outcome that might be returned
    by any application Action.
    
    Generic Error Outcome
    loginRequired
    /must-login-first.jsp
    
    

    ...or using a redirect...

    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext extContext = context.getExternalContext();
    String url = extContext.encodeActionURL(extContext
            .getRequestContextPath()
            + "/messages.faces");
    extContext.redirect(url);
    

    I recommend looking at the JSF specification for more details.

    Error messages can be placed on the request scope/session scope/url parameters, as you like.


    Assuming a Servlet container, you can use the usual web.xml error page configuration.

    
        java.lang.Exception
        /errorPage.faces
    
    

    In your backing bean, you can wrap and throw your checked exceptions in RuntimeExceptions.

    Some JSF implementations/frameworks will catch these errors (Apache MyFaces/Facelets), so you'll have to configure them not to.

提交回复
热议问题