package com.school.manager.controller;
import com.school.manager.entity.Clazz;
import com.school.manager.entity.Student;
import com.school.manager.service.ClazzService;
import com.school.manager.service.StudentService;
import com.school.manager.util.StringUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.school.manager.page.Page;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* 学生Controller
* @auther TR
*/
@RequestMapping("/student")
@Controller
public class StudentController {
private static final Log log = LogFactory.getLog(StudentController.class);
@Autowired
private ClazzService clazzService;
@Autowired
private StudentService studentService;
// 新增的图片类型白名单
private static final Set<String> ALLOWED_EXTENSIONS =
new HashSet<>(Arrays.asList("jpg", "png", "gif", "jpeg"));
@Autowired
private Student student;
/**
* 学生列表页面
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView list(ModelAndView model) {
model.setViewName("student/student_list");
List<Clazz> clazzList = clazzService.findAll();
model.addObject("clazzList", clazzList);
model.addObject("clazzListJson", JSONArray.fromObject(clazzList));
return model;
}
/**
* 获取学生列表
*/
@RequestMapping(value = "/get_list", method = {RequestMethod.POST})
@ResponseBody
public Map<String, Object> getList(@RequestParam(value = "name", required = false, defaultValue = "") String name,
@RequestParam(value = "clazzId", required = false, defaultValue = "")
Long clazzId, HttpServletRequest request, Page page) {
Map<String, Object> ret = new HashMap<>();
Map<String, Object> queryMap = new HashMap<>();
queryMap.put("username", "%" + name + "%");
Object attribute = request.getSession().getAttribute("userType");
if ("2".equals(attribute.toString())) {
//说明是学生
Student loginStudent = (Student) request.getSession().getAttribute("user");
queryMap.put("username", "%" + loginStudent.getUsername() + "%");
}
if (clazzId != null) {
queryMap.put("clazzId", clazzId);
}
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
ret.put("rows", studentService.findList(queryMap));
ret.put("total", studentService.getTotal(queryMap));
return ret;
}
/**
* 添加学生的操作
*/
@RequestMapping(value = "/add", method = {RequestMethod.POST})
@ResponseBody
public Map<String, String> add(Student student) {
Map<String, String> ret = new HashMap<String, String>();
if (StringUtils.isEmpty(student.getUsername())) {
ret.put("type", "error");
ret.put("msg", "学生姓名不能为空");
return ret;
}
if (StringUtils.isEmpty(student.getPassword())) {
ret.put("type", "error");
ret.put("msg", "学生登陆密码不能为空");
return ret;
}
if (student.getClazzId() == null) {
ret.put("type", "error");
ret.put("msg", "请选择所属班级");
return ret;
}
if (isExist(student.getUsername(), null)) {
ret.put("type", "error");
ret.put("msg", "该学生姓名已经存在");
return ret;
}
student.setSn(StringUtil.generatesn("S", ""));
if (studentService.add(student) <= 0) {
ret.put("type", "error");
ret.put("msg", "学生添加失败");
return ret;
}
ret.put("type", "success");
ret.put("msg", "学生添加成功");
return ret;
}
private boolean isExist(String username, Long id) {
Student student = studentService.findByUserName(username);
if (student != null) {
return true;
}
return false;
}
@RequestMapping(value = "/upload_photo", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> uploadPhoto(
@RequestParam("photo") MultipartFile photo,
HttpServletRequest request) {
Map<String, String> ret = new HashMap<>();
if (photo.isEmpty()) {
ret.put("type", "error");
ret.put("msg", "请选择文件");
return ret;
}
if (photo.getSize() > 10485760) { // 10MB
ret.put("type", "error");
ret.put("msg", "文件大于10M,请上传小于10M的图片");
return ret;
}
String originalFilename = photo.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
if (!ALLOWED_EXTENSIONS.contains(suffix)) {
ret.put("type", "error");
ret.put("msg", "文件格式不正确,请上传jpg,png,gif,jpeg格式的文件");
return ret;
}
String savePath = request.getServletContext().getRealPath("\\upload\\");
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdirs();
}
String filename = System.currentTimeMillis() + "." + suffix;
try {
photo.transferTo(new File(saveDir, filename));
ret.put("type", "success");
ret.put("msg", "图片上传成功");
// 使用统一路径分隔符
ret.put("src", request.getContextPath() + "\\upload\\" + filename);
} catch (IOException e) {
log.error("文件上传失败", e);
ret.put("type", "error");
ret.put("msg", "图片上传失败");
}
return ret;
}
/**
* 修改学生信息的操作
*/
@RequestMapping(value = "/edit", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> edit(Student student) {
Map<String, String> ret = new HashMap<String, String>();
if (StringUtils.isEmpty(student.getUsername())) {
ret.put("type", "error");
ret.put("msg", "学生姓名不能为空");
return ret;
}
if (StringUtils.isEmpty(student.getPassword())) {
ret.put("type", "error");
ret.put("msg", "学生密码不能为空");
return ret;
}
if (student.getClazzId() == null) {
ret.put("type", "error");
ret.put("msg", "请选择所属班级");
return ret;
}
if ((isExist(student.getUsername(), student.getId()))) {
ret.put("type", "error");
ret.put("msg", "该姓名已经存在");
return ret;
}
student.setSn(StringUtil.generatesn("S", ""));
if (studentService.edit(student) <= 0) {
ret.put("type", "error");
ret.put("msg", "学生修改失败");
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论






















收起资源包目录





































































































共 905 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论

- lichangcheng1232025-04-10感谢楼主的分享

记得晚安静静
- 粉丝: 117
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 【西门子PLC例程】-老外编的某汽车厂输送线S5程序.zip
- 【西门子PLC例程】-老外编写的PLC自动更新日光节约时间程序.zip
- 【西门子PLC例程】-老外编写的棒材打捆机程序.zip
- 【西门子PLC例程】-连接RS485编码器到S7-200 PPI接口.zip
- 【西门子PLC例程】-冷水机组.zip
- 【西门子PLC例程】-连接S7-200(主站)到多个S7-200(从站)通过GSM MODEM.zip
- 【西门子PLC例程】-煤炭取样及称重.zip
- 【西门子PLC例程】-流量累计.zip
- 【西门子PLC例程】-六台泵,两用四备,轮值运行.zip
- 【西门子PLC例程】-模拟量输出输入举例.zip
- 【西门子PLC例程】-美的风冷热泵中央空调PLC程序.zip
- 【西门子PLC例程】-模拟量输入处理范例.zip
- 【西门子PLC例程】-目前国内较先进的水厂控制.zip
- 【西门子PLC例程】-浓料泵控制程序.zip
- 【西门子PLC例程】-南光真空镀膜机PLC程序.zip
- 【西门子PLC例程】-汽缸.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
