package com.hp.school.controller; import java.io.File; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; 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.hp.school.entity.Clazz; import com.hp.school.entity.Student; import com.hp.school.page.Page; import com.hp.school.service.ClazzService; import com.hp.school.service.StudentService; import net.sf.json.JSONArray; @RequestMapping("/student") @Controller public class StudentController { // 注入学生 @Autowired private StudentService studentService; // 注入学生 @Autowired private ClazzService clazzService; @RequestMapping("/upload_photo") @ResponseBody public Map<String, Object> uploadphoto(MultipartFile photo, HttpServletRequest request, HttpServletResponse response) { Map<String, Object> map = new HashMap<>(); // 判断 是否已经选择一个文件 if (photo == null) { map.put("type", "error"); map.put("msg", "请选择文件!"); return map; } // 1. 选中的文件是否超过 最大允许值 if (photo.getSize() > 10485760) { map.put("type", "error"); map.put("msg", "文件最大允许10M!"); return map; } // 2.比如只允许上传图片 ,判断 后缀是否是图片 String originalFilename = photo.getOriginalFilename(); // 获取上传文件名123.jpg // 截取 后缀 jpg String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length()); if (!"jpg,png,gif,jpeg".contains(suffix.toLowerCase())) { map.put("type", "error"); map.put("msg", "只能上传图片!"); return map; } // 3. 获取 文件上传 服务器路径 String serverPath = request.getServletContext().getRealPath("/") + "\\" + "upload" + "\\"; // System.out.println(serverPath); File file = new File(serverPath); if (!file.exists()) { file.mkdir(); // 创建服务器路径 ...tomcat/xx/xxx/upload } // 4.上传 String filename = new Date().getTime()+ originalFilename; try { photo.transferTo(new File(serverPath + filename));// 上传 } catch (IOException e) { e.printStackTrace(); } // System.out.println(request.getServletContext().getContextPath()+"/"+originalFilename); map.put("type", "success"); map.put("msg", "上传成功"); map.put("src", request.getServletContext().getContextPath() + "/" + "upload/" + filename ); return map; } // list /*@RequestMapping(value="/list",method=RequestMethod.GET) public ModelAndView toLogin(ModelAndView model){ model.setViewName("student/student_list"); return model; } */ /** * 跳转到 学生jsp页面 ,跳转同时携带 学生 列表信息 * @param model * @return */ @RequestMapping("/list") public ModelAndView list(ModelAndView model){ model.setViewName("student/student_list"); //绑定 所有学生 信息 到列表 List<Clazz> clazzList = clazzService.findAll(); model.addObject("clazzList", clazzList); //将 学生列表 绑定到 model中 // 学生 列表 是 动态生成的 , 而且数据是json数据 , 将 学生转成json , 以便于 clazz_list.jsp页面 列表显示json学生信息 model.addObject("gradeListJson", JSONArray.fromObject(clazzList)); return model; } /** * 添加学生 * @param user * @return */ @RequestMapping(value="/add",method=RequestMethod.POST) @ResponseBody public Map<String,String> add(Student student){ Map<String,String> map = new HashMap<String, String>(); if(StringUtils.isEmpty(student.getUsername())){ map.put("type", "error"); map.put("msg", "学生名不能为空!"); return map; } student.setSn("s2019"+new Date().getTime()); //保存学生 int result = studentService.add(student); // TODO 学号 ,可以随机生成 if(result<=0){ map.put("type", "error"); map.put("msg", "学生保存失败!"); return map; } map.put("type", "success"); map.put("msg", "添加学生成功!"); return map; } /** * 获取学生列表数据 -- 包含 条件查询 分页 * @return */ @RequestMapping(value="/get_list",method=RequestMethod.POST) @ResponseBody /** * @param username 模糊查询条件 * @param page 分页类 * @param gradeId 根据 年级 筛选 ,该年级下面的 学生信息 * @return */ public Map<String,Object> getList( @RequestParam(name="name",required=false,defaultValue="") String name, @RequestParam(name="clazzid",required=false) String clazzid, Page page ){ Map<String,Object> map = new HashMap<>(); // 最终数据在这里 // 这个map 等同于 QueryBean Map<String,Object> queryMap = new HashMap<>(); // 是一个查询条件类 //拼装 limit ?,? queryMap.put("offset", page.getOffset()); queryMap.put("pageSize", page.getRows()); queryMap.put("name", "%"+name+"%"); if(clazzid!=null){ // 已经选中了一个已存在的年级 queryMap.put("clazzId", clazzid); } map.put("rows", studentService.getList(queryMap)); //比如查询的第2页显示的一个集合数据 map.put("total", studentService.getTotal(queryMap)); //接收总数量 return map; } }

9632148963 LV1
2024年12月10日
skook7 LV2
2024年10月30日
hongdongdong LV14
2024年6月18日
潘潘123456 LV2
2023年12月30日
uni-code_0123 LV1
2023年8月4日
douhongwen LV1
2023年7月21日
ice_candy LV1
2023年6月19日
493240689 LV3
2023年6月3日
微信网友_6469820124057600 LV6
2023年5月30日
liuchang183 LV5
2023年4月22日

甜心冰淇淋 LV4
6月15日
微信网友_7520905278033920 LV1
5月22日
xianyu091012 LV5
2024年12月26日
571818771 LV3
2024年12月16日
84126415 LV2
2024年12月10日
565236523
2024年12月10日
暂无贡献等级
9632148963 LV1
2024年12月10日
moxiao
2024年12月3日
暂无贡献等级
asdfgh112
2024年7月4日
暂无贡献等级
时光海 LV2
2024年6月30日