修改密码环节无法获取staff对象信息
package com.imooc.sm.controller;
import com.imooc.sm.entity.Staff;
import com.imooc.sm.service.SelfService;
import org.apache.ibatis.reflection.SystemMetaObject;
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 {
//注意这里的页面表达 之前都是"../staff_list.jsp"
//这里"login.jsp"
//因为这里是一级目录
request.getRequestDispatcher("login.jsp").forward(request, response);
}
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//首先接收用户传递过来的值
String account =request.getParameter("account");
String password=request.getParameter("password");
//调用业务层的login方法,将返回给staff对象
Staff staff =selfService.login(account,password);
//如果成功返回一个staff 如果失败返回null
if (staff==null){
response.sendRedirect("toLogin.do");
}else{
//返回staff 存放在session中
HttpSession session=request.getSession();
//以大写的USER为键接收
session.setAttribute("USER",staff);
response.sendRedirect("main.do");
}
}
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//返回staff 存放在session中
HttpSession session=request.getSession();
//以大写的USER为键接收,退出系统将值赋为空
session.setAttribute("USER",null);
response.sendRedirect("toLogin.do");
}
public void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("index.jsp").forward(request, response);
}
//这个是再self/info.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)request.getAttribute("USER");
System.out.println(staff);
if (!staff.getPassword().equals(password)){
System.out.println("判断成功");
response.sendRedirect("../toChangePassword.do");
}else {
System.out.println("判断成功");
selfService.changepassword(staff.getId(),password1);
//然后进行跳转到新的页面
//response.sendRedirect("toLogin.do");
//如果直接跳转,session中还有当前用户的信息
//所以跳转到logout.do中
//response.sendRedirect("../logout.do");
//因为这里做的是再右下角的窗体中做的,再这部分中无法实现整体页面的跳转
//若如此做,左边和顶部的信息仍然再,将再右下角的窗口中重新打开一个登陆界面
//所以需要将整个网页全部重定向回登陆界面,这里要用到javascript
/*<script type="text/javascript">parent.location.href="../logout.do"</script>
* 这是一句客户端代码,表示定位到父节点
* 客户端代码需要再java中传递过去
* */
response.getWriter().print("<script type=\"text/javascript\">parent.location.href=\"../logout.do\"</script>");
}
}
}
正在回答
如下图所示,你调用错对象了,应该使用sesssion对象去获取USER,而不是request对象。祝学习愉快~
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星