Web tier: servlets, JSP, Web frameworks: How to tell action something wrong occurred in service layer
Hi,all I have an action retrieve data from web form and transform user's email and password to Service layer, if the email and password are validated, OK, this user is passed. But if reverse, how can I tell my action something wrong occurred ? Now, my solution is throw an Exception in Service layer with a message "No, you can not login". But I am confused with Java Exception, maybe runtime exception should use as my way, but I am not sure. If I design my service as : boolean isValidate(String email, String password), I always think it's not a good idea to return the logic to action and write if/else in my struts2 action. So, what is the good way? ps: including the scenario as follow I want to add a keyword to database, but a unit key existed, so, what is the best way to tell my action: "ok, the keyword you give me has already existed!". throw DuplicateResourceException or return other things? Hummm, confused. Thanks!
- Posted by: Haulyn Jason
- Posted on: September 09 2008 11:04 EDT
- RE : How to tell action something wrong occurred in service laye by Pankaj Jaiswal on September 19 2008 02:46 EDT
i think its not going to be a confusing issue. Yes, you are right that the action should be left away from logic present there at the db label like boolean return to the action layer. The best way is to throw the specific exception (e.g. LoginFailureException) from the database to action. and it should not be a run time exception, rather than it should a checked Exception with appropriate message. At the action, if you get this exception, means something went wrong at the db layer, hence show the UI specific message to the user. hope it goes well.