复习:
jsp: java 服务页面,用于响应数据,展示数据。
内容: html(css/js/jQuery) java (Servlet)
使用:
- 指令 page、include、taglib
- 内置对象
域对象:pageContext、request、session、application
- el表达式和jstl标签库
${域对象名.绑定名}
jstl标签库:
指令
<c:if test=”${}”>
<c:foreach items=”${}” var=””>
<c:set var=”” value=””>
今日内容
- 删除
- 文件上传
- 分页查询
功能分析:在index.jsp页面中单击删除超链接时,先进行友好的删除提示,如果确定要删除请求后台servelt进行删除操作。
![](https://img-blog.csdnimg.cn/d491b9fc6af7421784e4beccf9f64aa5.png)
![](https://img-blog.csdnimg.cn/c4ad1d644f544e9990c12816640785c2.png)
dao:
![](https://img-blog.csdnimg.cn/125a898e754a449fb37a9d4c76f8b683.png)
service:
![](https://img-blog.csdnimg.cn/49e563b1ca2c4bd898585138529862fa.png)
servlet:
![](https://img-blog.csdnimg.cn/53d3bb4e366543c1bf9ff6fdd8f938a2.png)
上传操作必须要修改两个地方:
- 修改上传的表单,添加一个enctype属性,值必须是multipart/form-data
![](https://img-blog.csdnimg.cn/b912370e47ee44ac978eb950c055c2fc.png)
- 修改上传的servlet,添加一个注解
![](https://img-blog.csdnimg.cn/c7ac9618249f40ee963f4a0434fcfeeb.png)
- 写上传代码
上传使用的是servlet3.0以上版本提供的Part对象:
part.getSize() //获取上传文件大小
part.getSubmittedFileName()//获取上传文件的文件名
part.write(path);//上传方法
封装上传工具类:
package com.ujiuye.utils;
import javax.servlet.http.Part; import java.io.File; import java.io.IOException; import java.util.UUID;
public class FileUploadUtils { public static String uploadPic(Part part) throws IOException { //获取上传图片名 String pic = part.getSubmittedFileName(); //确定上传到服务器的哪个路径下 String path = "E:/stupic"; File file = new File(path); if (!file.exists()){ //判断上传的路径是存在, 如果不存在则创建 file.mkdir(); } //上传操作:E:\stupic\1.jpg pic = UUID.randomUUID()+pic; part.write(path+"/"+ pic); return pic; } } |
//添加学员 public void addStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { //获取页面传递过来数据 String sname = req.getParameter("sname"); String sex = req.getParameter("sex"); String age = req.getParameter("age"); String degree = req.getParameter("degree"); String mark = req.getParameter("mark"); //获取上传对象 Part part = req.getPart("pic"); //调用工具类中上传方法 String pic = FileUploadUtils.uploadPic(part); //封装学生对象 Student stu = new Student(sname,sex,Integer.parseInt(age),degree,pic,mark); //调用service层的添加方法 int row = ss.addStudent(stu); PrintWriter out = resp.getWriter(); //判断 if(row > 0){ //out.print("添加成功"); //跳转到显示 列表页面 resp.sendRedirect("student?method=findAllStudent"); }else{ //out.print("添加失败"); resp.sendRedirect("addStudent.html"); } } |
//修改学员 public void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取页面传递过来数据 String sname = req.getParameter("sname"); String sex = req.getParameter("sex"); String age = req.getParameter("age"); String degree = req.getParameter("degree"); String pic = req.getParameter("oldpic");//旧头像 String mark = req.getParameter("mark"); String sid = req.getParameter("sid"); //上传 Part part = req.getPart("pic"); //判断是否选择的新头像 if(part.getSize()!=0){ //调用上传工具类 pic = FileUploadUtils.uploadPic(part); } //封装对象 Student stu = new Student(Integer.parseInt(sid),sname,sex,Integer.parseInt(age),degree,pic,mark); //调用service int row = ss.updateStudent(stu); //判断 if (row>0){ resp.sendRedirect("student?method=findAllStudent"); } } |
- 配置远程服务器存储图片的路径到tomcat(挂载服务器路径)
![](https://img-blog.csdnimg.cn/46db9561ec744f26861d8b6606704a52.png)
![](https://img-blog.csdnimg.cn/53aa79426201495e8cb30770f364e30a.png)
- 修改index.jsp页面中显示图片
![](https://img-blog.csdnimg.cn/b554c9a169cb4a8eb05b994d9d2e0e75.png)
![](https://img-blog.csdnimg.cn/2e1524a687704b25b27cf4a8c7b758dd.png)
为什么要使用分页?
减轻客户端和服务器端压力。
分析:分页查询中共涉及哪些变量
- totalCount 表中记录总数 select count(*) from student
- pageSize 页容量 自定义 可修改
- currentPage 当前页码 从页面中直接获取
- totalPage 总页数
totalPage =( totalCount %pageSize==0) ?( totalCount /pageSize ):( totalCount /pageSize+1 )
- startIndex 每页第一条记录的起始下标
startIndex = (currentPage -1)*pageSize
- prePage 上一页 currentPage -1
- nextPage 下一页 currentPage +1
封装分页查询的工具类
package com.ujiuye.utils;
import java.util.List;
public class PageUitls<T> { public PageUitls(int totalCount, int pageSize, String currentPage) { this.totalCount = totalCount; this.pageSize = pageSize; initCurrentPage(currentPage); initTotalPage(); initStartIndex(); initPrePage(); initNextPage(); }
//给当前页码赋值 private void initCurrentPage(String currentPage){ if(currentPage==null){ this.currentPage = 1; }else{ this.currentPage = Integer.parseInt(currentPage); } }
//计算总页数 private void initTotalPage(){ this.totalPage = (totalCount%pageSize==0)?(totalCount/pageSize):(totalCount/pageSize+1); } //计算每页中第一条记录的超始下标 private void initStartIndex(){ this.startIndex = (currentPage-1)*pageSize; } //计算上一页 private void initPrePage(){ if(this.currentPage == 1){ this.prePage = 1; }else{ this.prePage = this.currentPage - 1; } } //下一页 private void initNextPage(){ if(this.currentPage == this.totalPage){ this.nextPage = this.totalPage; }else{ this.nextPage = this.currentPage + 1; } } private int totalCount;//总记录数 private int pageSize;//页容量 private int currentPage;//当前页码 private int totalPage;//总页数 private int startIndex;//每页第一条记录的起始下标 private int prePage;//上一页 private int nextPage;//下一页
private List<T> list;//分页查询出来数据
public List<T> getList() { return list; }
public void setList(List<T> list) { this.list = list; }
public int getTotalCount() { return totalCount; }
public void setTotalCount(int totalCount) { this.totalCount = totalCount; }
public int getPageSize() { return pageSize; }
public void setPageSize(int pageSize) { this.pageSize = pageSize; }
public int getCurrentPage() { return currentPage; }
public void setCurrentPage(int currentPage) { this.currentPage = currentPage; }
public int getTotalPage() { return totalPage; }
public void setTotalPage(int totalPage) { this.totalPage = totalPage; }
public int getStartIndex() { return startIndex; }
public void setStartIndex(int startIndex) { this.startIndex = startIndex; }
public int getPrePage() { return prePage; }
public void setPrePage(int prePage) { this.prePage = prePage; }
public int getNextPage() { return nextPage; }
public void setNextPage(int nextPage) { this.nextPage = nextPage; } } |
使用工具类实现分页查询:
dao:
![](https://img-blog.csdnimg.cn/59ca3f7ceef247d7915e554280f9032f.png)
![](https://img-blog.csdnimg.cn/604303a832bf4ef19bce34d560b5a8ff.png)
service:
![](https://img-blog.csdnimg.cn/059fe62e4b814f44b401fed810f4fd3a.png)
![](https://img-blog.csdnimg.cn/2b587eb4508c4353945e1481bcb1c9ad.png)
servlet:
//查询全部学生信息 public void findAllStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取总记录数 int totalCount = ss.getTotalCount(); //从页面获取当前页码 String currentPage = req.getParameter("currentPage"); //创建分页查询工具类对象 PageUitls pu = new PageUitls(totalCount,5,currentPage); //调用serivce层查询方法 List<Student> list = ss.findAllStudentByPage(pu); //将分页查询现来的数据保存到分页工具类对象的集合中 pu.setList(list); //将分页工具类对象保存到域中 req.setAttribute("pu",pu); //转发到index.jsp页面 req.getRequestDispatcher("index.jsp").forward(req,resp); } |
页面:index.jsp
![](https://img-blog.csdnimg.cn/8b3219f284094e26993c8a57469ccdd6.png)
![](https://img-blog.csdnimg.cn/20d6cb313e8c4ce699d3fbcb51d41ac4.png)
手动输入页码时,先判断页码范围是否合法,不合法给提示,合法跳转到后台做查询:
![](https://img-blog.csdnimg.cn/7122051d5b9845b6aa9b3fc77e3533d6.png)
效果:
![](https://img-blog.csdnimg.cn/a91aeaac7d844f7cba264e2050d0ab22.png)
![](https://img-blog.csdnimg.cn/b0ec5f015cda4f10823eda5607b99f3b.png)
在这里选择要导入的模块项目
![](https://img-blog.csdnimg.cn/f8298188970e430dbe3612b31334f3c9.png)
如果是web项目,还需要设置项目类型
![](https://img-blog.csdnimg.cn/ce440e3f7368402bab6e655d95581d0e.png)