DAO代码
public List<UserBeans> authenticateUser(UserBeans userBean)
{
List<UserBeans> userlist =new ArrayList<UserBeans>();
UserBeans user = new UserBeans();
//@SuppressWarnings("deprecation")
java.sql.Connection con = null;
try {
con = dataSource.getConnection();
} catch (SQLException e1)
{
e1.printStackTrace();
}
try
{
java.sql.PreparedStatement pst=con.prepareStatement("select loginid, studentid, email from login " +
" where email = "+userBean.getemail()+" and password = "+userBean.getpassword());
ResultSet rs=pst.executeQuery();
while(rs.next())
{
user.setloginid(rs.getInt("loginid"));
user.setstudentid(rs.getInt("studentid"));
user.setemail(rs.getString("email"));
userlist.add(user);
}
rs.close();
}
catch(Exception e)
{
System.out.print(e);
}
return userlist;
}
控制器代码
@RequestMapping(value="/doLogin", method = RequestMethod.GET)
public ModelAndView doLoginn(@ModelAttribute @Valid UserBeans userBean,BindingResult result, HttpSession session)
{
ModelAndView view = null;
List<UserBeans> bean=new ArrayList<UserBeans>();
bean=eventlogins.authenticateUser(userBean);
if(!result.hasFieldErrors())
{
if(bean!=null && userBean.getemail()!=null && userBean.getpassword()!=null)
{
session.setAttribute("bean",bean);
System.out.println("username and password are correct");
view =new ModelAndView("studentdashboard");
}
else
{
view=new ModelAndView("login");
system.out.println("username and password are wrong");
}
}
return view;
}
如何检查电子邮件和密码是否正确。实际上,DAO将列表返回给控制器,然后将其与控制器代码中的电子邮件和密码匹配。如果电子邮件和密码正确,则用于学生仪表板,否则仅保留在登录页面中。