首页>代码>基于springboot和websocket简单的聊天室>/chat-room-Strong-master/src/main/java/top/wonderheng/chatroom/controller/UserController.java
package top.wonderheng.chatroom.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import top.wonderheng.chatroom.service.UserService; import top.wonderheng.chatroom.vo.User; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Map; @Controller public class UserController { @Autowired private UserService userService; /** * 执行注册 成功后登录页面 否则调回注册页面 */ @PostMapping("/doregister") public ModelAndView register(@RequestParam(value = "username", required = false) String username, @RequestParam(value = "password", required = false) String password, HttpServletResponse response) { PrintWriter out = null; try { response.setContentType("text/html;charset=utf-8"); out = response.getWriter(); } catch (IOException e) { e.printStackTrace(); } if (registerUser(username) == true) { User user1 = new User(); user1.setUsername(username); user1.setPassword(password); user1.setCreated_date(UDateToLocalDateTime()); userService.save(user1); //注册成功,重定向登录页面 out.print("<script language=\"javascript\">alert('注册成功,欢迎使用!');</script>"); return new ModelAndView("index"); } else { //失败重定向注册页面 out.print("<script language=\"javascript\">alert('注册失败,请稍后再试!');</script>"); return new ModelAndView("registration"); } } /** * 跳转登录页面 * * @return */ @RequestMapping("/tryLogin") public ModelAndView login() { return new ModelAndView("index"); } /** * 跳转注册页面 * * @return */ @RequestMapping("/tryregistration") public ModelAndView registration() { return new ModelAndView("registration"); } public Boolean registerUser(String username) { if (userService.findByUsername(username).isEmpty()) { return true; } else { return false; } } /** * 执行登录 */ @PostMapping("/dologin") public ModelAndView login(@RequestParam(value = "username", required = false) String username, @RequestParam(value = "password", required = false) String password, Map<String, String> map, HttpServletResponse response) { User user = userService.findByUsernameAndPassword(username, password); PrintWriter out = null; try { response.setContentType("text/html;charset=gb2312"); out = response.getWriter(); } catch (IOException e) { e.printStackTrace(); } finally { if (user != null) { map.put("username", username); //登陆成功,重定向聊天页面 return new ModelAndView("chat", map); } else { //失败重定向登录页面 out.print("<script language=\"javascript\">alert('用户名或密码错误,请重试!');</script>"); return new ModelAndView("index"); } } } public LocalDateTime UDateToLocalDateTime() { java.util.Date date = new java.util.Date(); Instant instant = date.toInstant(); ZoneId zone = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone); return localDateTime; } }
最近下载更多
Xiaobaiya11 LV2
3月4日
admin_z LV22
2023年12月21日
anninghh LV1
2023年12月4日
西瓜哥哥 LV4
2023年8月9日
gzryue LV6
2023年4月25日
天险无涯 LV15
2023年3月20日
微信网友_6393440821071872 LV2
2023年3月17日
zhaoxu123123 LV10
2022年12月4日
xiaoyuer2 LV8
2022年11月22日
万盛拓海 LV2
2022年10月20日
最近浏览更多
黑 LV6
9小时前
Xiaobaiya11 LV2
3月4日
uni-code_0123 LV1
1月31日
YhXyHx523 LV4
2023年12月24日
admin_z LV22
2023年12月21日
微信网友_6762641760833536
2023年12月4日
暂无贡献等级
anninghh LV1
2023年12月4日
skook7 LV2
2023年11月21日
杨最代1 LV5
2023年10月31日
lilong007 LV20
2023年10月19日