删除功能分析:
list.jsp:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <!-- 网页使用的语言 --> <html lang="zh-CN"> <head> <!-- 指定字符集 --> <meta charset="utf-8"> <!-- 使用Edge最新的浏览器的渲染方式 --> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。 width: 默认宽度与设备的宽度相同 initial-scale: 初始的缩放比,为1:1 --> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> <title>用户信息管理系统</title> <!-- 1. 导入CSS的全局样式 --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- 2. jQuery导入,建议使用1.9以上的版本 --> <script src="js/jquery-2.1.0.min.js"></script> <!-- 3. 导入bootstrap的js文件 --> <script src="js/bootstrap.min.js"></script> <style type="text/css"> td, th { text-align: center; } </style> <%-- 删除指定id --%> <script> function deleteUser(id) { //用户安全提示 if (confirm("确定要删除吗?")){ //访问路径 location.href = "${pageContext.request.contextPath}/delUserServlet?id="+id; } } </script> </head> <body> <div class="container"> <h3 style="text-align: center">用户信息列表</h3> <div style="float: left"> <form class="form-inline"> <div class="form-group"> <label for="exampleInputName2">姓名</label> <input type="text" class="form-control" id="exampleInputName2" placeholder="李四"> </div> <div class="form-group"> <label for="exampleInputEmail2">籍贯</label> <input type="email" class="form-control" id="exampleInputEmail2" placeholder="北京"> </div> <div class="form-group"> <label for="exampleInputEmail3">邮箱</label> <input type="email" class="form-control" id="exampleInputEmail3" placeholder=""> </div> <button type="submit" class="btn btn-default">查询</button> </form> </div> <div style="float: right; margin: 5px"> <td colspan="8" align="center"><a class="btn btn-primary" href="${pageContext.request.contextPath}/add.jsp">添加联系人</a></td> <td colspan="8" align="center"><a class="btn btn-primary" href="add.html">删除选中</a></td> </div> <table border="1" class="table table-bordered table-hover"> <tr class="success"> <th><input type="checkbox"></th> <th>编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>籍贯</th> <th>QQ</th> <th>邮箱</th> <th>操作</th> </tr> <c:forEach items="${users}" var="user" varStatus="s"> <tr> <td><input type="checkbox"></td> <td>${s.count}</td> <td>${user.name}</td> <td>${user.gender}</td> <td>${user.age}</td> <td>${user.address}</td> <td>${user.qq}</td> <td>${user.email}</td> <td> <a class="btn btn-default btn-sm" href="update.html">修改</a> <%--<a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/delUserServlet?id=${user.id}">删除</a>--%> <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id})">删除</a> </td> </tr> </c:forEach> </table> <div> <nav aria-label="Page navigation"> <ul class="pagination"> <li> <a href="#" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li> <a href="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> <span style="font-size: 25px;margin-left: 5px"> 共15条记录,共4页 </span> </ul> </nav> </div> </div> </body> </html>
UserDao接口:
package com.example.dao; import com.example.domain.User1; import java.util.List; /** * 用户操作的dao */ public interface UserDao { public List<User1> findAll(); public User1 findUserByUsernameAndPassword(String username,String password); void add(User1 user1); void delete(int id); }
UserDaoImpl类:
package com.example.dao.impl; import com.example.dao.UserDao; import com.example.domain.User1; import com.example.util.JDBCUtils; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; public class UserDaoImpl implements UserDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<User1> findAll() { //实现JDBC数据库操作 //定义sql String sql = "select * from user1"; List<User1> users = template.query(sql, new BeanPropertyRowMapper<User1>(User1.class)); return users; } @Override public User1 findUserByUsernameAndPassword(String username, String password) { try { String sql = "select * from user1 where username = ? and password = ?"; User1 user1 = template.queryForObject(sql, new BeanPropertyRowMapper<User1>(User1.class), username, password); return user1; } catch (DataAccessException e) { e.printStackTrace(); return null; } } @Override public void add(User1 user1) { //1.定义sql String sql = "insert into user1 value(null,?,?,?,?,?,?,null,null)"; //2.执行sql template.update(sql,user1.getName(),user1.getGender(),user1.getAge(),user1.getAddress(),user1.getQq(),user1.getEmail()); } @Override public void delete(int id) { //1.定义sql String sql = "delete from user1 where id = ?"; template.update(sql, id); } }
UserService接口:
package com.example.service; import com.example.domain.User1; import java.util.List; public interface UserService { /** * 查询方法 * @return */ public List<User1> findAll(); /** * 登录方法 * @param user1 * @return */ User1 login(User1 user1); /** * 添加User1 * @param user1 */ void addUser(User1 user1); /** * 根据id删除User1 * @param id */ void deleteUser(String id); }
UserServiceImpl类:
package com.example.service.impl; import com.example.dao.UserDao; import com.example.dao.impl.UserDaoImpl; import com.example.domain.User1; import com.example.service.UserService; import java.util.List; public class UserServiceImpl implements UserService { public UserDao dao = new UserDaoImpl(); @Override public List<User1> findAll() { //调用dao完成查询 return dao.findAll(); } @Override public User1 login(User1 user1) { return dao.findUserByUsernameAndPassword(user1.getUsername(),user1.getPassword()); } @Override public void addUser(User1 user1) { dao.add(user1); } @Override public void deleteUser(String id) { dao.delete(Integer.parseInt(id));//转换为int类型 } }
DelUserServlet类:
package com.example.web.servlet; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet(name = "delUserServlet", value = "/delUserServlet") public class DelUserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取参数,id String id = request.getParameter("id"); //2.调用service删除 UserService service = new UserServiceImpl(); service.deleteUser(id); //3.跳转查询所有Servlet response.sendRedirect(request.getContextPath()+"/userListServlet"); } }
修改功能分析
修改功能代码实现
用户信息回显代码实现:
UserDao接口:
package com.example.dao; import com.example.domain.User1; import java.util.List; /** * 用户操作的dao */ public interface UserDao { public List<User1> findAll(); public User1 findUserByUsernameAndPassword(String username,String password); void add(User1 user1); void delete(int id); User1 findById(int id); }
UserDaoImpl实现类:
package com.example.dao.impl; import com.example.dao.UserDao; import com.example.domain.User1; import com.example.util.JDBCUtils; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; public class UserDaoImpl implements UserDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<User1> findAll() { //实现JDBC数据库操作 //定义sql String sql = "select * from user1"; List<User1> users = template.query(sql, new BeanPropertyRowMapper<User1>(User1.class)); return users; } @Override public User1 findUserByUsernameAndPassword(String username, String password) { try { String sql = "select * from user1 where username = ? and password = ?"; User1 user1 = template.queryForObject(sql, new BeanPropertyRowMapper<User1>(User1.class), username, password); return user1; } catch (DataAccessException e) { e.printStackTrace(); return null; } } @Override public void add(User1 user1) { //1.定义sql String sql = "insert into user1 value(null,?,?,?,?,?,?,null,null)"; //2.执行sql template.update(sql,user1.getName(),user1.getGender(),user1.getAge(),user1.getAddress(),user1.getQq(),user1.getEmail()); } @Override public void delete(int id) { //1.定义sql String sql = "delete from user1 where id = ?"; template.update(sql, id); } @Override public User1 findById(int id) { String sql = "select * from user1 where id = ?"; return template.queryForObject(sql, new BeanPropertyRowMapper<User1>(User1.class),id);// 只查询一次 } }
UserService接口:
package com.example.service; import com.example.domain.User1; import java.util.List; public interface UserService { /** * 查询方法 * @return */ public List<User1> findAll(); /** * 登录方法 * @param user1 * @return */ User1 login(User1 user1); /** * 添加User1 * @param user1 */ void addUser(User1 user1); /** * 根据id删除User1 * @param id */ void deleteUser(String id); /** * 根据id查询 * @param id * @return */ User1 findUserById(String id); }
UserServiceImpl实现类:
package com.example.service.impl; import com.example.dao.UserDao; import com.example.dao.impl.UserDaoImpl; import com.example.domain.User1; import com.example.service.UserService; import java.util.List; public class UserServiceImpl implements UserService { public UserDao dao = new UserDaoImpl(); @Override public List<User1> findAll() { //调用dao完成查询 return dao.findAll(); } @Override public User1 login(User1 user1) { return dao.findUserByUsernameAndPassword(user1.getUsername(),user1.getPassword()); } @Override public void addUser(User1 user1) { dao.add(user1); } @Override public void deleteUser(String id) { dao.delete(Integer.parseInt(id));//转换为int类型 } @Override public User1 findUserById(String id) { return dao.findById(Integer.parseInt(id)); } }
findUserServlet:
package com.example.web.servlet; import com.example.domain.User1; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet(name = "findUserServlet", value = "/findUserServlet") public class findUserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取id String id = request.getParameter("id"); //2.调用Service查询 UserService service = new UserServiceImpl(); User1 user1 = service.findUserById(id); //3.将User1存入request request.setAttribute("user1", user1); //4.转发到update.jsp request.getRequestDispatcher("/update.jsp").forward(request, response); } }
list.jsp页面:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <!-- 网页使用的语言 --> <html lang="zh-CN"> <head> <!-- 指定字符集 --> <meta charset="utf-8"> <!-- 使用Edge最新的浏览器的渲染方式 --> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。 width: 默认宽度与设备的宽度相同 initial-scale: 初始的缩放比,为1:1 --> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> <title>用户信息管理系统</title> <!-- 1. 导入CSS的全局样式 --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- 2. jQuery导入,建议使用1.9以上的版本 --> <script src="js/jquery-2.1.0.min.js"></script> <!-- 3. 导入bootstrap的js文件 --> <script src="js/bootstrap.min.js"></script> <style type="text/css"> td, th { text-align: center; } </style> <%-- 删除指定id --%> <script> function deleteUser(id) { //用户安全提示 if (confirm("确定要删除吗?")){ //访问路径 location.href = "${pageContext.request.contextPath}/delUserServlet?id="+id; } } </script> </head> <body> <div class="container"> <h3 style="text-align: center">用户信息列表</h3> <div style="float: left"> <form class="form-inline"> <div class="form-group"> <label for="exampleInputName2">姓名</label> <input type="text" class="form-control" id="exampleInputName2" placeholder="李四"> </div> <div class="form-group"> <label for="exampleInputEmail2">籍贯</label> <input type="email" class="form-control" id="exampleInputEmail2" placeholder="北京"> </div> <div class="form-group"> <label for="exampleInputEmail3">邮箱</label> <input type="email" class="form-control" id="exampleInputEmail3" placeholder=""> </div> <button type="submit" class="btn btn-default">查询</button> </form> </div> <div style="float: right; margin: 5px"> <td colspan="8" align="center"><a class="btn btn-primary" href="${pageContext.request.contextPath}/add.jsp">添加联系人</a></td> <td colspan="8" align="center"><a class="btn btn-primary" href="add.html">删除选中</a></td> </div> <table border="1" class="table table-bordered table-hover"> <tr class="success"> <th><input type="checkbox"></th> <th>编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>籍贯</th> <th>QQ</th> <th>邮箱</th> <th>操作</th> </tr> <c:forEach items="${users}" var="user" varStatus="s"> <tr> <td><input type="checkbox"></td> <td>${s.count}</td> <td>${user.name}</td> <td>${user.gender}</td> <td>${user.age}</td> <td>${user.address}</td> <td>${user.qq}</td> <td>${user.email}</td> <td> <a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改</a> <%--<a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/delUserServlet?id=${user.id}">删除</a>--%> <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id})">删除</a> </td> </tr> </c:forEach> </table> <div> <nav aria-label="Page navigation"> <ul class="pagination"> <li> <a href="#" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li> <a href="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> <span style="font-size: 25px;margin-left: 5px"> 共15条记录,共4页 </span> </ul> </nav> </div> </div> </body> </html>
update.jsp页面:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ 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="" method="post"> <div class="form-group"> <label for="name">姓名:</label> <input type="text" class="form-control" id="name" name="name" value="${user1.name}" readonly="readonly" placeholder="请输入姓名" /> </div> <div class="form-group"> <label>性别:</label> <c:if test="${user1.gender == '男'}"> <input type="radio" name="gender" value="男" checked />男 <input type="radio" name="gender" value="女" />女 </c:if> <c:if test="${user1.gender == '女'}"> <input type="radio" name="gender" value="男" />男 <input type="radio" name="gender" value="女" checked="checked" />女 </c:if> </div> <div class="form-group"> <label for="age">年龄:</label> <input type="text" class="form-control" id="age" name="age" value="${user1.age}" placeholder="请输入年龄" /> </div> <div class="form-group"> <label for="address">籍贯:</label> <select name="address" class="form-control" > <c:if test="${user1.address == '陕西'}"> <option value="陕西" selected>陕西</option> <option value="北京">北京</option> <option value="湖南">湖南</option> </c:if> <c:if test="${user1.address == '北京'}"> <option value="陕西">陕西</option> <option value="北京" selected>北京</option> <option value="湖南">湖南</option> </c:if> <c:if test="${user1.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" class="form-control" name="qq" value="${user1.qq}" placeholder="请输入QQ号码"/> </div> <div class="form-group"> <label for="email">Email:</label> <input type="text" class="form-control" name="email" value="${user1.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>
用户修改代码实现:
UserDao接口:
package com.example.dao; import com.example.domain.User1; import java.util.List; /** * 用户操作的dao */ public interface UserDao { public List<User1> findAll(); public User1 findUserByUsernameAndPassword(String username,String password); void add(User1 user1); void delete(int id); User1 findById(int id); void update(User1 user1); }
UserDaoImpl实现类:
package com.example.dao.impl; import com.example.dao.UserDao; import com.example.domain.User1; import com.example.util.JDBCUtils; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; public class UserDaoImpl implements UserDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<User1> findAll() { //实现JDBC数据库操作 //定义sql String sql = "select * from user1"; List<User1> users = template.query(sql, new BeanPropertyRowMapper<User1>(User1.class)); return users; } @Override public User1 findUserByUsernameAndPassword(String username, String password) { try { String sql = "select * from user1 where username = ? and password = ?"; User1 user1 = template.queryForObject(sql, new BeanPropertyRowMapper<User1>(User1.class), username, password); return user1; } catch (DataAccessException e) { e.printStackTrace(); return null; } } @Override public void add(User1 user1) { //1.定义sql String sql = "insert into user1 value(null,?,?,?,?,?,?,null,null)"; //2.执行sql template.update(sql,user1.getName(),user1.getGender(),user1.getAge(),user1.getAddress(),user1.getQq(),user1.getEmail()); } @Override public void delete(int id) { //1.定义sql String sql = "delete from user1 where id = ?"; template.update(sql, id); } @Override public User1 findById(int id) { String sql = "select * from user1 where id = ?"; return template.queryForObject(sql, new BeanPropertyRowMapper<User1>(User1.class),id);// 只查询一次 } @Override public void update(User1 user1) { //1.定义sql String sql = "update user1 set name = ? , gender = ? , age = ?, address = ?, qq = ?, email = ? where id = ?"; template.update(sql,user1.getName(),user1.getGender(),user1.getAge(),user1.getAddress(),user1.getQq(),user1.getEmail(),user1.getId()); } }
UserService接口:
package com.example.service; import com.example.domain.User1; import java.util.List; public interface UserService { /** * 查询方法 * @return */ public List<User1> findAll(); /** * 登录方法 * @param user1 * @return */ User1 login(User1 user1); /** * 添加User1 * @param user1 */ void addUser(User1 user1); /** * 根据id删除User1 * @param id */ void deleteUser(String id); /** * 根据id查询 * @param id * @return */ User1 findUserById(String id); /** * 修改用户信息 * @param user1 */ void updateUser(User1 user1); }
UserServiceImpl实现类:
package com.example.service.impl; import com.example.dao.UserDao; import com.example.dao.impl.UserDaoImpl; import com.example.domain.User1; import com.example.service.UserService; import java.util.List; public class UserServiceImpl implements UserService { public UserDao dao = new UserDaoImpl(); @Override public List<User1> findAll() { //调用dao完成查询 return dao.findAll(); } @Override public User1 login(User1 user1) { return dao.findUserByUsernameAndPassword(user1.getUsername(),user1.getPassword()); } @Override public void addUser(User1 user1) { dao.add(user1); } @Override public void deleteUser(String id) { dao.delete(Integer.parseInt(id));//转换为int类型 } @Override public User1 findUserById(String id) { return dao.findById(Integer.parseInt(id)); } @Override public void updateUser(User1 user1) { dao.update(user1); } }
updateUserServlet:
package com.example.web.servlet; import com.example.domain.User1; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; @WebServlet(name = "updateUserServlet", value = "/updateUserServlet") public class updateUserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.设置编码 request.setCharacterEncoding("utf-8"); //2.获取map Map<String, String[]> map = request.getParameterMap(); //3.封装对象 User1 user1 = new User1(); try { BeanUtils.populate(user1, map); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } //4.调用service修改 UserService service = new UserServiceImpl(); service.updateUser(user1); //5.跳转到查询所有Servlet response.sendRedirect(request.getContextPath()+"/userListServlet"); } }
标签:功能,删除,User1,user1,public,修改,import,com,id From: https://www.cnblogs.com/qihaokuan/p/17006392.html