classManager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
班级列表
// 添加班级功能
班级ID
班级名称
操作
1
一班
编辑
删除
2
二班
编辑
删除
3
三班
编辑
删除
4
四班
编辑
删除
点击 add 按钮,调用 add方法(在admin-class.js里)
admin-class.js
$(function(){
$('#form-div').hide();
});
function add(){
$('#form-div').show();
}
当页面都加载完毕,才会执行该方法,即页面加载完毕,先隐藏添加班级页面,点击 add方法,使其显示
输出:
点击 添加 按钮
新建 ClassService.java
package com.neuedu.service;
import com.neuedu.po.TbClass;
public interface ClassService {
public void save(TbClass tbClass);
}
新建 AdminServiceImpl.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.TbClassMapper;
import com.neuedu.po.TbClass;
import com.neuedu.service.ClassService;
@Service
public class ClassServiceImpl implements ClassService {
@Autowired
private TbClassMapper tbClassMapper;
@Override
public void save(TbClass tbClass) {
tbClassMapper.insertSelective(tbClass);
}
}
修改ClassController.java
package com.neuedu.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.neuedu.po.TbClass;
import com.neuedu.service.ClassService;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
@Controller
@RequestMapping("/class")
public class ClassController {
@Autowired
private ClassService classService;
@RequestMapping("/list")
public String list(HttpSession session) {
Object admin = session.getAttribute("admin");
if(admin == null) {
return "admin/login";
}
return "admin/classManager";
}
@RequestMapping("/save") //新增代码
public String save(TbClass tbClass) {
classService.save(tbClass);
return "admin/classManager";
} //新增代码
}
classManager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
班级列表
班级ID
班级名称
操作
1
一班
编辑
删除
2
二班
编辑
删除
3
三班
编辑
删除
4
四班
编辑
删除
避免中文乱码:修改web.xml
course
contextConfigLocation
classpath:beans.xml
org.springframework.web.context.ContextLoaderListener
characterEncoding //新增代码
org.springframework.web.filter.CharacterEncodingFilter //新增代码
encoding //新增代码
UTF-8 //新增代码
characterEncoding
/*
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springmvc
/
输出:输入班级,点击添加,成功将信息注入数据库
上述列表是写死的,不能动态从数据库获取信息,修改代码
修改TbClassMapper.java
package com.neuedu.mapper;
import java.util.List;
import com.neuedu.po.TbClass;
public interface TbClassMapper {
int deleteByPrimaryKey(Integer classid);
int insert(TbClass record);
int insertSelective(TbClass record);
TbClass selectByPrimaryKey(Integer classid);
int updateByPrimaryKeySelective(TbClass record);
int updateByPrimaryKey(TbClass record);
public List getList(); //新增代码
}
修改TbClassMapper.xml
classid, classname
delete from tb_class
where classid = #{classid,jdbcType=INTEGER}
insert into tb_class (classid, classname)
values (#{classid,jdbcType=INTEGER}, #{classname,jdbcType=VARCHAR})
insert into tb_class
classid,
classname,
#{classid,jdbcType=INTEGER},
#{classname,jdbcType=VARCHAR},
update tb_class
classname = #{classname,jdbcType=VARCHAR},
where classid = #{classid,jdbcType=INTEGER}
update tb_class
set classname = #{classname,jdbcType=VARCHAR}
where classid = #{classid,jdbcType=INTEGER}
//新增代码
修改 ClassService.java
package com.neuedu.service;
import com.neuedu.po.TbClass;
import java.util.List;
public interface ClassService {
public void save(TbClass tbClass);
public List getList(); //新增代码
}
修改ClassServiceImpl.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.TbClassMapper;
import com.neuedu.po.TbClass;
import com.neuedu.service.ClassService;
@Service
public class ClassServiceImpl implements ClassService {
@Autowired
private TbClassMapper tbClassMapper;
@Override
public void save(TbClass tbClass) {
tbClassMapper.insertSelective(tbClass);
}
@Override
public List getList() { //新增代码
return tbClassMapper.getList(); //新增代码
}
}
修改ClassController.java
package com.neuedu.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
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 com.neuedu.po.TbClass;
import com.neuedu.service.ClassService;
@Controller
@RequestMapping("/class")
public class ClassController {
@Autowired
private ClassService classService; //新增代码
@RequestMapping("/list")
public String list(HttpSession session,Model model) { //新增代码
Object admin = session.getAttribute("admin"); //新增代码
if(admin == null) {
return "admin/login";
}
System.out.println(model.getClass().getName());
List list = classService.getList();
model.addAttribute("list",list);
return "admin/classManager";
}
/*转发和重定向:
redirect:重定向到后面的路径里,不会再加前缀和后缀
forward:转发到后面的路径里,不会再加前缀和后缀
*/
@RequestMapping("/save") //新增代码
public String save(HttpSession session,TbClass tbClass) { //新增代码
Object admin = session.getAttribute("admin");
if(admin == null) {
return "admin/login"; //新增代码
}
classService.save(tbClass); //新增代码
return "forward:/class/list"; //新增代码
}
}
修改 classManager.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.classid }
${c.classname }
编辑
删除
//修改代码
输出:输入内容--点击添加---成功从数据库导入页面