首页>代码>spring+spring mvc+mybatis整合实现用户登录注册功能(idea用maven搭建)>/ssm-02/src/main/java/neusoft/controller/UserController.java
package neusoft.controller; import neusoft.pojo.User; import neusoft.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.servlet.http.HttpSession; import javax.validation.Valid; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; //@PostMapping("/login")http://localhost:8080/ssm-02/user/login @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(String name, String password, Model model, HttpSession session) { System.out.println("login:::name:::" + name + ", password" + password); User bean = userService.login(name, password); if (bean != null) { //登录成功 //session保存 session.setAttribute("loginUser", bean); //记住密码,使用Cookie }else { //登录失败 model.addAttribute("msg", "用户名或密码错误"); //return "forward:login.jsp";//http://localhost:8080/ssm-02/user/login.jsp return "forward:../login.jsp"; } return "index"; } @PostMapping("/reg") public String reg(@Valid User bean, BindingResult br, String cfmpassword, Model model) { System.out.println("reg:::" + bean); System.out.println("reg:::" + cfmpassword); //判断两次密码是否一致 if(!bean.getPassword().equals(cfmpassword)) { model.addAttribute("msg", "两次输入密码不一致"); return "forward:../reg.jsp"; } //数据较验 if(br.hasErrors()) { Map<String, String> errors = new HashMap<String, String>(); System.out.println("==============================表单数据出错================================="); List<FieldError> fieldErrors = br.getFieldErrors(); for (FieldError item : fieldErrors) { System.out.println(item.getField()); System.out.println(item.getDefaultMessage()); errors.put(item.getField(), item.getDefaultMessage()); } model.addAttribute("errors", errors); return "forward:../reg.jsp"; } //判断用户名重复 if (userService.getByName(bean.getName()) != null) { model.addAttribute("msg", "该用户名已经被使用"); return "forward:../reg.jsp"; } boolean result = userService.insert(bean); if (result) { System.out.println("注册成功"); return "forward:../login.jsp"; } System.out.println("注册失败"); model.addAttribute("msg", "注册失败"); return "forward:../reg.jsp"; } }
最近下载更多
Sul_ALL LV1
3月20日
微信网友_6802079662936064 LV3
2023年12月31日
微信网友_6738347199975424
2023年11月16日
暂无贡献等级
微信网友_6722962072784896 LV1
2023年11月5日
logitch LV1
2023年10月10日
1289755 LV3
2023年7月30日
ljkljk332 LV1
2023年6月29日
微信网友_6536139133505536 LV1
2023年6月26日
saury7 LV1
2023年6月19日
微信网友_6522850944520192 LV1
2023年6月17日
最近浏览更多
微信网友_6512020493586432 LV2
4月12日
Sul_ALL LV1
3月16日
songsmm LV1
3月11日
微信网友_6802079662936064 LV3
2023年12月31日
1QAZWSX
2023年12月28日
暂无贡献等级
wwwww816 LV3
2023年12月19日
yexinggg
2023年12月15日
暂无贡献等级
hamberduan LV1
2023年12月10日
微信网友_6753981327003648
2023年11月27日
暂无贡献等级
微信网友_6738347199975424
2023年11月16日
暂无贡献等级