795~797 修改分析,用户回显,用户修改
修改功能分析
更改修改的list按钮
数据回显
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <%@taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <!-- 网页使用的语言 --> <html lang= "zh-CN" > <head> <!-- 指定字符集 --> <meta charset= "utf-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1" > <title>修改用户</title> <link href= "css/bootstrap.min.css" rel= "stylesheet" > <script src= "js/jquery-2.1.0.min.js" ></script> <script src= "js/bootstrap.min.js" ></script> </head> <body> <div class = "container" style= "width: 400px;" > <h3 style= "text-align: center;" >修改联系人</h3> <form action= "${pageContext.request.contextPath}/updateUserServlet" method= "post" > <!-- 隐藏域 提交id--> <input type= "hidden" name= "id" value= "${user.id}" > <div class = "form-group" > <label for = "name" >姓名:</label> <input type= "text" class = "form-control" id= "name" name= "name" value= "${user.name}" readonly = "readonly" placeholder= "请输入姓名" /> </div> <div class = "form-group" > <label>性别:</label> <c: if test= "${user.gender == '男'}" > <input type= "radio" name= "gender" value= "男" checked />男 <input type= "radio" name= "gender" value= "女" />女 </c: if > <c: if test= "${user.gender == '女'}" > <input type= "radio" name= "gender" value= "男" />男 <input type= "radio" name= "gender" value= "女" checked />女 </c: if > </div> <div class = "form-group" > <label for = "age" >年龄:</label> <input type= "text" class = "form-control" value= "${user.age}" id= "age" name= "age" placeholder= "请输入年龄" /> </div> <div class = "form-group" > <label for = "address" >籍贯:</label> < select name= "address" id= "address" class = "form-control" > <c: if test= "${user.address == '陕西'}" > <option value= "陕西" selected>陕西</option> <option value= "北京" >北京</option> <option value= "上海" >上海</option> </c: if > <c: if test= "${user.address == '北京'}" > <option value= "陕西" >陕西</option> <option value= "北京" selected>北京</option> <option value= "上海" >上海</option> </c: if > <c: if test= "${user.address == '上海'}" > <option value= "陕西" >陕西</option> <option value= "北京" >北京</option> <option value= "上海" selected>上海</option> </c: if > </ select > </div> <div class = "form-group" > <label for = "qq" >QQ:</label> <input type= "text" id= "qq" class = "form-control" value= "${user.qq}" name= "qq" placeholder= "请输入QQ号码" /> </div> <div class = "form-group" > <label for = "email" >Email:</label> <input type= "text" id= "email" class = "form-control" value= "${user.email}" name= "email" placeholder= "请输入邮箱地址" /> </div> <div class = "form-group" style= "text-align: center" > <input class = "btn btn-primary" type= "submit" value= "提交" /> <input class = "btn btn-default" type= "reset" value= "重置" /> <input class = "btn btn-default" type= "button" value= "返回" /> </div> </form> </div> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.example.web.servlet; import com.example.domain.User; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @WebServlet( "/userListServlet" ) public class UserListServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.调用UserService完成查询 UserService service = new UserServiceImpl(); List<User> users = service.findAll(); //2.将list存入request域 request.setAttribute( "users" ,users); //3.转发到list.jsp request.getRequestDispatcher( "/list.jsp" ).forward(request,response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this .doPost(request,response); } } |
用户修改
list.jsp里加上隐藏域提交ai
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | void updateUser(User user); //修改 ========== //修改 @Override public void updateUser(User user) { dao.update(user); } ========== void update(User user); //修改 ========== @Override public void update(User user) { String sql = "update user set name = ?,gender = ? ,age = ? , address = ? , qq = ?, email = ? where id = ?" ; template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(), user.getId()); }========== package com.example.web.servlet; import com.example.domain.User; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; @WebServlet( "/updateUserServlet" ) public class UpdateUserServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.设置编码 request.setCharacterEncoding( "utf-8" ); //2.获取map Map<String, String[]> map = request.getParameterMap(); //3.封装对象 User user = new User(); try { BeanUtils.populate(user,map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //4.调用Service修改 UserService service = new UserServiceImpl(); service.updateUser(user); //5.跳转到查询所有Servlet response.sendRedirect(request.getContextPath()+ "/userListServlet" ); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this .doPost(request, response); } } |
list.jsp↓
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <%@taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <!-- 网页使用的语言 --> <html lang= "zh-CN" > <head> <!-- 指定字符集 --> <meta charset= "utf-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1" > <title>修改用户</title> <link href= "css/bootstrap.min.css" rel= "stylesheet" > <script src= "js/jquery-2.1.0.min.js" ></script> <script src= "js/bootstrap.min.js" ></script> </head> <body> <div class = "container" style= "width: 400px;" > <h3 style= "text-align: center;" >修改联系人</h3> <form action= "${pageContext.request.contextPath}/updateUserServlet" method= "post" > <!-- 隐藏域 提交id--> <input type= "hidden" name= "id" value= "${user.id}" > <div class = "form-group" > <label for = "name" >姓名:</label> <input type= "text" class = "form-control" id= "name" name= "name" value= "${user.name}" readonly = "readonly" placeholder= "请输入姓名" /> </div> <div class = "form-group" > <label>性别:</label> <c: if test= "${user.gender == '男'}" > <input type= "radio" name= "gender" value= "男" checked />男 <input type= "radio" name= "gender" value= "女" />女 </c: if > <c: if test= "${user.gender == '女'}" > <input type= "radio" name= "gender" value= "男" />男 <input type= "radio" name= "gender" value= "女" checked />女 </c: if > </div> <div class = "form-group" > <label for = "age" >年龄:</label> <input type= "text" class = "form-control" value= "${user.age}" id= "age" name= "age" placeholder= "请输入年龄" /> </div> <div class = "form-group" > <label for = "address" >籍贯:</label> < select name= "address" id= "address" class = "form-control" > <c: if test= "${user.address == '陕西'}" > <option value= "陕西" selected>陕西</option> <option value= "北京" >北京</option> <option value= "上海" >上海</option> </c: if > <c: if test= "${user.address == '北京'}" > <option value= "陕西" >陕西</option> <option value= "北京" selected>北京</option> <option value= "上海" >上海</option> </c: if > <c: if test= "${user.address == '上海'}" > <option value= "陕西" >陕西</option> <option value= "北京" >北京</option> <option value= "上海" selected>上海</option> </c: if > </ select > </div> <div class = "form-group" > <label for = "qq" >QQ:</label> <input type= "text" id= "qq" class = "form-control" value= "${user.qq}" name= "qq" placeholder= "请输入QQ号码" /> </div> <div class = "form-group" > <label for = "email" >Email:</label> <input type= "text" id= "email" class = "form-control" value= "${user.email}" name= "email" placeholder= "请输入邮箱地址" /> </div> <div class = "form-group" style= "text-align: center" > <input class = "btn btn-primary" type= "submit" value= "提交" /> <input class = "btn btn-default" type= "reset" value= "重置" /> <input class = "btn btn-default" type= "button" value= "返回" /> </div> </form> </div> </body> </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本