(源代码见仓库:https://gitee.com/jianghao233/course)
上篇博客中,初步完成分页功能,继续完善分页功能,
由图可知,当前页为 1 (首页),< 还可以点 ;当前页为 2 (末页),> 还可以点不符合常理,修改代码
修改 courseManger.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
课程列表
课程ID
课程名称
课程学时
课程学分
操作
${c.courseid }
${c.coursename }
${c.hour }
${c.score }
编辑
删除
输出:点击课程管理---进入课程管理页面--当在首页时,< 无链接 ;当在末页时,>无链接
至此,可视化部分基本再没有问题了;但部分代码过于繁琐,(传递数据都可以封装到之前建立的OV包中)可以继续完善代码
新建 PageBean.java
package com.neuedu.vo;
import java.util.List;
import com.neuedu.commons.Commons;
public class PageBean {
private int nowPage; //当前页
private int record; //每页显示的记录数
private int totalPage; //总页数
private int count; //总记录数
private List list; //查询的分页记录
public PageBean() {}
public PageBean(int nowPage, int record) {
super();
this.nowPage = nowPage;
this.record = record;
}
public int getNowPage() {
return nowPage;
}
public void setNowPage(int nowPage) {
this.nowPage = nowPage;
}
public int getRecord() {
return record;
}
public void setRecord(int record) {
this.record = record;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
totalPage = count == 0 ? 0 : (count - 1) / Commons.RECORD + 1;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
修改 CourseService.java
package com.neuedu.service;
import java.util.List;
import com.neuedu.po.TbCourse;
import com.neuedu.vo.PageBean;
public interface CourseService {
void save(TbCourse tbCourse);
TbCourse getCourseById(Integer courseid);
void getList(PageBean pageBean); //修改代码
void update(TbCourse tbCourse);
void delete(Integer courseid);
int getCount();
}
修改 CourseServiceImpl.java
package com.neuedu.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.neuedu.commons.Commons;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;
import com.neuedu.vo.PageBean;
import com.sun.javafx.css.CssError.InlineStyleParsingError;
@Controller
@RequestMapping("/course")
public class CourseController {
@Autowired
private CourseService courseService;
@RequestMapping({"/","/list"})
public String list(@RequestParam(value="nowPage",required=false,defaultValue="1")
Integer nowPage,Model model) {
//查询课程列表
PageBean pageBean = new PageBean<>(nowPage,Commons.RECORD);
courseService.getList(pageBean);
model.addAttribute("pageBean", pageBean);
return "admin/courseManager";
}
///显示添加页面
@RequestMapping("/showAdd")
public String showAdd() {
return "admin/courseAdd";
}
//保存课程信息
@RequestMapping("/save")
public String save(TbCourse tbCourse) {
//调用service保存课程信息
courseService.save(tbCourse);
return "redirect:/course/";
}
//显示修改页面
@RequestMapping("/showModify")
public String showModify(Integer courseid,Map map) {
TbCourse tbCourse = courseService.getCourseById(courseid);
map.put("c", tbCourse);
return "admin/courseModify";
}
@RequestMapping("/modify")
public String modify(TbCourse tbCourse) {
courseService.update(tbCourse);
return "redirect:/course/";
}
@RequestMapping("/delete")
public String delete(Integer courseid) {
courseService.delete(courseid);
return "redirect:/course/";
}
}
TbCourseMapper.java
package com.neuedu.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.neuedu.po.TbCourse;
public interface TbCourseMapper {
int deleteByPrimaryKey(Integer courseid);
int insert(TbCourse record);
int insertSelective(TbCourse record);
TbCourse selectByPrimaryKey(Integer courseid);
int updateByPrimaryKeySelective(TbCourse record);
int updateByPrimaryKey(TbCourse record);
public List getList(@Param("start") int start,@Param("record")int record);
public int getCount();
}
TbCourseMapper.xml
courseid, coursename, hour, score, picurl
delete from tb_course
where courseid = #{courseid,jdbcType=INTEGER}
insert into tb_course (courseid, coursename, hour,
score, picurl)
values (#{courseid,jdbcType=INTEGER}, #{coursename,jdbcType=VARCHAR}, #{hour,jdbcType=INTEGER},
#{score,jdbcType=DOUBLE}, #{picurl,jdbcType=VARCHAR})
insert into tb_course
courseid,
coursename,
hour,
score,
picurl,
#{courseid,jdbcType=INTEGER},
#{coursename,jdbcType=VARCHAR},
#{hour,jdbcType=INTEGER},
#{score,jdbcType=DOUBLE},
#{picurl,jdbcType=VARCHAR},
update tb_course
coursename = #{coursename,jdbcType=VARCHAR},
hour = #{hour,jdbcType=INTEGER},
score = #{score,jdbcType=DOUBLE},
picurl = #{picurl,jdbcType=VARCHAR},
where courseid = #{courseid,jdbcType=INTEGER}
update tb_course
set coursename = #{coursename,jdbcType=VARCHAR},
hour = #{hour,jdbcType=INTEGER},
score = #{score,jdbcType=DOUBLE},
picurl = #{picurl,jdbcType=VARCHAR}
where courseid = #{courseid,jdbcType=INTEGER}
修改 CourseController.java(多处修改)
package com.neuedu.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.neuedu.commons.Commons;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;
import com.neuedu.vo.PageBean;
import com.sun.javafx.css.CssError.InlineStyleParsingError;
@Controller
@RequestMapping("/course")
public class CourseController {
@Autowired
private CourseService courseService;
@RequestMapping({"/","/list"})
public String list(@RequestParam(value="nowPage",required=false,defaultValue="1")
Integer nowPage,Model model) {
//查询课程列表
PageBean pageBean = new PageBean<>(nowPage,Commons.RECORD);
courseService.getList(pageBean);
model.addAttribute("pageBean", pageBean);
return "admin/courseManager";
}
///显示添加页面
@RequestMapping("/showAdd")
public String showAdd() {
return "admin/courseAdd";
}
//保存课程信息
@RequestMapping("/save")
public String save(TbCourse tbCourse) {
//调用service保存课程信息
courseService.save(tbCourse);
return "redirect:/course/";
}
//显示修改页面
@RequestMapping("/showModify")
public String showModify(Integer courseid,Map map) {
TbCourse tbCourse = courseService.getCourseById(courseid);
map.put("c", tbCourse);
return "admin/courseModify";
}
@RequestMapping("/modify")
public String modify(TbCourse tbCourse) {
courseService.update(tbCourse);
return "redirect:/course/";
}
@RequestMapping("/delete")
public String delete(Integer courseid) {
courseService.delete(courseid);
return "redirect:/course/";
}
}
修改 courseManager.jsp(添加 PageBean)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
课程列表
课程ID
课程名称
课程学时
课程学分
操作
${c.courseid }
${c.coursename }
${c.hour }
${c.score }
编辑
删除
输出:与之前的功能完全一样,将数据封装在 PageBean 中,以后直接调用即可。分页功能完全开发完成