There are a few things.
@Transactional @Repository("LoginDao") public class LoginDao { public void findByUname(Users loginForm) { System.out.println("Hi Dao" + loginForm.getUname()); Session session = my_sessionfactory.getCurrentSession(); Query query = session.getNamedQuery("loginUser.findByUname").setString("userid", loginForm.getUname()); Users loginObj = (Users)session.getNamedQuery("Users.findByUname").setParameter("id", loginForm.getUname());
At first, your LoginDao does not have a field named my_sessionfactory , at least not in the code you are showing. Next, there is no named query named loginUser.findByUname , there is a query Users.findByUname . Finally, you are trying to apply a Query object to a Users object that will never work
Change dao as follows (I also assumed that you want to return the result).
@Transactional @Repository("LoginDao") public class LoginDao { @Autowired private SessionFactory sessionFactory; public Users findByUname(Users loginForm) { System.out.println("Hi Dao" + loginForm.getUname()); Session session = sessionFactory.getCurrentSession(); Query query = session.getNamedQuery("Users.findByUname"); query.setString("userid", loginForm.getUname()); query.setString("password", loginForm.getPassword()); Users loginObj = (Users) query.uniqueResult(); return loginObj; } }
The named queries you use should be HQL queries, not SQL queries, so you need to rewrite your queries to include NOT property names and column names.
Verification does not work correctly, just include BindingResult in your method signature and pass it to the validator. Also, do not add a custom object to the model; it is already present.
@Controller @RequestMapping("/") public class SigninController { @Autowired private LoginService lsp; @RequestMapping(value = "/loginsuccess", method = RequestMethod.GET) public String loginSuccess(@ModelAttribute("loginForm") Users loginObj, BindingResult result, ModelMap model) { UserValidator userValidator = new UserValidator(); userVaidator.validate(result, loginObj); Long count = this.lsp.validateLogin(loginObj); model.addAttribute("message1", loginObj.getUname()); System.out.println("result error"+result.getAllErrors()); System.out.println("result error count"+result.getErrorCount()); if (result.hasErrors()) { return "signin"; } else { return "loginsuccess"; } }
Another solution would be to add a validator to the binder (this will allow you to simply add the @Valid annotation and spring will do the validation for you). To do this, add a method annotated with @InitBinder and add a validator.
@Controller @RequestMapping("/") public class SigninController { @Autowired private LoginService lsp; @RequestMapping(value = "/loginsuccess", method = RequestMethod.GET) public String loginSuccess(@ModelAttribute("loginForm") @Valid Users loginObj, BindingResult result, ModelMap model) { Long count = this.lsp.validateLogin(loginObj); model.addAttribute("message1", loginObj.getUname()); System.out.println("result error"+result.getAllErrors()); System.out.println("result error count"+result.getErrorCount()); if (result.hasErrors()) { return "signin"; } else { return "loginsuccess"; } } @InitBinder public void initBinder(WebDataBinder binder) { binder.setValidator(new UserValidator()); } }
As for checking for incorrect passwords, you should put the password in your request and get an object with username / password as part of the request.