欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UserController.java  

程序员文章站 2022-07-03 23:10:41
...

package org.travelzen.hotel.user.controller;

 

import java.io.File;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

 

import javax.servlet.http.HttpServletRequest;

import javax.validation.Valid;

 

import org.apache.commons.io.FileUtils;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.ExceptionHandler;

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.bind.annotation.SessionAttributes;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import org.springframework.web.servlet.view.InternalResourceViewResolver;

import org.travelzen.hotel.user.exception.UserException;

import org.travelzen.hotel.user.vo.User;

 

@Controller

@RequestMapping("/travelzen/hotel")

@SessionAttributes("loginUser")

public class UserController {

 

private final static Map<String, User> users = new HashMap<String ,User>();

//private final static List<User> users = new ArrayList<User>();

 

public UserController() {

 users.put("ldh", new User("ldh", "123", "刘德华", "ldh@travelzen.com"));

users.put("zxy", new User("zxy", "123", "张学友", "zxy@travelzen.com"));

users.put("lm", new User("lm", "123", "黎明", "lm@travelzen.com"));

users.put("gfc", new User("gfc", "123", "郭富城", "gfc@travelzen.com"));

users.put("zjl", new User("zjl", "123", "周杰伦", "zjl@travelzen.com"));

}

 

@RequestMapping(value="/users", method=RequestMethod.GET)

public String list(Model model) {

if( ! model.containsAttribute("loginUser")) {

return "redirect:/travelzen/hotel/login";

}

model.addAttribute("users", users);//key为map

return "travelzen/hotel/user/list";

}

 

@RequestMapping(value="/user/add", method=RequestMethod.GET)

public String add(Model model) {

model.addAttribute(new User());//User-user

return "travelzen/hotel/user/add";

}

 

@RequestMapping(value="/user/add", method=RequestMethod.POST)

public String add(@Valid User user, BindingResult binding, @RequestParam(required=false) MultipartFile[] photos, HttpServletRequest req, Model model) throws IOException {

if(binding.hasErrors()) {

//接受错误信息

//return "redirect:/travel/hotel/user/add";

return "travelzen/hotel/user/add";

}

for(MultipartFile photo : photos) {

if(photo.isEmpty()) continue;

System.out.println(photo.getContentType() + ", " + photo.getName() + ", " + photo.getOriginalFilename());

String realPath = req.getSession().getServletContext().getRealPath("/resources/upload/");

FileUtils.copyInputStreamToFile(photo.getInputStream(), new File(realPath + "/" + photo.getOriginalFilename()));

}

users.put(user.getUserName(), user);

model.addAttribute("loginUser", user);

return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/travelzen/hotel/users";

}

 

//显示用户

@RequestMapping(value="/user/{userName}", method=RequestMethod.GET)

public String show(@PathVariable String userName, Model model) {

model.addAttribute(users.get(userName));

return "travelzen/hotel/user/show";

}

 

@ResponseBody

@RequestMapping(value="/user/{userName}", params="json", method=RequestMethod.GET)

//public User showJson(@PathVariable String userName, @RequestParam String json, Model model) {

public User showJson(@PathVariable String userName) {

return users.get(userName);

}

 

@RequestMapping(value="/user/{userName}/update", method=RequestMethod.GET)

public String update(@PathVariable String userName, Model model) {

model.addAttribute(users.get(userName));

return "travelzen/hotel/user/update";

}

 

@RequestMapping(value="/user/{userName}/update", method=RequestMethod.POST)

public String update(@PathVariable String userName, @Valid User user, BindingResult binding, Model model) {

if(binding.hasErrors()) {

//return "travelzen/hotel/user/user/update";

}

users.put(user.getUserName(), user);

return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/travelzen/hotel/users";

}

 

@RequestMapping(value="/user/{userName}/delete", method=RequestMethod.GET)

public String delete(@PathVariable String userName) {

users.remove(userName);

return "redirect:/travelzen/hotel/users";

}

 

@RequestMapping(value="/login", method=RequestMethod.GET)

public String login() {

return "travelzen/hotel/user/login";

}

 

@RequestMapping(value="/login", method=RequestMethod.POST)

public String login(String userName, String password, Model model) {

if( ! users.containsKey(userName)) {

throw new UserException("用户名不存在");

}

if( ! password.equals(users.get(userName).getPassword())) {

throw new UserException("用户密码不正确");

}

model.addAttribute("loginUser", users.get(userName));

return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/travelzen/hotel/users";

}

 

@ExceptionHandler(value={UserException.class})

public String handlerException(Exception e, HttpServletRequest req) {

req.setAttribute("e", e);

return "travelzen/hotel/error";

}

 

@RequestMapping(value="/logout", method=RequestMethod.GET)

public String logout() {

return "travelzen/hotel/user/logout";

}

 

@RequestMapping("/redir")

public String redir(Model model, RedirectAttributes ra) {

ra.addFlashAttribute("tttt", "吞吞吐吐");

//model.addAttribute("tttt", "吞吞吐吐");

return "redirect:/travelzen/hotel/users";

}

 

}