Handling a response in a @Controller

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question



























    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?







    share|improve this question























      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?







      share|improve this question













      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?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 13 at 8:16









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Jan 13 at 7:49









      amol singh

      84




      84




















          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.






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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.






            share|improve this answer



























              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.






              share|improve this answer

























                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.






                share|improve this answer















                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.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited May 10 at 18:22









                Sam Onela

                5,88361545




                5,88361545











                answered May 10 at 18:02









                Nishanthd

                1




                1






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?