具体的错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
初学的时候这个问题非常奇怪,因为代码明明好好的,就是访问不到,报空指针异常。折腾了几个小时。发现是**@Autowired**注释的问题
在service层中:
@Service
public class UserServiceImpl implements UserService {
@Autowired //添加自动装配的注释
private UserMapper userMapper;
@Override
public User Login(String username) {
User user = userMapper.login(username);
return user;
}
}
在Controller层中:
@Controller
public class LoginController {
@Autowired //这里也要添加
private UserService userService = new UserServiceImpl();
@RequestMapping("/user/login")
public String login(@RequestParam("Username") String username, @RequestParam("Password") String password, Model model){
User user = userService.Login(username);
if (user != null && user.getPassword()== password){
return "dashboard";
}else{
return "index";
}
}
}