初识 Spring(21)---(SpringMVC实战--构建学生管理系统(11))

本文介绍了一个基于SpringMVC的学生管理系统中的课程管理模块实现分页功能的过程。通过使用自定义的PageBean类和调整JSP页面上的链接逻辑来确保分页导航的正确性。

SpringMVC实战--构建学生管理系统(11)

首页部分课程管理页面(分页功能)制作(在上篇博客基础上继续)

(源代码见仓库: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"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/common.css"/>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-common.css"/>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-course.css"/>
		<script src="${pageContext.request.contextPath}/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
		
		
	</head>
	<body>
		<div id="class-title">
			课程列表
		</div>
		<div id="add-div">
			<form action="${pageContext.request.contextPath}/course/showAdd" method="post">
				<input type="submit" value="添加" />
			</form>
		</div>
		<table cellspacing="0">
			<tr>
				<th>课程ID</th>
				<th>课程名称</th>
				<th>课程学时</th>
				<th>课程学分</th>
				<th>操作</th>
			</tr>
			<c:forEach items="${list}" var="c">
				<tr>
				<td>${c.courseid }</td>
				<td>${c.coursename }</td>
				<td>${c.hour }</td>
				<td>${c.score }</td>
				<td>
					<a href="${pageContext.request.contextPath}/course/showModify?courseid=${c.courseid}">编辑</a>
					<a href="${pageContext.request.contextPath}/course/delete?courseid=${c.courseid}">删除</a>
				</td>
			</tr>
			</c:forEach>
			
			
		</table>
		<div id="fenye">
			<div class="page">
				<c:if test="${nowPage == 1}">      //新增代码
					<
				</c:if>
				<c:if test="${nowPage > 1}">         //新增代码
					<a href="${pageContext.request.contextPath}/course/list?nowPage=${nowPage -1}"><</a>
				</c:if>                             //新增代码
				
			</div>
			<div class="page">
				${nowPage }
			</div>
			<div class="page">
				<c:if test="${nowPage == totalPage}">         //新增代码
					>
				</c:if>
				<c:if test="${nowPage < totalPage}">         //新增代码
					<a href="${pageContext.request.contextPath}/course/list?nowPage=${nowPage +1}">></a>
				</c:if>                                     //新增代码
				
				
			</div>
			<div class="page">
				${totalPage }
			</div>
			<div class="page">
				${count }
			</div>
		</div>
	</body>
</html>

输出:点击课程管理---进入课程管理页面--当在首页时,< 无链接 ;当在末页时,>无链接

至此,可视化部分基本再没有问题了;但部分代码过于繁琐,(传递数据都可以封装到之前建立的OV包中)可以继续完善代码

新建 PageBean.java

package com.neuedu.vo;

import java.util.List;

import com.neuedu.commons.Commons;

public class PageBean<T> {
	private int nowPage; //当前页
	private int record; //每页显示的记录数
	private int totalPage; //总页数
	private int count; //总记录数
	
	private List<T> 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<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}
	
	

}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin_index.css"/>
	</head>
	<body>
		<div id="header">
			
		</div> 
		<div id="left">
			<ul id="nav">
				<li><a href="${pageContext.request.contextPath}/class/list" target="content">班级管理</a></li>
				<li><a href="${pageContext.request.contextPath}/student/" target="content">学生管理</a></li>
				<li><a href="${pageContext.request.contextPath}/course/" target="content">课程管理</a></li>
				<li><a href="scoreInManager.html" target="content">成绩录入</a></li>
			</ul>
		</div>
		<div id="right">
			<iframe id="iframe" name="content" src="studentManager.jsp" width="100%" height="700px"></iframe>
		</div>
	</body>
</html>

修改 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<TbCourse> 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<TbCourse> 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<String,Object> 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<TbCourse> getList(@Param("start") int start,@Param("record")int record);

    public int getCount();
}

TbCourseMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.neuedu.mapper.TbCourseMapper" >
  <resultMap id="BaseResultMap" type="com.neuedu.po.TbCourse" >
    <id column="courseid" property="courseid" jdbcType="INTEGER" />
    <result column="coursename" property="coursename" jdbcType="VARCHAR" />
    <result column="hour" property="hour" jdbcType="INTEGER" />
    <result column="score" property="score" jdbcType="DOUBLE" />
    <result column="picurl" property="picurl" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    courseid, coursename, hour, score, picurl
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from tb_course
    where courseid = #{courseid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tb_course
    where courseid = #{courseid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.neuedu.po.TbCourse" >
    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>
  <insert id="insertSelective" parameterType="com.neuedu.po.TbCourse" >
    insert into tb_course
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="courseid != null" >
        courseid,
      </if>
      <if test="coursename != null" >
        coursename,
      </if>
      <if test="hour != null" >
        hour,
      </if>
      <if test="score != null" >
        score,
      </if>
      <if test="picurl != null" >
        picurl,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="courseid != null" >
        #{courseid,jdbcType=INTEGER},
      </if>
      <if test="coursename != null" >
        #{coursename,jdbcType=VARCHAR},
      </if>
      <if test="hour != null" >
        #{hour,jdbcType=INTEGER},
      </if>
      <if test="score != null" >
        #{score,jdbcType=DOUBLE},
      </if>
      <if test="picurl != null" >
        #{picurl,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.neuedu.po.TbCourse" >
    update tb_course
    <set >
      <if test="coursename != null" >
        coursename = #{coursename,jdbcType=VARCHAR},
      </if>
      <if test="hour != null" >
        hour = #{hour,jdbcType=INTEGER},
      </if>
      <if test="score != null" >
        score = #{score,jdbcType=DOUBLE},
      </if>
      <if test="picurl != null" >
        picurl = #{picurl,jdbcType=VARCHAR},
      </if>
    </set>
    where courseid = #{courseid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.neuedu.po.TbCourse" >
    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}
  </update>
  
  <select id="getList" resultType="TbCourse">
  	select * from tb_course limit #{start},#{record}
  </select>
  
  
  <select id="getCount" resultType="int">
  	select count(*) from tb_course 
  </select>
</mapper>

修改 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<TbCourse> 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<String,Object> 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" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/common.css"/>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-common.css"/>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-course.css"/>
		<script src="${pageContext.request.contextPath}/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			a{
				text-decoration: none;
			}
		</style>
	</head>
	<body>
		<div id="class-title">
			课程列表
		</div>
		<div id="add-div">
			<form action="${pageContext.request.contextPath}/course/showAdd" method="post">
				<input type="submit"value="添加"/>		
			</form>
				
		</div>
		<table cellspacing="0">
			<tr>
				<th>课程ID</th>
				<th>课程名称</th>
				<th>课程学时</th>
				<th>课程学分</th>
				<th>操作</th>
			</tr>
			<c:forEach items="${pageBean.list }" var="c">
				<tr>
					<td>${c.courseid }</td>
					<td>${c.coursename }</td>
					<td>${c.hour }</td>
					<td>${c.score }</td>
					<td>
						<a href="${pageContext.request.contextPath}/course/showModify?courseid=${c.courseid}">编辑</a>
						<a href="${pageContext.request.contextPath}/course/delete?courseid=${c.courseid}">删除</a>
					</td>
				</tr>
			</c:forEach>
			
			
		</table>
		<div id="fenye">
			<div class="page">
				<c:if test="${pageBean.nowPage == 1}">
					<
				</c:if>
				<c:if test="${pageBean.nowPage > 1}">
					<a href="${pageContext.request.contextPath}/course/list?nowPage=${pageBean.nowPage - 1}"><</a>
				</c:if>
				
			</div>
			<div class="page">
				${pageBean.nowPage }
			</div>
			<div class="page">
				<c:if test="${pageBean.nowPage == pageBean.totalPage}">
					>
				</c:if>
				<c:if test="${pageBean.nowPage < pageBean.totalPage}">
					<a href="${pageContext.request.contextPath}/course/list?nowPage=${pageBean.nowPage + 1}">></a>
				</c:if>				
			</div>
			<div class="page">
				${pageBean.totalPage}
			</div>
			<div class="page">
				${pageBean.count }
			</div>
		</div>
	</body>
</html>

    

输出:与之前的功能完全一样,将数据封装在 PageBean 中,以后直接调用即可。分页功能完全开发完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值