package com.xujd.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 com.xujd.model.User;
import com.xujd.service.UserService;
@Controller
@RequestMapping("/bg/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 用户登入
* @param request
* @return
*/
/*@SuppressWarnings("null")
@RequestMapping(value="/login")
public String login(HttpServletRequest request,HttpServletResponse response,User user){
HttpSession session = request.getSession();
User u = new User();
u = userService.login(user);
if(u==null||u.equals("")){
return "/login";
}else{
session.setAttribute("user_session", u);
return "redirect:/bg/user/getAllUser";
}
}*/
/**
* 获取所有用户列表
* @param request
* @return
*/
@RequestMapping("/getAllUser")
public String getAllUser(HttpServletRequest request,Model model){
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user_session");
//System.out.println("sessionID:"+session.getId());
//System.out.println("session中数据为:id:"+user.getId()+"---name:"+user.getName()+"---gae:"+user.getAge()+"---loginName:"+user.getLoginName()+"---password:"+user.getPassword());
List<User> findAll = userService.findAll();
model.addAttribute("userList", findAll);
/*request.setAttribute("userList", findAll);*/
return "/user/allUser";
}
/**
* 跳转到添加用户界面
* @param request
* @return
*/
@RequestMapping("/toAddUser")
public String toAddUser(User user,Model model){
return "/user/addUser";
}
/**
* 添加用户并重定向
* @param user
* @param request
* @return
*/
@RequestMapping("/addUser")
public String addUser(User user,HttpServletRequest request,Model model){
userService.save(user);
//request.setAttribute("succ","succ");
//model.addAttribute("succ", "succ");
return "redirect:getAllUser";
}
@RequestMapping("/editUser")
public String editUser(int id,HttpServletRequest request,Model model){
request.setAttribute("user", userService.findById(id));
return "/user/editUser";
}
/**
*编辑用户
* @param user
* @param request
* @return
*/
@RequestMapping("/updateUser")
public String updateUser(User user,Model model,HttpServletRequest request){
if(userService.update(user)){
model.addAttribute("succ", "succ");
return "redirect:getAllUser";
}else{
return "/error";
}
}
/**
* 根据id查询单个用户
* @param id
* @param request
* @return
*/
@RequestMapping("/getUser")
public String getUser(int id,HttpServletRequest request){
request.setAttribute("user", userService.findById(id));
return "/user/editUser";
}
/**
* 删除用户
* @param id
* @param request
* @param response
*/
@RequestMapping("/delUser")
public void delUser(int id,HttpServletRequest request,HttpServletResponse response,Model model){
String result = "{\"result\":\"error\"}";
if(userService.delete(id)){
result = "{\"result\":\"success\"}";
}else{
model.addAttribute("error", "error");
}
response.setContentType("application/json");
try {
PrintWriter out = response.getWriter();
out.write(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}