麻烦老师帮我看下,为什么是空指针呢?
package com.imooc.sm.global;
import com.imooc.sm.entity.Log;
import com.imooc.sm.entity.Staff;
import com.imooc.sm.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Component
@Aspect
public class LogAdvice {
@Autowired
private LogService logService;
@After("execution(* com.imooc.sm.controller.*.*(..)) && !execution(* com.imooc.sm.controller.SelfController.*(..))")
public void operationLog(JoinPoint joinPoint) {
Log log = new Log();
log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
log.setOperation(joinPoint.getSignature().getName());
HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
HttpSession session = request.getSession();
Object obj = session.getAttribute("USER");
Staff staff = (Staff) obj;
log.setOperator(staff.getAccount());
log.setResult("成功");
logService.addOperationLog(log);
}
@AfterThrowing(throwing = "e", pointcut = "execution(* com.imooc.sm.controller.*.*(..))")
public void systemLog(JoinPoint joinPoint, Throwable e) {
Log log = new Log();
log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
log.setOperation(joinPoint.getSignature().getName());
HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
HttpSession session = request.getSession();
Object obj = session.getAttribute("USER");
Staff staff = (Staff) obj;
log.setOperator(staff.getAccount());
log.setResult(e.getClass().getSimpleName());
logService.addSystemLog(log);
}
@After("execution(* com.imooc.sm.controller.SelfController.login(..))")
public void loginLog(JoinPoint joinPoint) {
Log log = new Log();
log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
log.setOperation(joinPoint.getSignature().getName());
HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
HttpSession session = request.getSession();
Object obj = session.getAttribute("USER");
if (obj == null) {
log.setOperation(request.getParameter("account"));
log.setResult("失败");
} else {
Staff staff = (Staff) obj;
log.setOperator(staff.getAccount());
log.setResult("成功");
}
logService.addLoginLog(log);
}
@Before("execution(* com.imooc.sm.controller.SelfController.logout(..))")
public void logoutLog(JoinPoint joinPoint) {
Log log = new Log();
log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
log.setOperation(joinPoint.getSignature().getName());
HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
HttpSession session = request.getSession();
Object obj = session.getAttribute("USER");
if (obj == null) {
log.setOperation(request.getParameter("account"));
log.setResult("失败");
} else {
Staff staff = (Staff) obj;
log.setOperator(staff.getAccount());
log.setResult("成功");
}
logService.addLoginLog(log);
}
}
进不去个人中心哦
正在回答
空指针异常,既获用null去调方法,或属性。那么,同学看到的是哪行报空指针异常呢?
从同学的截图看,
obj为null。那么,我们是在哪里向域中存的呢?
是在下图中的红框位置。
建议同学看一下,是否在下图的红框位置向域中设置了user 呢?如果设置了,建议在前边打印一句输出语句,看看是否执行到这儿了。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
package com.imooc.sm.controller; import com.imooc.sm.entity.Staff; import com.imooc.sm.service.SelfService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; @Controller("selfController") public class SelfController { @Autowired private SelfService selfService; public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("login.jsp").forward(request, response); } public void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("index.jsp").forward(request, response); } public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String account = request.getParameter("account"); String password = request.getParameter("password"); Staff staff = selfService.login(account, password); if (staff == null) { response.sendRedirect("toLogin.do"); } else { HttpSession session = request.getSession(); session.setAttribute("USER", staff); response.sendRedirect("main.do"); System.out.println(staff); } } public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.setAttribute("USER", null); response.sendRedirect("toLogin.do"); } public void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("../info.jsp").forward(request, response); } public void toChangePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("../change_password.jsp").forward(request, response); } public void changePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String password = request.getParameter("password"); String password1 = request.getParameter("password1"); HttpSession session = request.getSession(); Staff staff = (Staff) session.getAttribute("USER"); if (!staff.getPassword().equals(password)) { response.sendRedirect("toChangePassword.do"); } else { selfService.changePassword(staff.getId(), password1); response.sendRedirect("../logout.do"); response.getWriter().print("<script type=\"text/javascript\">parent.location.href=\"../logout.do\"</script>"); ; } } }
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星