Handling a response in a @Controller
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I have a simple spring controller which takes a @ModelAtrribute
object and registers an employee. If it is a success, I forward it to a success, else an error page, and I am doing this via a boolean return value from @Service
and Dao.
@Controller
:
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView registerEmployee(@ModelAttribute LoginModel loginModel)
LOGGER.info(" REGISTER Controller [] ",loginModel.toString());
ModelAndView model = new ModelAndView();
if(!registerService.registerEmployeeByAdmin(loginModel))
model.setViewName("register");
model.addObject("errorMessage", "Unable to register user, Please try again");
return model;
model.addObject("employee", loginModel);
return model;
@Service
:
public boolean registerEmployeeByAdmin(LoginModel loginModel)
LOGGER.info("Saving Employee login info ",loginModel.toString());
boolean registered = registerDao.registerEmployeeByAdmin(loginModel);
if(registered)
registerDao.registerEmployeeInfo(loginModel);
return registered; //the value returned
Dao:
public boolean registerEmployeeByAdmin(LoginModel loginModel)
boolean registered = false;
String addEmployeeQuery = "insert into employee_login (loginid,password_hash,user_created_date,"
+ "email) values (?,?,?,?);";
try (Connection conn = Database.getConnection(env);
PreparedStatement pstmt = conn.prepareStatement(addEmployeeQuery);)
if (conn != null)
pstmt.setInt(1, loginModel.getUserid());
pstmt.setString(2, EmployeeHelper.getHashFromPassword(loginModel.getPassword_hash()));
pstmt.setTimestamp(3, new java.sql.Timestamp(System.currentTimeMillis()));
pstmt.setString(4, loginModel.getEmail());
int rows = pstmt.executeUpdate();
if (rows > 0)
registered = true;
catch (SQLException
Is this the right way or can there be another way which is neat and clean?
java spring-mvc
add a comment |Â
up vote
0
down vote
favorite
I have a simple spring controller which takes a @ModelAtrribute
object and registers an employee. If it is a success, I forward it to a success, else an error page, and I am doing this via a boolean return value from @Service
and Dao.
@Controller
:
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView registerEmployee(@ModelAttribute LoginModel loginModel)
LOGGER.info(" REGISTER Controller [] ",loginModel.toString());
ModelAndView model = new ModelAndView();
if(!registerService.registerEmployeeByAdmin(loginModel))
model.setViewName("register");
model.addObject("errorMessage", "Unable to register user, Please try again");
return model;
model.addObject("employee", loginModel);
return model;
@Service
:
public boolean registerEmployeeByAdmin(LoginModel loginModel)
LOGGER.info("Saving Employee login info ",loginModel.toString());
boolean registered = registerDao.registerEmployeeByAdmin(loginModel);
if(registered)
registerDao.registerEmployeeInfo(loginModel);
return registered; //the value returned
Dao:
public boolean registerEmployeeByAdmin(LoginModel loginModel)
boolean registered = false;
String addEmployeeQuery = "insert into employee_login (loginid,password_hash,user_created_date,"
+ "email) values (?,?,?,?);";
try (Connection conn = Database.getConnection(env);
PreparedStatement pstmt = conn.prepareStatement(addEmployeeQuery);)
if (conn != null)
pstmt.setInt(1, loginModel.getUserid());
pstmt.setString(2, EmployeeHelper.getHashFromPassword(loginModel.getPassword_hash()));
pstmt.setTimestamp(3, new java.sql.Timestamp(System.currentTimeMillis()));
pstmt.setString(4, loginModel.getEmail());
int rows = pstmt.executeUpdate();
if (rows > 0)
registered = true;
catch (SQLException
Is this the right way or can there be another way which is neat and clean?
java spring-mvc
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a simple spring controller which takes a @ModelAtrribute
object and registers an employee. If it is a success, I forward it to a success, else an error page, and I am doing this via a boolean return value from @Service
and Dao.
@Controller
:
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView registerEmployee(@ModelAttribute LoginModel loginModel)
LOGGER.info(" REGISTER Controller [] ",loginModel.toString());
ModelAndView model = new ModelAndView();
if(!registerService.registerEmployeeByAdmin(loginModel))
model.setViewName("register");
model.addObject("errorMessage", "Unable to register user, Please try again");
return model;
model.addObject("employee", loginModel);
return model;
@Service
:
public boolean registerEmployeeByAdmin(LoginModel loginModel)
LOGGER.info("Saving Employee login info ",loginModel.toString());
boolean registered = registerDao.registerEmployeeByAdmin(loginModel);
if(registered)
registerDao.registerEmployeeInfo(loginModel);
return registered; //the value returned
Dao:
public boolean registerEmployeeByAdmin(LoginModel loginModel)
boolean registered = false;
String addEmployeeQuery = "insert into employee_login (loginid,password_hash,user_created_date,"
+ "email) values (?,?,?,?);";
try (Connection conn = Database.getConnection(env);
PreparedStatement pstmt = conn.prepareStatement(addEmployeeQuery);)
if (conn != null)
pstmt.setInt(1, loginModel.getUserid());
pstmt.setString(2, EmployeeHelper.getHashFromPassword(loginModel.getPassword_hash()));
pstmt.setTimestamp(3, new java.sql.Timestamp(System.currentTimeMillis()));
pstmt.setString(4, loginModel.getEmail());
int rows = pstmt.executeUpdate();
if (rows > 0)
registered = true;
catch (SQLException
Is this the right way or can there be another way which is neat and clean?
java spring-mvc
I have a simple spring controller which takes a @ModelAtrribute
object and registers an employee. If it is a success, I forward it to a success, else an error page, and I am doing this via a boolean return value from @Service
and Dao.
@Controller
:
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView registerEmployee(@ModelAttribute LoginModel loginModel)
LOGGER.info(" REGISTER Controller [] ",loginModel.toString());
ModelAndView model = new ModelAndView();
if(!registerService.registerEmployeeByAdmin(loginModel))
model.setViewName("register");
model.addObject("errorMessage", "Unable to register user, Please try again");
return model;
model.addObject("employee", loginModel);
return model;
@Service
:
public boolean registerEmployeeByAdmin(LoginModel loginModel)
LOGGER.info("Saving Employee login info ",loginModel.toString());
boolean registered = registerDao.registerEmployeeByAdmin(loginModel);
if(registered)
registerDao.registerEmployeeInfo(loginModel);
return registered; //the value returned
Dao:
public boolean registerEmployeeByAdmin(LoginModel loginModel)
boolean registered = false;
String addEmployeeQuery = "insert into employee_login (loginid,password_hash,user_created_date,"
+ "email) values (?,?,?,?);";
try (Connection conn = Database.getConnection(env);
PreparedStatement pstmt = conn.prepareStatement(addEmployeeQuery);)
if (conn != null)
pstmt.setInt(1, loginModel.getUserid());
pstmt.setString(2, EmployeeHelper.getHashFromPassword(loginModel.getPassword_hash()));
pstmt.setTimestamp(3, new java.sql.Timestamp(System.currentTimeMillis()));
pstmt.setString(4, loginModel.getEmail());
int rows = pstmt.executeUpdate();
if (rows > 0)
registered = true;
catch (SQLException
Is this the right way or can there be another way which is neat and clean?
java spring-mvc
edited Jan 13 at 8:16
Jamalâ¦
30.1k11114225
30.1k11114225
asked Jan 13 at 7:49
amol singh
84
84
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
-2
down vote
There is another much simpler clean way.
Use ControllerAdvice and throw error from Service Layer itself. Build your common error response there. Yeah it is global exception handling.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-2
down vote
There is another much simpler clean way.
Use ControllerAdvice and throw error from Service Layer itself. Build your common error response there. Yeah it is global exception handling.
add a comment |Â
up vote
-2
down vote
There is another much simpler clean way.
Use ControllerAdvice and throw error from Service Layer itself. Build your common error response there. Yeah it is global exception handling.
add a comment |Â
up vote
-2
down vote
up vote
-2
down vote
There is another much simpler clean way.
Use ControllerAdvice and throw error from Service Layer itself. Build your common error response there. Yeah it is global exception handling.
There is another much simpler clean way.
Use ControllerAdvice and throw error from Service Layer itself. Build your common error response there. Yeah it is global exception handling.
edited May 10 at 18:22
Sam Onela
5,88361545
5,88361545
answered May 10 at 18:02
Nishanthd
1
1
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185017%2fhandling-a-response-in-a-controller%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password