1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| package com.controller; import com.domain.PageBean; import com.domain.User; import com.service.impl.UserServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.SessionAttributes;
import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Map; import java.util.Random;
@Controller @SessionAttributes("loginUser") public class UserController {
@Autowired private UserServiceImpl service;
@RequestMapping("findUserByPage") public String findAll(@RequestParam Map<String,Object> map, Model model){ PageBean pb = new PageBean(); if(map.get("currentPage")==null){ pb.setCurrentPage(1); }else{ pb.setCurrentPage(Integer.parseInt(map.get("currentPage").toString())); } if(map.get("name")!=null){ pb.setName(map.get("name").toString()); }else{ pb.setName(""); } if(map.get("address")!=null){ pb.setAddress(map.get("address").toString()); }else{ pb.setAddress(""); } if(map.get("email")!=null){ pb.setEmail(map.get("email").toString()); }else{ pb.setEmail(""); } model.addAttribute("pb", service.findUserByPage(pb)); return "list"; };
@RequestMapping("checkCode") public void checkCode(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader("pragma","no-cache"); response.setHeader("cache-control","no-cache"); response.setHeader("expires","0");
int width = 80; int height = 30; BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics(); g.setColor(Color.GRAY); g.fillRect(0,0, width,height);
String base = "0123456789ABCDEFGabcdefg"; int size = base.length(); Random r = new Random(); StringBuffer sb = new StringBuffer(); for(int i=1;i<=4;i++){ int index = r.nextInt(size); char c = base.charAt(index); sb.append(c); } String checkCode = sb.toString(); System.out.println(checkCode); request.getSession().setAttribute("CHECKCODE_SERVER",checkCode);
g.setColor(Color.YELLOW); g.setFont(new Font("黑体",Font.BOLD,24)); g.drawString(checkCode,15,25);
ImageIO.write(image,"PNG",response.getOutputStream()); }
@RequestMapping("login") public String login(String username, String password, String verifycode, HttpSession session, Model model){ if("".equals(username)|| "".equals(password)|| "".equals(verifycode)){ model.addAttribute("error","请输入信息"); return "login"; } if(!verifycode.equalsIgnoreCase(session.getAttribute("CHECKCODE_SERVER").toString())){ model.addAttribute("error","验证码错误"); return "login"; } User user = service.login(username, password); if(user==null){ model.addAttribute("error","用户名或密码错误"); return "login"; } session.setAttribute("loginUser", user); return "index"; }
@RequestMapping("addUser") public String addUser(User user){ service.add(user); return "redirect:findUserByPage"; }
@RequestMapping("delUser") public String delUser(String id){ service.delUser(id.toString()); return "redirect:findUserByPage"; }
@RequestMapping("delSelected") public String delSelected(@RequestParam("uid") String[] ids){ for (String id : ids) { service.delUser(id); } return "redirect:findUserByPage"; }
@RequestMapping("findUser") public String findUser(String id, Model model){ User user = service.findUser(id); model.addAttribute("u",user); return "update"; }
@RequestMapping("update") public String update(User user){ service.update(user); return "redirect:findUserByPage"; } }
|