jsf 2 - How to customize database unique constraint error message in JSF -


i have name uni constraint, , when user registers duplicate name, throws exception:

caused by: java.sql.sqlintegrityconstraintviolationexception: duplicate entry 'john' key 'uk_t622yodh32su1s2wseebkimwp' 

in jsf view page, exception caught exception handler displayed user:

duplicate entry 'john' key 'uk_t622yodh32su1s2wseebkimwp' 

code:

public void register (student student) {    ...  catch (exception e) {             string errormessage = getrooterrormessage(e);             facesmessage m = new facesmessage(facesmessage.severity_error, errormessage, "registration unsuccessful");             facescontext.addmessage(null, m);         } }  private string getrooterrormessage(exception e) {          string errormessage = "registration failed.";         if (e == null) {             return errormessage;         }          throwable t = e;         while (t != null) {             errormessage = t.getlocalizedmessage();             t = t.getcause();         }          return errormessage;     } 

registration.xhtml:

<p>             <h:panelgrid columns="2">                 <h:commandbutton id="register"                     action="#{registrationcontroller.register(registrationview.student)}" value="register"                     styleclass="register" />                 <h:messages styleclass="messages" errorclass="invalid"                     infoclass="valid" warnclass="warning" globalonly="true" />             </h:panelgrid>         </p> 

the problem here that, displays exception error code, above. error message like:

the user name has been registered, please use one. thanks. 

this more user friendly, compared sql constraint violation error message. how achieve this?

i not sure mean "exception error code"?

in code explicitly putting java exception message in facesmessage. if want different displayed user, should write different facesmessage.

however, suggest check if user exists before try persist it. should cover of cases such simple pre-check(s):

public void register (student student) {     if(service.exists(student)) {         string message = "the user name has been registered, please use one. thanks.";         facesmessage m = new facesmessage(facesmessage.severity_error, message,                                           "registration unsuccessful");         facescontext.addmessage(null, m);     } else {         try {             service.create(student);         } catch (exception e) {             string errormessage = getrooterrormessage(e);             facesmessage m = new facesmessage(facesmessage.severity_error, errormessage,                                                "registration unsuccessful");             facescontext.addmessage(null, m);         }     } } 

beside able explicitly check integrity constraint violation in sqlexception, not able distinguish between different types of such integrity constraint violations way.

public static boolean isconstraintviolation(sqlexception e) {     return e.getsqlstate().startswith("23"); } 

you may check, whether error message contains string duplicate entry, not reliable.

i prefer first suggested attempt precheck. if user exists, add proper facesmassages facescontext. cover of cases. , in unlikely event, when 2 users register same username @ same time, display "unexpected error" user.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -