首页>代码>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";
}
}
最近下载更多
微信网友_7547316598050816 LV1
6月10日
xiubiaoya LV1
6月1日
vincemokea LV9
4月23日
22048042 LV1
4月9日
哪里的完整版 LV8
3月1日
zhoubowen LV3
2024年12月29日
lee041022 LV1
2024年12月13日
王高阳 LV1
2024年10月9日
qiwwwww LV2
2024年6月20日
Sul_ALL LV1
2024年3月20日
最近浏览更多
微信网友_7547316598050816 LV1
6月10日
241404
6月6日
暂无贡献等级
xiubiaoya LV1
6月1日
微信网友_7516989224996864 LV1
5月19日
vincemokea LV9
4月23日
22048042 LV1
4月9日
哪里的完整版 LV8
3月1日
zhoubowen LV3
2024年12月29日
lee041022 LV1
2024年12月13日
xiaoaitx LV8
2024年11月22日

