package controller; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import model.User; import model.UserException; import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ModelAttribute; 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.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.sun.org.glassfish.gmbal.ParameterNames; @Controller @RequestMapping("/user" ) public class UserController { private Map<String,User> users = new HashMap<String,User>(); public UserController() { users.put("haha" ,new User("haha","haha" ,"哈哈" ,"sss" )); users.put("hehe" ,new User("hehe","hehe" ,"呵呵 " ,"sss" )); users.put("nimei" ,new User("nimei","nimei" ,"你妹" ,"sss" )); users.put("zhangquandan" ,new User("zhangquandan" ,"张全蛋" ,"哈哈" ,"sss" )); } /** * 遍历用户 * @param model * @return */ @RequestMapping(value= "/users",method=RequestMethod.GET) public String list(Model model){ model.addAttribute("users" ,users ); return "user/list" ; } //链接到add页面时是GET请求,会访问这段代码 // @RequestMapping(value="/add",method=RequestMethod.GET) // public String add(Model model){ // //开启modelDriven // model.addAttribute(new User()); // return "user/add"; // } /** * 进入添加页面 * @param user * @return */ @RequestMapping(value= "/add",method=RequestMethod.GET) public String add( @ModelAttribute("user" ) User user){ return "user/add" ; } /** * 添加用户 上传单个文件 * @param user * @param br * @return * @throws IOException */ /* //在具体添加用户时,是post请求时,就访问以下代码 @RequestMapping(value="/add",method=RequestMethod.POST) public String add(@Validated User user,BindingResult br,MultipartFile image,HttpServletRequest request) throws IOException{//一定要紧跟validated之后写验证结果类 if(br.hasErrors()) { //如果有错误直接返回页面 return "user/add"; } String realPath = request.getSession().getServletContext().getRealPath("/resources/upload"); System.out.println(realPath); File file = new File(realPath+"/"+image.getOriginalFilename()); FileUtils.copyInputStreamToFile(image.getInputStream(), file); System.out.println(image.getName()+","+image.getOriginalFilename()+","+image.getContentType()); users.put(user.getUserName(), user); //UrlBasedViewResolver接口 return "redirect:/user/users"; }*/ /** * 添加用户 上传多个文件 * @param user * @param br * @return * @throws IOException */ //在具体添加用户时,是post请求时,就访问以下代码 @RequestMapping(value= "/add",method=RequestMethod.POST) public String add( @Validated User user,BindingResult br ,@RequestParam ("images" ) MultipartFile[] images,HttpServletRequest request ) throws IOException{//一定要紧跟validated之后写验证结果类 if(br .hasErrors()) { //如果有错误直接返回页面 return "user/add" ; } String realPath = request.getSession().getServletContext().getRealPath("/resources/upload"); System. out.println(realPath ); for(MultipartFile image : images) { if(image .isEmpty()) { continue; } File file = new File(realPath+"/" +image .getOriginalFilename()); FileUtils. copyInputStreamToFile(image.getInputStream(), file); } users.put(user .getUserName(), user); //UrlBasedViewResolver接口 return "redirect:/user/users" ; } /** * 查看用户详细信息 * @param userName * @param model * @return */ @RequestMapping(value= "/{userName}",method=RequestMethod.GET) public String show( @PathVariable String userName,Model model ) { model.addAttribute(users .get(userName)); return "user/show" ; }
json的代码
@RequestMapping(value= "/{userName}",method=RequestMethod.GET,params="json") @ResponseBody public User show( @PathVariable String userName ) { return users .get(userName); }
/** * 进入修改页面 * @param userName * @param model * @return */ @RequestMapping(value= "/{userName}/update",method=RequestMethod.GET) public String update( @PathVariable String userName,Model model ) { model.addAttribute(users .get(userName)); return "user/update" ; } /** * 修改用户数据 * @param user * @param userName * @param br * @return */ @RequestMapping(value= "/{userName}/update",method=RequestMethod.POST) public String udpate( @PathVariable String userName,@Validated User user,BindingResult br) { if(br .hasErrors()) { return "user/update" ; } users.put(userName ,user ); return "redirect:/user/users" ; } @RequestMapping(value= "/{userName}/delete",method=RequestMethod.GET) public String delete( @PathVariable String userName) { users.remove(userName ); return "redirect:/user/users" ; } @RequestMapping(value= "/login",method=RequestMethod.POST) public String login(String userName,String password,HttpSession session ) { if(!users .containsKey(userName)) { throw new UserException("用户名不存在 "); } User u = users.get(userName ); if(!u .getPassword().equals(password)) { throw new UserException("用户密码不正确"); } session.setAttribute("loginUser" , u ); return "redirect:/user/users" ; } /** * 局部异常处理,仅仅只能处理这个控制器的中的异常 * */ /* @ExceptionHandler(value={UserException.class}) public String handlerException(UserException e,HttpServletRequest request) { request.setAttribute("e", e); return "user/error"; }*/ }
架包
jackjson包:
http://pan.baidu.com/s/1mgDTboW
截图:
源码:
http://pan.baidu.com/s/1eQxkqKa