2021年6月8日:
主要控制层代码:
package com.atguigu.crud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.atguigu.crud.bean.Club;
import com.atguigu.crud.bean.User;
import com.atguigu.crud.service.ClubService;
import com.atguigu.crud.service.UserService;
import com.atguigu.crud.utils.ValidateCodeExpiredException;
import net.sf.json.JSONArray;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
public class MainController {
@Autowired
UserService userService;
@Autowired
ClubService club;
@RequestMapping(value = { "/register" })
public String doregister(User user, HttpServletRequest request) {
// 获取请求的ip地址
user.setRegister_ip(request.getRemoteAddr());
request.setAttribute("msg", "注册成功");
return "forward:/register.jsp";
}
@RequestMapping("/login")
public String dologin(String login_mail, String pwd, Model model, HttpSession session) {
User user = userService.login(login_mail, pwd);
if (user == null) {
model.addAttribute("msg", "用户名或密码错误");
} else {
if (user.getStatus() == 0) {
model.addAttribute("msg", "需激活|" + user.getId());
} else {
session.setAttribute("user", user);
List<Club> list = club.list(user.getId());
session.setAttribute("clublist", JSONArray.fromObject(list));
if (user.getNick_name() == null) {
return "redirect:/user/edit?action=xiu";
}
return "forward:/index.jsp";
}
}
return "forward:/login.jsp";
}
@GetMapping(value = "/active", produces = { "application/json;charset=UTF-8" })
@ResponseBody
public String active(Long userId, String code) {
try {
boolean result = userService.active(userId, code);
if (result) {
return "激活成功!";
} else {
return "验证码错误,请重新去邮箱确认";
}
} catch (ValidateCodeExpiredException e) {
return e.getMessage();
}
}
@GetMapping("/logout.html")
public String logout(HttpSession session) {
session.invalidate();
return "redirect:/login.jsp";
}
@GetMapping("/index")
public String logout1(String club, String user) {
return "forward:/index.jsp?clubname=" + club + "&user=" + user;
}
}