毕业设计|基于SSM+JSP的家校通管理系统(二)https://developer.aliyun.com/article/1423937
4.2 网站内容发布
@Controller @RequestMapping("/introduction") public class IntroductionController extends BaseController { /** * 依赖注入 start dao/service/=== */ @Autowired private IntroductionService introductionService; /** * 前台查询网页介绍 * * @param request * @param response * @return */ @RequestMapping(value = "/queryIntroduction.do") public String queryIntroduction(Model model, HttpServletRequest request, HttpServletResponse response) { List<Introduction> introductionList=introductionService.listAll(); model.addAttribute("introductionList", introductionList); //存储查询条件 model.addAttribute("introductionList", introductionList); return "introduction/uIntroduction"; } /** * 分页查询 返回list对象(通过对象) * * @param request * @param response * @return */ @RequestMapping(value = "/findByObj.do") public String findByObj(Introduction introduction, Model model, HttpServletRequest request, HttpServletResponse response) { Pager<Introduction> pagers =new Pager<Introduction>(); if(introduction.getTitle()!=null){ Map<String, Object> map = getMap(); map.put("title",introduction.getTitle()); //模糊查询分页查询 pagers = introductionService.findByMap(map); }else{ //分页查询 pagers = introductionService.findByEntity(introduction); } model.addAttribute("pagers", pagers); //存储查询条件 model.addAttribute("obj", introduction); return "introduction/introduction"; } /** * 分页查询 返回list对象(通过Map) * * @param request * @param response * @return */ @RequestMapping(value = "/findByMap.do") public String findByMap(Introduction introduction, Model model, HttpServletRequest request, HttpServletResponse response) { //通过map查询 Map<String,Object> params = new HashMap<String,Object>(); if(!isEmpty(introduction.getTitle())){ params.put("title", introduction.getTitle()); } if(!isEmpty(introduction.getContent())){ params.put("content", introduction.getContent()); } if(!isEmpty(introduction.getAddTime())){ params.put("addTime", introduction.getAddTime()); } if(!isEmpty(introduction.getImageUrl())){ params.put("imageUrl", introduction.getImageUrl()); } //分页查询 Pager<Introduction> pagers = introductionService.findByMap(params); model.addAttribute("pagers", pagers); //存储查询条件 model.addAttribute("obj", introduction); return "introduction/introduction"; } /** * 跳至添加页面 * @return */ @RequestMapping(value = "/add.do") public String add() { return "introduction/add"; } /** * 添加执行 * @return */ @RequestMapping(value = "/exAdd.do") public String exAdd(@RequestParam(value = "file", required = false) MultipartFile file, Introduction introduction,HttpServletRequest request, Model model) { System.out.println("开始"); String path = request.getSession().getServletContext().getRealPath("/")+"upload/"; String fileName = file.getOriginalFilename(); System.out.println(path); File targetFile = new File("D:/my/upload", fileName); if(!targetFile.exists()){ targetFile.mkdirs(); } introduction.setImageUrl("\\upload\\"+fileName); //保存 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } introduction.setAddTime(new Date()); introductionService.insert(introduction); return "redirect:/introduction/findByObj.do"; } /** * 跳至修改页面 * @return */ @RequestMapping(value = "/update.do") public String update(Integer id,Model model) { Introduction obj = introductionService.load(id); model.addAttribute("obj",obj); return "introduction/update"; } /** * 添加修改 * @return */ @RequestMapping(value = "/exUpdate.do") public String exUpdate(@RequestParam(value = "file", required = false) MultipartFile file, Introduction introduction,HttpServletRequest request, Model model) { System.out.println("开始"); String path = request.getSession().getServletContext().getRealPath("/")+"upload/"; String fileName = file.getOriginalFilename(); System.out.println(path); File targetFile = new File("D:/my/upload", fileName); if(!targetFile.exists()){ targetFile.mkdirs(); } if(fileName!=null){ introduction.setImageUrl("\\upload\\"+fileName); } //保存 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } introductionService.update(introduction); return "redirect:/introduction/findByObj.do"; } /** * 删除通过主键 * @return */ @RequestMapping(value = "/delete.do") public String delete(Integer id, Model model, HttpServletRequest request, HttpServletResponse response) { //真正删除 introductionService.deleteById(id); //通过参数删除 //Map<String,Object> params = new HashMap<String,Object>(); //params.put("id", id); //introductionService.deleteBySqId("deleteBySql", params); //状态删除 //Introduction load = introductionService.load(id); //load.setIsDelete(1); //introductionService.update(load); return "redirect:/introduction/findByObj.do"; } // --------------------------------------- 华丽分割线 ------------------------------ /** * 分页查询 返回list json(通过对象) * * @param request * @param response * @return */ @RequestMapping(value = "/findByObj.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String findByObjByEntity(Introduction introduction, Model model, HttpServletRequest request, HttpServletResponse response) { //分页查询 Pager<Introduction> pagers = introductionService.findByEntity(introduction); JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("pagers", pagers); jsonObject.put("obj", introduction); return jsonObject.toString(); } /** * 分页查询 返回list json(通过Map) * * @param request * @param response * @return */ @RequestMapping(value = "/findByMap.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String findByMapMap(Introduction introduction, Model model, HttpServletRequest request, HttpServletResponse response) { //通过map查询 Map<String,Object> params = new HashMap<String,Object>(); if(!isEmpty(introduction.getTitle())){ params.put("title", introduction.getTitle()); } if(!isEmpty(introduction.getContent())){ params.put("content", introduction.getContent()); } if(!isEmpty(introduction.getAddTime())){ params.put("addTime", introduction.getAddTime()); } if(!isEmpty(introduction.getImageUrl())){ params.put("imageUrl", introduction.getImageUrl()); } //分页查询 Pager<Introduction> pagers = introductionService.findByMap(params); JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("pagers", pagers); jsonObject.put("obj", introduction); return jsonObject.toString(); } /** * ajax 添加 * @param * @return */ @RequestMapping(value = "/exAdd.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String exAddJson(Introduction introduction, Model model, HttpServletRequest request, HttpServletResponse response) { introductionService.insert(introduction); JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("message", "添加成功"); return jsonObject.toString(); } /** * ajax 修改 * @param * @return */ @RequestMapping(value = "/exUpdate.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String exUpdateJson(Introduction introduction, Model model, HttpServletRequest request, HttpServletResponse response) { introductionService.update(introduction); JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("message", "修改成功"); return jsonObject.toString(); } /** * ajax 删除 * @return */ @RequestMapping(value = "/delete.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String exDeleteJson(Integer id, Model model, HttpServletRequest request, HttpServletResponse response) { //真正删除 introductionService.deleteById(id); //通过参数删除 //Map<String,Object> params = new HashMap<String,Object>(); //params.put("id", id); //introductionService.deleteBySqId("deleteBySql", params); //状态删除 //Introduction load = introductionService.load(id); //load.setIsDelete(1); //introductionService.update(load); JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("message", "删除成功"); return jsonObject.toString(); } /** * 单文件上传 * @param file * @param request * @param model * @return */ @RequestMapping(value = "/saveFile") public String saveFile(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, Model model) { System.out.println("开始"); String path = request.getSession().getServletContext().getRealPath("/upload"); String fileName = file.getOriginalFilename(); System.out.println(path); File targetFile = new File(path, fileName); if(!targetFile.exists()){ targetFile.mkdirs(); } //保存 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } return ""; } /** * springMvc多文件上传 * @param files * @param id * @return */ @RequestMapping(value = "/saveFiles") public String saveFiles(@RequestParam("file") CommonsMultipartFile[] files,Integer id,HttpServletRequest request){ for(int i = 0;i<files.length;i++){ System.out.println("fileName---------->" + files[i].getOriginalFilename()); if(!files[i].isEmpty()){ int pre = (int) System.currentTimeMillis(); try { //拿到输出流,同时重命名上传的文件 String filePath = request.getRealPath("/upload"); File f=new File(filePath); if(!f.exists()){ f.mkdirs(); } String fileNmae=new Date().getTime() + files[i].getOriginalFilename(); File file=new File(filePath+"/"+pre + files[i].getOriginalFilename()); if(!file.exists()){ file.createNewFile(); } files[i].transferTo(file); } catch (Exception e) { e.printStackTrace(); System.out.println("上传出错"); } } } return ""; } }
五,相关作品展示
基于Java开发、Python开发、PHP开发、C#开发等相关语言开发的实战项目
基于Nodejs、Vue等前端技术开发的前端实战项目
基于微信小程序和安卓APP应用开发的相关作品
基于51单片机等嵌入式物联网开发应用
基于各类算法实现的AI智能应用
基于大数据实现的各类数据管理和推荐系统