项目描述
基于ssm的学生成绩管理系统。分为管理员、教师、学生三种角色
管理员(编辑公告、批量导入学生/教师/课程基本信息、及增删改查、对系统接口访问权限的控制);
教师(录入学生成绩、查看自己的课程/学生、结束课程)
学生(选课、查成绩)
运行环境
jdk8+tomcat8+mysql+IntelliJ IDEA
项目技术
spring+spring mvc+mybatis+layui+jquery
项目截图
部分代码
@Controller
@RequestMapping(value="/auth")
public class AuthController {
@Autowired
AuthService authService;
@ResponseBody
@RequestMapping(value="/list")
public String getAuthList(@RequestParam(defaultValue="0")int curr,@RequestParam(defaultValue="10")int nums,
@RequestParam(defaultValue="")String searchKey) {
Pagination<Auth> page = new Pagination<Auth>();
page.setTotalItemsCount(authService.getTotalItemsCount(searchKey));
page.setPageSize(nums);
page.setPageNum(curr);
List<Auth> list = authService.getAuthList(page, searchKey);
String jsonStr = StrUtil.RETURN_JONS_PRE_STR + page.getTotalItemsCount()
+ StrUtil.RETURN_JONS_MID_STR
+ JSON.toJSONString(list) + StrUtil.RETURN_JONS_END_STR;
return jsonStr;
}
@ResponseBody
@RequestMapping(value="/setting")
public String setting(Auth auth, String type, Byte val) {
if ("teacherAuth".equals(type)) {
auth.setTeacherAuth(val);
} else {
auth.setStudentAuth(val);
}
System.out.println(auth.toString());
if (authService.update(auth) > 0) return StrUtil.RESULT_TRUE;
return "操作失败!";
}
@Controller
@RequestMapping(value="/login")
public class LoginController {
@Autowired
AuthService authService;
@Autowired
AdminService adminServiceImpl;
@Autowired
TeacherService teacherServiceImpl;
@Autowired
StudentService studentServiceImpl;
@RequestMapping(value="/loginPage")
public ModelAndView toLoginPage() {
return new ModelAndView("login");
}
@ResponseBody
@RequestMapping(value="/doLogin")
public String doLogin(@RequestParam(defaultValue="") String username,
@RequestParam(defaultValue="") String password,
@RequestParam(defaultValue="0") int userType,
@RequestParam(defaultValue="") String verifyCode, HttpSession session) {
//比较验证码
String sessionVerifyCode = (String) session.getAttribute(StrUtil.VERIFY_CODE);
if (sessionVerifyCode == null || !sessionVerifyCode.equals(verifyCode.toUpperCase()) ) {
return StrUtil.CODE_ERROR;
}
Login loginUser = null;
if (userType == 1) {
loginUser = (Login) adminServiceImpl;
} else if(userType == 2) {
loginUser = (Login) teacherServiceImpl;
} else if(userType == 3) {
loginUser = (Login) studentServiceImpl;
}
User user = loginUser.loginValidate(username, password.toUpperCase());//获得验证后user对象
if (user != null) {
//List<Auth> menuList = authService.getMenuList(user.getUserType());
List<Auth> urlList = authService.getUrlList(user.getUserType());
user.setUrlList(urlList);
//user.setMenuList(menuList);
session.setAttribute(StrUtil.USER, user);
return JSON.toJSONString(user);
}
return StrUtil.RESULT_FALSE;
}
@RequestMapping(value="/out")
public ModelAndView loginOut(HttpSession session) {
session.invalidate();//销毁sessions
//请求重定向到主页(login页)
return new ModelAndView("redirect:/");
}
@RequestMapping(value="/getVerifyCode")
public void getVerifyCode(HttpServletResponse response, HttpSession session) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
session.setAttribute("verifyCode", drawCodeImg(output));
try {
ServletOutputStream out = response.getOutputStream();
output.writeTo(out);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 绘出验证码
* @param output
* @return
*/
private String drawCodeImg(ByteArrayOutputStream output) {
String code = "";
for (int i = 0; i < 4; i++) {
code += randomChar();
}
int width = 70;
int height = 36;
BufferedImage bImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Font font = new Font("Times New Roman", Font.PLAIN, 20);
Graphics2D graphics = bImage.createGraphics();
graphics.setFont(font);
graphics.setColor(new Color(66,2,82));
graphics.setBackground(new Color(226,226,240));
graphics.clearRect(0, 0, width, height);
FontRenderContext context = graphics.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(code, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = bounds.getY();
double baseY = y - ascent;
graphics.drawString(code, (int) x, (int) baseY);
graphics.dispose();
try {
ImageIO.write(bImage, "jpg", output);
} catch (IOException e) {
e.printStackTrace();
}
return code;
}
/**
* 返回一个随机字符
* @return
*/
private char randomChar() {
Random r = new Random();
String str = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789";
return str.charAt(r.nextInt(str.length()));
}
}
@Controller
@RequestMapping(value="notice")
public class NoticeController {
private static final Integer AUTH_A = 3;
private static final Integer AUTH_T = 2;
private static final Integer AUTH_S = 1;
@Autowired
private NoticeService noticeService;
@ResponseBody
@RequestMapping(value="/list")
public String getNoticeList(@RequestParam(defaultValue="0")int curr,
@RequestParam(defaultValue="20")int nums,
@RequestParam(defaultValue="")String searchKey,
HttpSession session) {
Pagination<Student> page = new Pagination<Student>();
page.setPageSize(nums);
page.setPageNum(curr);
List<Notice> list = new ArrayList<Notice>();
User user = (User) session.getAttribute(StrUtil.USER);
Integer auth = null;
if (user.getUserType().equals(StrUtil.ADMIN)) {
auth = AUTH_A;
} else if (user.getUserType().equals(StrUtil.TEACHER)) {
auth = AUTH_T;
} else if (user.getUserType().equals(StrUtil.STUDENT)) {
auth = AUTH_S;
}
page.setTotalItemsCount(noticeService.getTotalItemsCount(auth, searchKey));
list = noticeService.getNoticeList(page, auth, searchKey);
String jsonStr = StrUtil.RETURN_JONS_PRE_STR
+ page.getTotalItemsCount()
+ StrUtil.RETURN_JONS_MID_STR
+ JSON.toJSONString(list)
+ StrUtil.RETURN_JONS_END_STR;
return jsonStr;
}
/**
* 查看公告
* @param nId
* @param mav
* @return
*/
@RequestMapping(value="/info")
public ModelAndView showNoticeInfo(HttpSession session, Integer nId, ModelAndView mav) {
User user = (User) session.getAttribute(StrUtil.USER);
Integer auth = null;
if (user.getUserType().equals(StrUtil.ADMIN)) {
auth = AUTH_A;
} else if (user.getUserType().equals(StrUtil.TEACHER)) {
auth = AUTH_T;
} else if (user.getUserType().equals(StrUtil.STUDENT)) {
auth = AUTH_S;
}
Notice notice = noticeService.getNotice(nId);
//无权限查看
if (auth < notice.getAuth()) {
return new ModelAndView("404");
}
mav = new ModelAndView("notice");
mav.addObject("notice", notice);
return mav;
}
@RequestMapping(value="/look")
public ModelAndView showNotice(){
return new ModelAndView("notice");
}
@RequestMapping(value="/addPage")
public ModelAndView toAddPage() {
return new ModelAndView("noticeAdd");
}
/**
* 增加,或者修改notice
* @param opType
* @param notice
* @return
*/
@ResponseBody
@RequestMapping(value="/add")
public String addNotice(@RequestParam(defaultValue="2")Integer opType, Notice notice) {
int res = 0;
if (opType == 0) {
try {
res = noticeService.addNotice(notice);
} catch (Exception e) {
System.out.println("添加失败!");
return "添加失败!";
}
if (res > 0)
return StrUtil.RESULT_TRUE;
return "添加失败";
} else if (opType == 1) {
res = noticeService.updateNotice(notice);
if (res > 0) return StrUtil.RESULT_TRUE;
return "修改失败!";
}
return "error";
}
@ResponseBody
@RequestMapping(value="/delete")
public String deleteNotice(Notice notice) {
if (noticeService.deleteNotice(notice) > 0) return StrUtil.RESULT_TRUE;
return "删除失败!";
}
/**
* 批量删除
* @param stuIds
* @return
*/
@ResponseBody
@RequestMapping(value="/deleteList")
public String deleteNoticeList(String nIds) {
List<Integer> list = new ArrayList<Integer>();
try {
String[] ids = nIds.split(",");
for (String id: ids) {
list.add(Integer.parseInt(id));
}
if (noticeService.deleteNotice(list) > 0) {
return StrUtil.RESULT_TRUE;
}
} catch (Exception e) {
e.printStackTrace();
return "删除失败!参数出错!";//
}
return "删除失败!";
}
@ResponseBody
@RequestMapping(value="/uploadImg")
public String uploadImg(MultipartFile file,HttpServletRequest request) throws IOException{
System.out.println("comming!");
String path = request.getSession().getServletContext().getRealPath("/images");
System.out.println("path>>"+path);
String fileName = file.getOriginalFilename();
System.out.println("fileName>>"+fileName);
fileName = fileName.substring(fileName.lastIndexOf("."), fileName.length());
fileName = fileName + System.currentTimeMillis();
System.out.println("fileName>>"+fileName);
File dir = new File(path, fileName);
if(!dir.exists()){
dir.mkdirs();
}
// MultipartFile自带的解析方法
file.transferTo(dir);
String jsonStr = StrUtil.RETURN_JONS_PRE_STR
+ null
+ StrUtil.RETURN_JONS_MID_STR
+ "{\"src\":\"" + "/images/" + fileName + "\"}"
+ StrUtil.RETURN_JONS_END_STR;
return jsonStr;
}
}