java - How to implemenet correct interaction between Controller and Service layer in Spring MVC -


i have designed web app spring mvc, spring security , hibernate. have controller interacts service layer:

@controller @requestmapping(value="/") public class initcontroller {      @autowired     private userservice userservice;       @autowired     private studentservice studentservice;      @initbinder     public void initbinder(webdatabinder databinder){         databinder.registercustomeditor(string.class, "studentgroup", new studentnameeditor());     }      @requestmapping(value = "/login", method = requestmethod.get)     public modelandview getloginform(){         return new modelandview("login");     }      @requestmapping(value = "/registration.html",method = requestmethod.get)     public modelandview getregistrationform(){         return new modelandview("registration");     }      @requestmapping(value = "/students.html",method = requestmethod.get)     public modelandview getgroupform(){         return new modelandview("searchstudents");     }       @requestmapping(value = "/getstudents.html",method = requestmethod.post)     public modelandview getstudents(@valid @modelattribute("student") student student,                                 bindingresult result){         if(result.haserrors()){             return new modelandview("searchstudents");         }else{             return studentservice.getstudentofgroup(student);         }      }      @requestmapping(value = "/registrationconfirm.html",method = requestmethod.post)     public modelandview registration(@valid @modelattribute("user") user user,                                 bindingresult result){         if(result.haserrors()){             return new modelandview("registration");         }else{             user saveduser = userservice.registeruser(user);             if(objects.isnull(saveduser)){                 modelandview modelandview = new modelandview("login");                 modelandview.addobject("resultregistration",                                          "success registration!");                 return modelandview;             }else{                 modelandview modelandview = new modelandview("registration");                 modelandview.addobject("resultregistration",                                          "user same login or password registered in system already");                 return modelandview;             }         }     } } 

service layer interacts dao layer:

@transactional(propagation = propagation.requires_new ) @service("userservice") public class userserviceimpl implements userservice {      @autowired     private userdao userdao;      @transactional(propagation = propagation.requires_new )     @override     public user registeruser(user user) {         user userwiththesamelogin = userdao.getuserbylogin(user.getuserlogin());         if(!objects.isnull(userwiththesamelogin)){             //if user same login registered             return userwiththesamelogin;         }else{             user userwiththesamepassword = userdao.getuserbyemail(user.getuseremail());             if(!objects.isnull(userwiththesamepassword)){                 //if user same email registered                 return userwiththesamepassword;             }             else{                 //if user's credentials unique                 userdao.saveuser(user);                 return null;             }         }      } } 

i return user service layer, when email or login existed, , null when saving successfull.

i think simple returning value - bad, not logic , not distinctly. advice me please best way notifying controller result of saving user. throwing exception , catching them in controller or returning status code service layer? , show example please. grateful

my suggestion returning boolean registeruser true if user can registered , false otherwise:

@transactional(propagation = propagation.requires_new ) @override public boolean registeruser(user user) {     user userwiththesamelogin = userdao.getuserbylogin(user.getuserlogin());     if(!objects.isnull(userwiththesamelogin)){         //if user same login registered         return false;     }     user userwiththesamepassword = userdao.getuserbyemail(user.getuseremail());     if(!objects.isnull(userwiththesamepassword)){         //if user same email registered         return false;     }     //if user's credentials unique     userdao.saveuser(user);     return true; } 

in controller have:

@requestmapping(value = "/registrationconfirm.html",method = requestmethod.post) public modelandview registration(@valid @modelattribute("user") user user,                             bindingresult result){     if(result.haserrors()){         return new modelandview("registration");     }     if(userservice.registeruser(user)){         modelandview modelandview = new modelandview("login");         modelandview.addobject("resultregistration", "success registration!");         return modelandview;     }     //if other cases didn't hold true, means registration failed     modelandview modelandview = new modelandview("registration");     modelandview.addobject("resultregistration",          "user same login or password registered in system already");     return modelandview; } 

this feels more readable , gets rid of null checks. hope feels "better" well.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -