概述
4.密码修改
4.1.编写前端页面
写项目,建议从底层向上写,思考功能和架构。
4.2.userDao接口
//修改当前密码
public int updatePwd(Connection connection,int id,String userPassword) throws Exception;
4.3.userDao的实现类
//修改当前用户密码
@Override
public int updatePwd(Connection connection, int id, String userPassword) throws Exception {
int i = 0;
PreparedStatement preparedStatement = null;
if (connection != null){
String sql = "update smbms_user set userPassword = ? where id = ?";
Object[] params = {userPassword,id};
i = Base.execute(connection, preparedStatement, sql, params);
}
Base.close(null,preparedStatement,null);
return i;
}
4.4.userService接口
//修改用户密码
public boolean updatePwd(int id, String userPassword) throws Exception;
4.5.userService的实现类
//修改当前用户的密码
@Override
public boolean updatePwd(int id, String userPassword){
boolean flag = false;
Connection connection = null;
try {
connection = Base.getConnection();
if (userDAO.updatePwd(connection,id,userPassword) > 0){
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
Base.close(connection,null,null);
}
return flag;
}
4.6.userServet
public class UserServlet extends HttpServlet {
//servlet:控制层,调用业务层代码
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//从session里面拿id
Object user = req.getSession().getAttribute(Constans.USER_SESSION);
String newpassword = req.getParameter("newpassword");
boolean flag = false;
//调用userService中的方法
if (user != null && !StringUtils.isNullOrEmpty(newpassword)){
UserService userService = new UserServiceImpl();
try {
flag = userService.updatePwd(((User) user).getId(), newpassword);
if (flag){
req.setAttribute("message","修改密码成功,请退出,使用新密码登录。");
//密码修改成功,移除session
req.getSession().removeAttribute(Constans.USER_SESSION);
}else {
req.setAttribute("message","修改密码失败。");
}
} catch (Exception e) {
e.printStackTrace();
}
}else {
req.setAttribute("message","新密码有问题。");
}
req.getRequestDispatcher("jap/pwdmodify.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4.7.优先使用Ajax修改密码
使用阿里巴巴的fastJson,导入包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
//servlet方法复用
public class UserServlet extends HttpServlet {
//servlet:控制层,调用业务层代码
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
if (method.equals("savepwd")&&method!=null){
this.updatePwd(req,resp);
}else if (method.equals("pwdmodify")&&method!=null){
this.pwdModify(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
//修改密码
public void updatePwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//从session里面拿id
Object user = req.getSession().getAttribute(Constans.USER_SESSION);
String newpassword = req.getParameter("newpassword");
boolean flag = false;
//调用userService中的方法
if (user != null && !StringUtils.isNullOrEmpty(newpassword)){
UserService userService = new UserServiceImpl();
try {
flag = userService.updatePwd(((User) user).getId(), newpassword);
if (flag){
req.setAttribute("message","修改密码成功,请退出,使用新密码登录。");
//密码修改成功,移除session
req.getSession().removeAttribute(Constans.USER_SESSION);
}else {
req.setAttribute("message","修改密码失败。");
}
} catch (Exception e) {
e.printStackTrace();
}
}else {
req.setAttribute("message","新密码有问题。");
}
req.getRequestDispatcher("jap/pwdmodify.jsp").forward(req,resp);
}
//验证旧密码
public void pwdModify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Object user = req.getSession().getAttribute(Constans.USER_SESSION);
String oldPassword = (String) req.getAttribute("oldPassword");
//万能的Map,结果集
Map<String,String> resultMap = new TreeMap<>();
if (user==null){//如果session失效了,登录过期
resultMap.put("result","sessionerror");
}else if (StringUtils.isNullOrEmpty(oldPassword)){//旧密码为空
resultMap.put("result","error");
}else {
String userPassword = ((User) user).getUserPassword();//数据库中用户的密码
if (userPassword.equals(oldPassword)){
resultMap.put("result","true");
}else {
resultMap.put("result","false");
}
}
resp.setContentType("application/json");
PrintWriter writer = resp.getWriter();
//JSONSArray,阿里巴巴的JSON工具类,把map类型转换为json类型传递给前端
//json格式={key:value}
writer.write(JSONArray.toJSONString(resultMap));
writer.flush();
writer.close();
}
}
5.用户管理实底层现
导入用户的工具类、用户列表页面导入:用户列表、分页条
5.1获取用户数量
5.1.1创建分页的类
public class PageSupporttest {
//当前页码-来自于用户输入
private int currentPageNo = 1;
//总数量(表)
private int totalCount = 0;
//页面容量
private int pageSize = 0;
//总页数-totalCount/pageSize(+1)
private int totalPageCount = 1;
public int getCurrentPageNo() {
return currentPageNo;
}
public void setCurrentPageNo(int currentPageNo) {
if(currentPageNo > 0){
this.currentPageNo = currentPageNo;
}
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if(totalCount > 0){
this.totalCount = totalCount;
//设置总页数
this.setTotalPageCountByRs();
}
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if(pageSize > 0){
this.pageSize = pageSize;
}
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public void setTotalPageCountByRs(){
if(this.totalCount % this.pageSize == 0){
this.totalPageCount = this.totalCount / this.pageSize;
}else if(this.totalCount % this.pageSize > 0){
this.totalPageCount = this.totalCount / this.pageSize + 1;
}else{
this.totalPageCount = 0;
}
}
}
5.1.2获取用户的数量
userDao
//根据用户名或者角色名查询当前所有用户的数量
public int getUserCount(Connection connection,String username,int userRole) throws Exception;
userDaoImpl
//根据用户名或者角色名查询当前所有用户的数量
@Override
public int getUserCount(Connection connection, String username, int userRole) throws Exception {
int i = 0;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
if (connection != null){
StringBuilder sql = new StringBuilder();
sql.append("select count(1) as count from smbms_user user,smbms_role role where user.userRole = role.id");
ArrayList<Object> list = new ArrayList<>();
if (StringUtils.isNullOrEmpty(username)){
sql.append(" and userName like ?");
list.add("%" + username + "%");//index = 0
}
if (userRole > 0){
sql.append(" and u.userRole = ?");
list.add(userRole);//index = 1
}
Object[] params = list.toArray();
resultSet = Base.execute(connection, preparedStatement, resultSet, sql.toString(), params);
if (resultSet.next()){
i = resultSet.getInt("count");//从结果集中获取数量
}
}
Base.close(null,preparedStatement,resultSet);
return i;
}
userService
//根据用户名或者角色名查询当前所有用户的数量
public int getUserCount(String username,int userRole);
userServiceImpl
//根据用户名或者角色名查询当前所有用户的数量
@Override
public int getUserCount(String username, int userRole) {
int count = 0;
Connection connection = null;
try {
connection = Base.getConnection();
count = userDAO.getUserCount(connection, username, userRole);
} catch (Exception e) {
e.printStackTrace();
}finally {
Base.close(connection,null,null);
}
return count;
}
5.2获取用户列表
userDao
//通过条件查询---userList
public List<User> getUserList(Connection connection,String username,int userRole,int currentPageNo,int pageSize) throws Exception;
userDaoImpl
public List<User> getUserList(Connection connection, String username, int userRole, int currentPageNo, int pageSize) throws Exception {
List<User> userslist = new ArrayList<>();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
if (connection != null){
//用户存储sql语句
StringBuilder sql = new StringBuilder();
sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id");
//用于存储参数
ArrayList<Object> list = new ArrayList<>();
if (!StringUtils.isNullOrEmpty(username)){
sql.append(" and u.userName like ?");
list.add("%" + username + "%");//index = 0
}
if (userRole > 0){
sql.append(" and u.userRole = ?");
list.add(userRole);//index = 1
}
//在数据中,分页使用,limit startIndex,pageSize;总数
sql.append(" order by creationDate DESC limit ?,?");
//1 0 -(5)
//2 5 -(5)
//3 10 -(5)
//当前记录号=(当前页-1)*页面大小
currentPageNo = (currentPageNo-1)*pageSize;
list.add(currentPageNo);
list.add(pageSize);
Object[] params = list.toArray();
resultSet = Base.execute(connection, preparedStatement, resultSet, sql.toString(), params);
while (resultSet.next()){
User user = new User();
user.setId(resultSet.getInt("id"));
user.setUserCode(resultSet.getString("userCode"));
user.setUserName(resultSet.getString("userName"));
user.setGender(resultSet.getInt("gender"));
user.setBirthday(resultSet.getDate("birthday"));
user.setPhone(resultSet.getString("phone"));
user.setUserRole(resultSet.getInt("userRole"));
user.setUserRoleName(resultSet.getString("userRoleName"));
userslist.add(user);
}
}
Base.close(null,preparedStatement,resultSet);
return userslist;
}
userService
//通过条件查询---userList
public List<User> getUserList(String queryUsername, int queryUserRole, int currentPageNo, int pageSize) throws IOException;
userServiceImpl
//通过条件查询---userList
@Override
public List<User> getUserList(String username, int userRole, int currentPageNo, int pageSize){
List<User> userList = new ArrayList<>();
Connection connection = null;
try {
connection = Base.getConnection();
userList = userDAO.getUserList(connection, username, userRole, currentPageNo, pageSize);
} catch (Exception e) {
e.printStackTrace();
}finally {
Base.close(connection,null,null);
}
return userList;
}
5.3获取角色列表
为了职责的统一,可以把角色的操作单独放到另一个包中。
RoleDAO
//获取角色列表
public List<Role> getRoleList(Connection connection) throws Exception;
RoleDOAImpl
//获取角色列表
@Override
public List<Role> getRoleList(Connection connection) throws Exception {
List<Role> roleList = new ArrayList<>();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
if (connection != null){
String sql = "select * from smbms_role";
Object[] params = {};
resultSet = Base.execute(connection, preparedStatement, resultSet, sql, params);
while (resultSet.next()){
Role role = new Role();
role.setId(resultSet.getInt("id"));
role.setRoleCode(resultSet.getInt("roleCode"));
role.setRoleName(resultSet.getString("roleName"));
roleList.add(role);
}
}
Base.close(null,preparedStatement,resultSet);
return roleList;
}
RoleService
//获取角色列表
public List<Role> getRoleList() throws Exception;
RoleServiceImpl
//获取角色列表
@Override
public List<Role> getRoleList(){
List<Role> roleList = null;
Connection connection = null;
try {
connection = Base.getConnection();
roleList = roleDAO.getRoleList(connection);
} catch (Exception e) {
e.printStackTrace();
}finally {
Base.close(connection,null,null);
}
return roleList;
}
5.4用户显示的Service
1.获取用户前端的数据(查询)
2.判断请求是否需要执行,查看参数的值判断
3.为了实现分页,需要计算出当前页面和总页数、页面大小
4.用户列表展示
//查询用户列表
public void query(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
UserServiceImpl userService = new UserServiceImpl();
RoleServiceImpl roleService = new RoleServiceImpl();
List<User> userList = null;
List<Role> roleList = null;
//从前端获取到的数据
String queryUsername = req.getParameter("queryname");
String temp = req.getParameter("queryUserRole");
String pageIndex = req.getParameter("pageIndex");
//这是为了让界面有一个默认值0,不会出错
int queryUserRole = 0;
//第一次请求,一定是第一页,且页面大小固定
int currentPageNo = 1;
int pageSize = Constants.pageSize;
//获取用户列表
if (queryUsername == null) {
queryUsername = "";
}
if (temp != null && !temp.equals("")) {
queryUserRole = Integer.parseInt(temp);//给查询赋值
}
if (pageIndex != null) {
try {
currentPageNo = Integer.parseInt(pageIndex);
} catch (NumberFormatException e) {
resp.sendRedirect("error.jsp");
}
}
//获取用户的总数(分页:上一页,下一页)
int totalCount = userService.getUserCount(queryUsername, queryUserRole);
//通过页面的类设置当前页面、总的数量、页面大小
PageSupport page = new PageSupport();
page.setCurrentPageNo(currentPageNo);
page.setPageSize(pageSize);
page.setTotalCount(totalCount);
//最大页数
int totalPageCount = page.getTotalPageCount();
//控制首页和尾页
//如果页面小于1,就显示第一页
if (currentPageNo < 1) {
currentPageNo = 1;
} else if (currentPageNo > totalPageCount) {
//如果页面大于最大页数,就显示最后一页
currentPageNo = totalPageCount;
}
//获取用户列表展示.角色列表
userList = userService.getUserList(queryUsername, queryUserRole, currentPageNo, pageSize);
roleList = roleService.getRoleList();
req.setAttribute("userList", userList);
req.setAttribute("roleList", roleList);
req.setAttribute("totalCount", totalCount);
req.setAttribute("currentPageNo", currentPageNo);
req.setAttribute("totalPageCount", totalPageCount);
req.setAttribute("queryUserName", queryUsername);
req.setAttribute("queryUserRole", queryUserRole);
//转发请求
req.getRequestDispatcher("userlist.jsp").forward(req, resp);
}
最后
以上就是紧张汽车为你收集整理的javaWeb学习-----smbms(修改密码,用户管理底层)的全部内容,希望文章能够帮你解决javaWeb学习-----smbms(修改密码,用户管理底层)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复