java - Catching the IllegalArgumentException thrown by PropertyEditors in Spring -


i have propertyeditor in order translate ids persons, it's setastext (string text) follows:

public void setastext(string text) throws illegalargumentexception {     try {         int id = integer.parseint(text);         person person = peopleservice.get(id);         this.setvalue(person);     }     catch (numberformatexception ex) {         // ...         throw new illegalargumentexception("not number!: " + text);     }     catch (personnotfoundexcetion ex) {         // ...         throw new illegalargumentexception("impossible person: " + text);     } } 

and peoplecontroller has method follows:

@requestmapping("/getperson") public void ver (@requestparam person person, model model) {     model.addattribute (person);     // ... } 

i want catch illegalargumentexception in order show friendly message user, such "sorry, person looking isn't here", don't know that...

thanks!

general exception handling can done in way:

@exceptionhandler(exception.class) @responsestatus(httpstatus.internal_server_error) public string handleallexceptions(exception e) {   return "redirect:/error.html"; /* use correct view name */ } 

more specfic use bindingresult

@requestmapping(value = "/datedata", method = requestmethod.post) public string create(     @modelattribute("datedata") final datedata datedata,     final bindingresult result) {      if (result.haserrors()) {         return "datedata/create";     } else {         ...         return "myview";     }  } 

but guess works "forms" (modelattribute)

in humble opinion not idea let spring handle validaten of user input property editors. recommend use form way: build command object string field use validator on it.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -