基本上,我有一个users
表,并且我正在尝试通过利用Users pojo类中的getter进行表单的一些基本登录身份验证。它会引发空指针异常。
这里有一个jquery,它打包数据并将其发送到处理userlogin的控制器:
$.ajax({// defining the below function as ajax responsive//
url:'userlogin', // the function that process the mapped url name and matching type is going to receive the data//
type:'POST',
data:{user_email:email,user_password:pass},// function to get the value from jsp page and send it to mapped class function//
success: function(response){// if the backend process is success then the function will run by getting the response as its parameter//
alert(response.message);
}
});
这是处理该网址的控制器:
@RequestMapping(value="/userlogin", method=RequestMethod.POST)
public @ResponseBody ModelAndView validateuser(@Valid LoginForm loginForm, BindingResult result) {
if (result.hasErrors()) {
ModelAndView view =new ModelAndView("users");
return view;
}
boolean userExists = loginService.checkLogin(loginForm.getUser_email(),loginForm.getUser_password());
if(userExists){
ModelAndView view =new ModelAndView("user_loginsuccess");
return view;
}else{
ModelAndView view =new ModelAndView("users");
return view;
}
}
这里loginform
是我创建的pojo类,用于设置和获取登录凭据(user_name和user_password)。我还创建了两个接口api的“ loginapi
包下的LoginDao和Login Service,并在loginimpl
包下实现了它们。
这些接口的实现如下:
public class LoginServiceImpl implements LoginService{
@Autowired
private LoginDao loginDAO;
public void setLoginDAO(LoginDao loginDAO) {
this.loginDAO = loginDAO;
}
public boolean checkLogin(String user_email, String user_password){
System.out.println("In Service class...Check Login");
return loginDAO.checkLogin(user_email, user_password);
}
}
@Repository("loginDAO")
public class LoginDaoImpl implements LoginDao{
@Resource(name="sessionFactory")
protected SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession(){
return sessionFactory.openSession();
}
public boolean checkLogin(String user_email, String user_password){
System.out.println("In Check login");
Session session = sessionFactory.openSession();
boolean userFound = false;
//Query using Hibernate Query Language
String SQL_QUERY =" from Users as o where o.user_email=? and o.user_password=?";
Query query = session.createQuery(SQL_QUERY);
query.setParameter(0,user_email);
query.setParameter(1,user_password);
List list = query.list();
if ((list != null) && (list.size() > 0)) {
userFound= true;
}
session.close();
return userFound;
}
}
谁能解释为什么我得到空指针异常?是否有任何错误或我做错了什么? Spring Servlet试图传达什么?
我真的一直在这段代码中呆了很多天,如果有人对代码感兴趣,并就我做错的事情敲了我头,谢谢!!
答案 0 :(得分:0)
由于您没有添加异常堆栈跟踪,因此不确定它是否可以解决您的问题,但是根据您的问题,您@Service
缺少了LoginServiceImpl
您将获得private LoginDao loginDAO
的NullPointerException
@Service //need to add @service here
public class LoginServiceImpl implements LoginService{
@Autowired
private LoginDao loginDAO;//if not add @service,this line of code will throw NullPointerException
public void setLoginDAO(LoginDao loginDAO) {
this.loginDAO = loginDAO;
}
public boolean checkLogin(String user_email, String user_password){
System.out.println("In Service class...Check Login");
return loginDAO.checkLogin(user_email, user_password);
}
}