package yaohao.controller;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import yaohao.dao.impl.UserInfoDaoImpl;
import yaohao.pojo.*;
import yaohao.utils.Page;
@Controller
public class UserInfoController {
@RequestMapping("addUserInfo")
public String addUserInfo(UserInfo userinfo,MultipartFile myfile, HttpServletRequest req) throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
//用于文件上传
String filename = myfile.getOriginalFilename();
//获取服务器中upload文件夹的物理路径
String path = req.getSession().getServletContext().getRealPath("upload");
String newfilename = UUID.randomUUID().toString()+filename.substring(filename.lastIndexOf("."));
//将上传文件的文件名保存到pojo,最后传到数据库中
//如果缺少这句代码,则数据库中的用户图片名称为:null
userinfo.setPicture(newfilename);
dao.insert(userinfo);
//实现文件上传
File file = new File(path + "/" + newfilename);
myfile.transferTo(file);
return "index";
}
@RequestMapping("list")
public String selectAll(Model model, @RequestParam(defaultValue="0") Integer page,@RequestParam(defaultValue="2") Integer rows) throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
Page<UserInfo> pageList = dao.selectAll((page>0)?page-1:page,rows);
model.addAttribute("page",pageList);
return "/list";
}
@RequestMapping("checkName")
public @ResponseBody Map<String, String> checkName( HttpServletRequest req) throws Exception{
String username = req.getParameter("username");
System.out.println(req.getParameter("username"));
Map<String, String> map = new HashMap<String, String>();
if(username.equals("admin")){
map.put("info", "该用户已经存在啊");
}
else{
map.put("info", "该用户不存在啊");
}
return map;
}
@RequestMapping("checkNameJson")
public @ResponseBody UserInfo checkNameJson(@RequestBody UserInfo ui ) throws Exception{
UserInfo userinfo = new UserInfo();
if(ui.getUserName().equals("admin")){
userinfo.setLoves("已经被注册");
}
else{
userinfo.setLoves("可以注册");
}
return userinfo;
}
@RequestMapping("deleteUserInfo.action/{id}")
public String deleteUserInfo(@PathVariable("id") Integer id ) throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
dao.delete(id);
return "redirect:/list.action";
}
@RequestMapping("updateUserInfo.action")
public String updateInfo(UserInfo userinfo,MultipartFile myfile, HttpServletRequest req ) throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
//用于文件上传
String filename = myfile.getOriginalFilename();
if(filename.trim().length()>0){
//获取服务器中upload文件夹的物理路径
String path = req.getSession().getServletContext().getRealPath("upload");
String newfilename = UUID.randomUUID().toString()+filename.substring(filename.lastIndexOf("."));
//将上传文件的文件名保存到pojo,最后传到数据库中
//如果缺少这句代码,则数据库中的用户图片名称为:null
userinfo.setPicture(newfilename);
//实现文件上传
File file = new File(path + "/" + newfilename);
myfile.transferTo(file);
}
//执行修改
dao.update(userinfo);
return "redirect:/list.action";
}
@RequestMapping("toUpdateUserInfo.action/{userId}")
public String updateInfo(@PathVariable("userId") Integer userId,Model model ) throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
UserInfo ui = dao.selectOne(userId);
model.addAttribute("userinfo",ui);
return "/updateUserInfo";
}
@RequestMapping("testAjax")
@ResponseBody
public List<UserInfo> test() throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
//List<UserInfo> list = dao.selectAll(1,5);
return null;
}
}
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import yaohao.dao.impl.UserInfoDaoImpl;
import yaohao.pojo.*;
import yaohao.utils.Page;
@Controller
public class UserInfoController {
@RequestMapping("addUserInfo")
public String addUserInfo(UserInfo userinfo,MultipartFile myfile, HttpServletRequest req) throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
//用于文件上传
String filename = myfile.getOriginalFilename();
//获取服务器中upload文件夹的物理路径
String path = req.getSession().getServletContext().getRealPath("upload");
String newfilename = UUID.randomUUID().toString()+filename.substring(filename.lastIndexOf("."));
//将上传文件的文件名保存到pojo,最后传到数据库中
//如果缺少这句代码,则数据库中的用户图片名称为:null
userinfo.setPicture(newfilename);
dao.insert(userinfo);
//实现文件上传
File file = new File(path + "/" + newfilename);
myfile.transferTo(file);
return "index";
}
@RequestMapping("list")
public String selectAll(Model model, @RequestParam(defaultValue="0") Integer page,@RequestParam(defaultValue="2") Integer rows) throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
Page<UserInfo> pageList = dao.selectAll((page>0)?page-1:page,rows);
model.addAttribute("page",pageList);
return "/list";
}
@RequestMapping("checkName")
public @ResponseBody Map<String, String> checkName( HttpServletRequest req) throws Exception{
String username = req.getParameter("username");
System.out.println(req.getParameter("username"));
Map<String, String> map = new HashMap<String, String>();
if(username.equals("admin")){
map.put("info", "该用户已经存在啊");
}
else{
map.put("info", "该用户不存在啊");
}
return map;
}
@RequestMapping("checkNameJson")
public @ResponseBody UserInfo checkNameJson(@RequestBody UserInfo ui ) throws Exception{
UserInfo userinfo = new UserInfo();
if(ui.getUserName().equals("admin")){
userinfo.setLoves("已经被注册");
}
else{
userinfo.setLoves("可以注册");
}
return userinfo;
}
@RequestMapping("deleteUserInfo.action/{id}")
public String deleteUserInfo(@PathVariable("id") Integer id ) throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
dao.delete(id);
return "redirect:/list.action";
}
@RequestMapping("updateUserInfo.action")
public String updateInfo(UserInfo userinfo,MultipartFile myfile, HttpServletRequest req ) throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
//用于文件上传
String filename = myfile.getOriginalFilename();
if(filename.trim().length()>0){
//获取服务器中upload文件夹的物理路径
String path = req.getSession().getServletContext().getRealPath("upload");
String newfilename = UUID.randomUUID().toString()+filename.substring(filename.lastIndexOf("."));
//将上传文件的文件名保存到pojo,最后传到数据库中
//如果缺少这句代码,则数据库中的用户图片名称为:null
userinfo.setPicture(newfilename);
//实现文件上传
File file = new File(path + "/" + newfilename);
myfile.transferTo(file);
}
//执行修改
dao.update(userinfo);
return "redirect:/list.action";
}
@RequestMapping("toUpdateUserInfo.action/{userId}")
public String updateInfo(@PathVariable("userId") Integer userId,Model model ) throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
UserInfo ui = dao.selectOne(userId);
model.addAttribute("userinfo",ui);
return "/updateUserInfo";
}
@RequestMapping("testAjax")
@ResponseBody
public List<UserInfo> test() throws Exception{
UserInfoDaoImpl dao = new UserInfoDaoImpl();
//List<UserInfo> list = dao.selectAll(1,5);
return null;
}
}