云笔记的项目分析(5):个人中心:修改用户信息
作者:互联网
<button type="submit" id="btn" class="btn btn-success" >修改</button>
设置按钮的属性为submit,作为表单提交
<form id="myform" class="form-horizontal" method="post" action="user" enctype="multipart/form-data" >
<div class="form-group">
<input type="hidden" name="action" value="updateInfo">
<label for="nickName" class="col-sm-2 control-label">昵称:</label>
<div class="col-sm-3">
<input class="form-control" name="nick" id="nickName" placeholder="昵称" value="${user.nick }">
</div>
<label for="img" class="col-sm-2 control-label">头像:</label>
<div class="col-sm-5">
<input type="file" id="img" name="img">
</div>
</div>
<div class="form-group">
<label for="mood" class="col-sm-2 control-label">心情:</label>
<div class="col-sm-10">
<textarea class="form-control" name="mood" id="mood" rows="3">${user.mood }</textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="btn" class="btn btn-success" >修改</button>
<span style="color:red" id="msg">${resultInfo.msg }</span>
</div>
</div>
</form>
表单提交方式:method="post",设置可以文件上传,multipart/form-data 设置了隐藏域 <input type="hidden" name="action" value="updateInfo">
@MultipartConfig
文件上传servlet层必须要设置
Servlet层:
else if ("updateInfo".equals(action)){
// 修改用户信息
updateInfo(request, response);
}
private void updateInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.调用service的方法,传request参数,返回resultInfo对象
ResultInfo<User> resultInfo = userService.updateInfo(request);
//2.将resultInfo对象存到request作用域中
request.setAttribute("resultInfo", resultInfo);
//3.设置动态包含的页面值
request.setAttribute("changePage", "user/info.jsp");
//4.请求转发跳转到index.jsp
request.getRequestDispatcher("index.jsp").forward(request, response);
}
UserService层:
public ResultInfo<User> updateInfo(HttpServletRequest request) {
ResultInfo<User> resultInfo = new ResultInfo<>();
// 1、从session作用域中获取用户对象,得到userId
User user = (User) request.getSession().getAttribute("user");
Integer userId = user.getUserId();
// 2、接收参数(昵称、心情)
String nick = request.getParameter("nick");
String mood = request.getParameter("mood");
// 3、判断昵称是否为空
if (StringUtil.isEmpty(nick)) {
// 为空,code=0,msg=昵称不能为空,返回resultInfo对象
resultInfo.setCode(0);
resultInfo.setMsg("用户昵称不能为空!");
return resultInfo;
}
// 4、调用Dao层,查询昵称是否被其他用户占用,返回user对象
User u = userDao.findUserByNick(userId, nick);
因为用户昵称和用户Id是唯一的,修改前先判断昵称是否唯一
// 3、如果user对象存在
if (u != null) {
resultInfo.setCode(0);
resultInfo.setMsg("用户昵称已存在,不可使用!");
return resultInfo;
}
// 文件上传
String head = user.getHead();
try {
// 1、得到part对象 Part part = request.getPart("img"); // getPart(name):前台file表单元素的name属性值
Part part = request.getPart("img");
if (part != null) {
// 2、得到文件存放的路径 path = request.getServletContext().getRealPath("/WEB-INF/upload/");
String path = request.getServletContext().getRealPath("/WEB-INF/upload/");
// 3、得到上传文件的名称 fileName = part.getSubmitedFiledName()
String fileName = part.getSubmittedFileName();
// 4、上传文件到指定路径 part.write(path + fileName);
part.write(path + fileName);
head = fileName;
}
} catch (Exception e) {
e.printStackTrace();
}
// 调用Dao层,返回code
int code = userDao.updateInfo(userId, nick, mood, head);
if (code == 1) { // 成功
resultInfo.setCode(1);
resultInfo.setMsg("修改成功!");
// 更新session作用域中的user对象
user.setNick(nick);
user.setMood(mood);
user.setHead(head);
request.getSession().setAttribute("user", user);
} else {
resultInfo.setCode(0);
resultInfo.setMsg("修改失败!");
}
return resultInfo;
}
Dao层
1.查找用户信息
public User findUserByNick(Integer userId,String nickName){
User user = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//1.得到数据库连接
connection = DBUtil.getConnection();
//2.写sql语句
String sql = "select * from tb_user where nick = ? and userId != ?";
//3.预编译
preparedStatement = connection.prepareStatement(sql);
//4.设置参数,下标从1开始
preparedStatement.setString(1, nickName);
preparedStatement.setInt(2, userId);
//5.执行查询,返回结果集
resultSet = preparedStatement.executeQuery();
//6.分析结果集,得到user对象
if (resultSet.next()) {
user = new User();
user.setHead(resultSet.getString("head"));
user.setMood(resultSet.getString("mood"));
user.setNick(resultSet.getString("nick"));
user.setUname(resultSet.getString("uname"));
user.setUpwd(resultSet.getString("upwd"));
user.setUserId(resultSet.getInt("userId"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭
DBUtil.close(resultSet, preparedStatement, connection);
}
return user;
}
2.修改用户信息
public int updateInfo(Integer userId, String nick, String mood,
String head) {
// 1.定义sql语句
String sql = "update tb_user set nick = ?, mood = ?, head = ? where userId = ?";
//2.定义参数集合
List<Object> params = new ArrayList<Object>();
params.add(nick);
params.add(mood);
params.add(head);
params.add(userId);
// 3.调用BaseDao,返回受影响的行数
int rwo = new BaseDao().executeUpdate(sql,params);
return rwo;
}
userdao里调用了BaseDao里的方法
public int executeUpdate(String sql,List<Object> params) {
int row = 0; //受影响的行数
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//得到数据库连接
connection = DBUtil.getConnection();
//预编译
preparedStatement = connection.prepareStatement(sql);
//设置参数
//判断参数集合是否为空
if (params !=null && params.size()>0) {
//循环设置参数,下标从1开始
for (int i = 0; i < params.size(); i++) {
preparedStatement.setObject(i+1, params.get(i));
}
}
//执行更新,返回受影响的行数
row = preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
finally{
//关闭资源
DBUtil.close(null, preparedStatement, connection);
}
return row;
}
标签:String,userId,request,用户,笔记,项目分析,resultInfo,params,user 来源: https://blog.51cto.com/u_13985831/2836988