JavaWeb17.4【EL&JSTL:软件设计架构之三层架构、项目案例-用户信息列表展示】
1 <%-- 2 Created by IntelliJ IDEA. 3 User: yubaby 4 Date: 2021/7/4 5 Time: 11:55 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 10 11 12 <!DOCTYPE html> 13 <!-- 网页使用的语言 --> 14 <html lang="zh-CN"> 15 <head> 16 <!-- 指定字符集 --> 17 <meta charset="utf-8"> 18 <!-- 使用Edge最新的浏览器的渲染方式 --> 19 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 20 <!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。 21 width: 默认宽度与设备的宽度相同 22 initial-scale: 初始的缩放比,为1:1 --> 23 <meta name="viewport" content="width=device-width, initial-scale=1"> 24 <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> 25 <title>用户信息管理系统</title> 26 <!-- 1. 导入CSS的全局样式 --> 27 <link href="css/bootstrap.min.css" rel="stylesheet"> 28 <!-- 2. jQuery导入,建议使用1.9以上的版本 --> 29 <script src="js/jquery-2.1.0.min.js"></script> 30 <!-- 3. 导入bootstrap的js文件 --> 31 <script src="js/bootstrap.min.js"></script> 32 <style type="text/css"> 33 td, th { 34 text-align: center; 35 } 36 </style> 37 </head> 38 <body> 39 <div class="container"> 40 <h3 style="text-align: center">用户信息列表</h3> 41 <table border="1" class="table table-bordered table-hover"> 42 <tr class="success"> 43 <th>编号</th> 44 <th>姓名</th> 45 <th>性别</th> 46 <th>年龄</th> 47 <th>籍贯</th> 48 <th>QQ</th> 49 <th>邮箱</th> 50 <th>操作</th> 51 </tr> 52 53 <%--........................................................--%> 54 55 <c:forEach items="${users}" var="user" varStatus="s"> 56 <tr> 57 <td>${s.count}</td> 58 <td>${user.name}</td> 59 <td>${user.gender}</td> 60 <td>${user.age}</td> 61 <td>${user.address}</td> 62 <td>${user.qq}</td> 63 <td>${user.email}</td> 64 <td> 65 <a class="btn btn-default btn-sm" href="">修改</a> 66 <a class="btn btn-default btn-sm" href="">删除</a> 67 </td> 68 </tr> 69 </c:forEach> 70 71 <%--........................................................--%> 72 73 <tr> 74 <td colspan="8" align="center"><a class="btn btn-primary" href="">添加联系人</a></td> 75 </tr> 76 </table> 77 </div> 78 </body> 79 </html>
package com.haifei.web.servlet; import com.haifei.domain.User; import com.haifei.service.UserService; import com.haifei.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 { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { UserService userService = new UserServiceImpl(); List<User> users = userService.findAll(); request.setAttribute("users", users); request.getRequestDispatcher("/list1.jsp").forward(request, response); //浏览器访问http://localhost/day17_case/UserListServlet跳转到查询展示页面list1.jsp } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
1 package com.haifei.service; 2 3 import com.haifei.domain.User; 4 5 import java.util.List; 6 7 /** 8 * 用户管理的业务接口 9 */ 10 public interface UserService { 11 12 13 /** 14 * 查询所有用户 15 * @return 16 */ 17 List<User> findAll(); 18 * 添加用户 19 20 }
1 package com.haifei.service.impl; 2 3 import com.haifei.dao.UserDao; 4 import com.haifei.dao.impl.UserDaoImpl; 5 import com.haifei.domain.User; 6 import com.haifei.service.UserService; 7 8 import java.util.List; 9 10 public class UserServiceImpl implements UserService { 11 12 private UserDao userDao = new UserDaoImpl(); 13 14 15 @Override 16 public List<User> findAll() { 17 return userDao.findAll(); 18 } 19 20 }
1 package com.haifei.dao; 2 3 import com.haifei.domain.User; 4 5 import java.util.List; 6 7 public interface UserDao { 8 9 public List<User> findAll(); 10 11 }
1 package com.haifei.dao.impl; 2 3 import com.haifei.dao.UserDao; 4 import com.haifei.domain.User; 5 import com.haifei.util.JDBCUtils; 6 import org.springframework.jdbc.core.BeanPropertyRowMapper; 7 import org.springframework.jdbc.core.JdbcTemplate; 8 9 import java.util.List; 10 11 public class UserDaoImpl implements UserDao { 12 13 private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); 14 15 @Override 16 public List<User> findAll() { 17 String sql = "select * from user"; 18 List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class)); 19 return users; 20 } 21 22 23 }
1 package com.haifei.domain; 2 3 public class User { 4 5 private int id; 6 private String name; 7 private String gender; 8 private int age; 9 private String address; 10 private String qq; 11 private String email; 12 13 //private String username; 14 //private String password; 15 16 public User() { 17 } 18 19 public User(String name, String gender, int age, String address, String qq, String email) { 20 this.name = name; 21 this.gender = gender; 22 this.age = age; 23 this.address = address; 24 this.qq = qq; 25 this.email = email; 26 } 27 28 public void setId(int id) { 29 this.id = id; 30 } 31 32 public void setName(String name) { 33 this.name = name; 34 } 35 36 public void setGender(String gender) { 37 this.gender = gender; 38 } 39 40 public void setAge(int age) { 41 this.age = age; 42 } 43 44 public void setAddress(String address) { 45 this.address = address; 46 } 47 48 public void setQq(String qq) { 49 this.qq = qq; 50 } 51 52 public void setEmail(String email) { 53 this.email = email; 54 } 55 56 public int getId() { 57 return id; 58 } 59 60 public String getName() { 61 return name; 62 } 63 64 public String getGender() { 65 return gender; 66 } 67 68 public int getAge() { 69 return age; 70 } 71 72 public String getAddress() { 73 return address; 74 } 75 76 public String getQq() { 77 return qq; 78 } 79 80 public String getEmail() { 81 return email; 82 } 83 84 //public void setUsername(String username) { 85 this.username = username; 86 } 87 88 //public void setPassword(String password) { 89 this.password = password; 90 } 91 92 //public String getUsername() { 93 return username; 94 } 95 96 //public String getPassword() { 97 return password; 98 } 99 100 @Override 101 public String toString() { 102 return "User{" + 103 "id=" + id + 104 ", name='" + name + '\'' + 105 ", gender='" + gender + '\'' + 106 ", age=" + age + 107 ", address='" + address + '\'' + 108 ", qq='" + qq + '\'' + 109 ", email='" + email + '\'' + 110 //", username='" + username + '\'' + 111 //", password='" + password + '\'' + 112 '}'; 113 } 114 }
1 package com.haifei.util; 2 3 import com.alibaba.druid.pool.DruidDataSourceFactory; 4 5 import javax.sql.DataSource; 6 import javax.xml.crypto.Data; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.sql.Connection; 10 import java.sql.SQLException; 11 import java.util.Properties; 12 13 /** 14 * JDBC工具类 使用Durid连接池 15 */ 16 public class JDBCUtils { 17 18 private static DataSource ds ; 19 20 static { 21 22 try { 23 //1.加载配置文件 24 Properties pro = new Properties(); 25 //使用ClassLoader加载配置文件,获取字节输入流 26 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); 27 pro.load(is); 28 29 //2.初始化连接池对象 30 ds = DruidDataSourceFactory.createDataSource(pro); 31 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 } 38 39 /** 40 * 获取连接池对象 41 */ 42 public static DataSource getDataSource(){ 43 return ds; 44 } 45 46 47 /** 48 * 获取连接Connection对象 49 */ 50 public static Connection getConnection() throws SQLException { 51 return ds.getConnection(); 52 } 53 }
1 driverClassName=com.mysql.jdbc.Driver 2 url=jdbc:mysql:///day17 3 username=root 4 password=root 5 # 初始化连接数量 6 initialSize=5 7 # 最大连接数 8 maxActive=10 9 # 最大等待时间 10 maxWait=3000