失物招领|基于Web的校园失物招领系统的设计与实现(一)https://developer.aliyun.com/article/1423254
四,核心代码展示
package com.znz.ssm.blog.controller.admin; import com.znz.ssm.blog.entity.Category; import com.znz.ssm.blog.service.ArticleService; import com.znz.ssm.blog.service.CategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import java.util.List; /** * @author znz */ @Controller @RequestMapping("/admin/category") public class BackCategoryController { @Autowired private ArticleService articleService; @Autowired private CategoryService categoryService; /** * 后台分类列表显示 * * @return */ @RequestMapping(value = "") public ModelAndView categoryList() { ModelAndView modelandview = new ModelAndView(); List<Category> categoryList = categoryService.listCategoryWithCount(); modelandview.addObject("categoryList",categoryList); modelandview.setViewName("Admin/Category/index"); return modelandview; } /** * 后台添加分类提交 * * @param category * @return */ @RequestMapping(value = "/insertSubmit",method = RequestMethod.POST) public String insertCategorySubmit(Category category) { categoryService.insertCategory(category); return "redirect:/admin/category"; } /** * 删除分类 * * @param id * @return */ @RequestMapping(value = "/delete/{id}") public String deleteCategory(@PathVariable("id") Integer id) { //禁止删除有文章的分类 int count = articleService.countArticleByCategoryId(id); if (count == 0) { categoryService.deleteCategory(id); } return "redirect:/admin/category"; } /** * 编辑分类页面显示 * * @param id * @return */ @RequestMapping(value = "/edit/{id}") public ModelAndView editCategoryView(@PathVariable("id") Integer id) { ModelAndView modelAndView = new ModelAndView(); Category category = categoryService.getCategoryById(id); modelAndView.addObject("category",category); List<Category> categoryList = categoryService.listCategoryWithCount(); modelAndView.addObject("categoryList",categoryList); modelAndView.setViewName("Admin/Category/edit"); return modelAndView; } /** * 编辑分类提交 * * @param category 分类 * @return 重定向 */ @RequestMapping(value = "/editSubmit",method = RequestMethod.POST) public String editCategorySubmit(Category category) { categoryService.updateCategory(category); return "redirect:/admin/category"; } }
package com.czmec.action; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.czmec.bean.WordsBean; import com.czmec.service.WordsService; //用户操作 @Controller @RequestMapping("userAction") public class UserAction { @Resource WordsService wordsservice; //主页面 @RequestMapping("main") public String UserMain(){ return "main"; } //感恩 @RequestMapping("WordsList") public String WordsList(HttpServletRequest request){ List<WordsBean> list= wordsservice.SelectAllWords(); System.out.println(list); request.setAttribute("wordslist",list); return "WordsList"; } //添加一条感恩信 @RequestMapping("addWords") public String AddWords(WordsBean wBean,HttpServletRequest request){ wordsservice.AddWords(wBean); List<WordsBean> list= wordsservice.SelectAllWords(); System.out.println(list); request.setAttribute("wordslist",list); return "WordsList"; } //删除一条感恩信 @RequestMapping("DelWords") public String DelWords(WordsBean wBean,HttpServletRequest request){ wordsservice.DelWords(wBean); List<WordsBean> list= wordsservice.SelectAllWords(); System.out.println(list); request.setAttribute("wordslist",list); return "WordsList"; } }
package com.czmec.action; import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.List; import java.util.Random; import javax.annotation.Resource; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.czmec.bean.UserBean; import com.czmec.service.AdminService; import com.czmec.service.UserService; /* * 用户登录action */ @Controller @RequestMapping("user") public class UserLogin { @Resource(name="adminService") AdminService admiservice; @Resource(name="userService") private UserService userservice; @RequestMapping("login") public String login(){ return "login"; } @RequestMapping("checkLogin") public String checkLogin(UserBean user,@Param("checkcode")String checkcode,HttpSession session,HttpServletRequest request){ String piccode=(String) request.getSession().getAttribute("piccode"); checkcode=checkcode.toUpperCase();//全部改成大写 if(checkcode.equals(piccode)){ UserBean user1=userservice.login(user); if(user1!=null){ if(user1.getRole()==0){ session.setAttribute("countfound", admiservice.SelectCountFoundGoods()); session.setAttribute("countlost",admiservice.SelectCountLostGoods()); session.setAttribute("countuser", admiservice.SelectCountUser()); session.setAttribute("user", user1); List<UserBean> list= admiservice.SelectUser(); session.setAttribute("listUser", list); return "/admin/index"; }else { session.setAttribute("user", user1); return "index"; } } else { session.setAttribute("error", "用户名或密码错误"); return "Error"; } } else { request.setAttribute("image", "验证码不正确"); return "login"; } } @RequestMapping("error") public String Error(){ return "Error"; } //用户注册界面 @RequestMapping("register") public String register(){ return "register"; } //用户注册界面功能 @RequestMapping("registerAction") public String registerAction(UserBean user,HttpServletRequest req){ user.setRole(1); int i= userservice.AddUser(user); if (i>0) { req.setAttribute("message", "注册成功"); }else{ req.setAttribute("message", "注册失败"); } return "login"; } //用户退出系统 @RequestMapping("out") public String out(){ return "login"; } //发布寻物信息 @RequestMapping("AddLost") public String Addlost(){ return "AddLost"; } //验证码的实现 @RequestMapping("image") public void Image(HttpServletRequest request,HttpServletResponse response) throws IOException{ BufferedImage bi=new BufferedImage(68,22,BufferedImage.TYPE_INT_RGB); Graphics g=bi.getGraphics(); Color c=new Color(200,150,255); g.setColor(c); //背景 g.fillRect(0, 0, 68, 22); char[] ch="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); //随机获取4个字符 Random r=new Random(); //范围数组,随机的位置 int len=ch.length,index; StringBuffer sb=new StringBuffer(); for(int i=0;i<4;i++) { //获取随机数 index=r.nextInt(len);//获得一个随机的位置,一个字符 //放在图片上 g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));//给个颜色 //字符画在图片上,及位置 g.drawString(ch[index]+"", (i*15)+3, 18); //字符保存StringBuffer sb.append(ch[index]); } request.getSession().setAttribute("piccode", sb.toString()); //输出 ImageIO.write(bi, "JPG", response.getOutputStream()); } }
五,项目总结
校园失物招领系统使得用户更加方便、有效的找回丢失物品,也使得管理者在工作的时候更加简单、轻松,通过合理的分配人力、物力资源,提高校园失物招领工作的效率和质量。
主要特点为:
①界面简洁美观,容易上手。
②硬件要求低,容易维护。