ajaxcontroller

本文介绍了一个基于Spring框架的用户信息管理系统,包括用户信息的增删改查及头像上传功能。文章详细展示了如何使用MultipartFile进行文件上传,并通过UUID确保文件名唯一性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值