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

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

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

(源代码见仓库:https://gitee.com/jianghao233/course)

编辑功能:

修改代码: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 } 编辑 //修改代码 删除
<
1
>
4
20

修改代码:CourseService.java

package com.neuedu.service;

import java.util.List;

import com.neuedu.po.TbCourse;

public interface CourseService {
	void save(TbCourse tbCourse);
	TbCourse getCourseById(Integer courseid);
	List getList();
	
	void update(TbCourse tbCourse);   //新增代码
}
 

修改代码:配置文件 CourseServiceImpl.java

package com.neuedu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neuedu.mapper.TbCourseMapper;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;
@Service
public class CourseServiceImpl implements CourseService {
	
	@Autowired
	private TbCourseMapper courseMapper;
	@Override
	public void save(TbCourse tbCourse) {
		courseMapper.insertSelective(tbCourse);
	}
	@Override
	public List getList() {
		
		return courseMapper.getList();
	}
	@Override
	public TbCourse getCourseById(Integer courseid) {              //新增代码
	
		return courseMapper.selectByPrimaryKey(courseid);             //新增代码
	}
	@Override
	public void update(TbCourse tbCourse) {              
		courseMapper.updateByPrimaryKeySelective(tbCourse);             //新增代码
		
	}

}

修改代码: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.po.TbCourse;
import com.neuedu.service.CourseService;

import com.sun.javafx.css.CssError.InlineStyleParsingError;


@Controller
@RequestMapping("/course")
public class CourseController {
	@Autowired
	private CourseService courseService;
	
	@RequestMapping({"/","/list"})
	public String list(Model model) { 
		//查询课程列表
		List list = courseService.getList();
		model.addAttribute("list",list);
		
		
		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/";
		
	}                                            //新增代码
}

输出:点击课程管理--进入课程管理页面--点击编辑--进入编辑页面---输入更改信息---点击提交--数据已保存到数据库且显示

初识 Spring(19)---(SpringMVC实战--构建学生管理系统(09))_第1张图片

初识 Spring(19)---(SpringMVC实战--构建学生管理系统(09))_第2张图片初识 Spring(19)---(SpringMVC实战--构建学生管理系统(09))_第3张图片

初识 Spring(19)---(SpringMVC实战--构建学生管理系统(09))_第4张图片

删除功能:

修改代码: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 } 编辑 删除 //修改代码
<
1
>
4
20

修改代码:CourseService.java

package com.neuedu.service;

import java.util.List;

import com.neuedu.po.TbCourse;

public interface CourseService {
	void save(TbCourse tbCourse);
	TbCourse getCourseById(Integer courseid);
	List getList();
	
	void update(TbCourse tbCourse);
	void delete(Integer courseid);   //新增代码
}
 

修改代码:配置文件 CourseServiceImpl.java

package com.neuedu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neuedu.mapper.TbCourseMapper;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;
@Service
public class CourseServiceImpl implements CourseService {
	
	@Autowired
	private TbCourseMapper courseMapper;
	@Override
	public void save(TbCourse tbCourse) {
		courseMapper.insertSelective(tbCourse);
	}
	@Override
	public List getList() {
		
		return courseMapper.getList();
	}
	@Override
	public TbCourse getCourseById(Integer courseid) {
	
		return courseMapper.selectByPrimaryKey(courseid);
	}
	@Override
	public void update(TbCourse tbCourse) {
		courseMapper.updateByPrimaryKeySelective(tbCourse);
		
	}
	@Override                                   //新增代码
	public void delete(Integer courseid) {
		courseMapper.deleteByPrimaryKey(courseid);
		
	}                                           //新增代码

}

修改代码: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.po.TbCourse;
import com.neuedu.service.CourseService;

import com.sun.javafx.css.CssError.InlineStyleParsingError;


@Controller
@RequestMapping("/course")
public class CourseController {
	@Autowired
	private CourseService courseService;
	
	@RequestMapping({"/","/list"})
	public String list(Model model) { 
		//查询课程列表
		List list = courseService.getList();
		model.addAttribute("list",list);
		
		
		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/";
	}
}

输出:点击 网络攻防 的删除按钮---网络攻防成功被删除
初识 Spring(19)---(SpringMVC实战--构建学生管理系统(09))_第5张图片

初识 Spring(19)---(SpringMVC实战--构建学生管理系统(09))_第6张图片

你可能感兴趣的:(Java,#,Spring,突破Spring框架)