You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
11 KiB

11 months ago
package com.example.webproject.com.Control;
import com.example.webproject.com.Mapper.account_operator;
import com.example.webproject.com.Mapper.user_data_operator;
import com.example.webproject.com.Pojo.account;
import com.example.webproject.com.Pojo.user_data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@Controller
public class control {
@Autowired
public account_operator account_operator;
@Autowired
public user_data_operator user_data_operator;
//这个是访问登录界面
@GetMapping("/index")
public String test()throws Exception{
System.out.println("index");
// 注意在使用thymleaf时要注意 版本要和starter-web的要配合使用
//否者会跳转不到相应的界面
return "Login";
}
//这里要开始编写我们的对于账号上面的处理了
@RequestMapping("/postProcessorRegistration")
public ModelAndView postProcessorRegistration(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,@RequestParam Map<String,String> map){
System.out.println("postProcessorRegistration");
//这里我们是进行注册操作
//我们先去数据库里面查一下,有没有这个账号
//有就给出错误
//否则就将其插入
//不能获取model 要获取modelMap
// Map<String, Object> model = modelAndView.getModel();
String account = (String)map.get("account");
String password = (String)map.get("password");
ModelAndView modelAndView = new ModelAndView();
account one = account_operator.findOne(account);
System.out.println("数据库中查询的值: "+one);
if(one == null){
System.out.println("数据库中不存在,进行插入操作");
account_operator.insertOne(new account(account,password));
System.out.println("插入完成");
//在这里我们要将账号创建成功的消息显示出来
modelAndView.setViewName("Login");
modelAndView.addObject("isHaveAccount","");
}else{
//在这里我们要将账号已近存在的消息回显
modelAndView.setViewName("Login");
modelAndView.addObject("isHaveAccount","当前账号已有");
}
return modelAndView;
}
//这里要开始编写我们的对于账号上面的处理了
@RequestMapping("/postProcessorLogin")
// @ResponseBody
public ModelAndView postProcessorLogin(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,@RequestParam Map<String,String> map){
System.out.println("postProcessorLogin");
ModelAndView modelAndView = new ModelAndView();
//首先我们先从map中拿到 account和 password
String account = (String)map.get("account");
String password = (String)map.get("password");
System.out.println("account=="+account);
System.out.println("password=="+ password);
account one = account_operator.findOne(account);
if(one == null){
System.out.println("还没有账号,请注册一个");
//将没有账号显示出去,提醒用户注册账号
modelAndView.addObject("isHaveAccount","还没有账号,注册一个");
modelAndView.setViewName("Login");
}else{
if((one.getTxl_password().equals(password))){
System.out.println("密码正确,登入成功");
// modelAndView.setViewName("data_operator");
modelAndView.setViewName("redirect:data");
//在这里要进行coockie的设置在response 里面进行操作
//我们要在登入界面进行操作一旦登入操作成功我们就在这里进行cookie设置
//将cookie设置过去
Cookie account_cookie = new Cookie("account",account);
Cookie password_cookie = new Cookie("password",password);
httpServletResponse.addCookie(account_cookie);
httpServletResponse.addCookie(password_cookie);
}else{
System.out.println("密码错误,登入失败");
modelAndView.setViewName("Login");
}
}
return modelAndView;
}
@RequestMapping("data")
public ModelAndView data_operator(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
//获得所有的cookie
Cookie[]cookies = httpServletRequest.getCookies();
//为cookie设置过期时间
String account = "";
for (Cookie cookie : cookies) {
//当我们设置了cookie之后就只能通过https进行传输了不会在http里面进行传输
// cookie.setSecure(true);
//设置为1星期的过期时间
// cookie.setMaxAge(7*24*60*60);
System.out.println(cookie.getName() + "==" + cookie.getValue());
if("account".equals(cookie.getName())){
account = cookie.getValue();
}
}
//我们就可以设置cookie,当我们发现cookie里面已经存在了账户之后就直接显示当前的这个用户的数据
ModelAndView modelAndView = new ModelAndView();
//这里我们后面肯定是要根据我们当前的一个用户,进行操作的
//我们根据页面上面的显示来进行操作
//页面上面有什么么,我们就要进行什么操作
//就显示一个列表,通过一个参数显示每页 要显示多少的数据,在显示出来总数据的条数
//在每条数据的后面我们就显示,一个删除的按钮,修改,
List<user_data> all = user_data_operator.findOne(account);
modelAndView.addObject("user_data",all);
modelAndView.setViewName("data_operator");
return modelAndView;
}
@RequestMapping("add_user_data")
public ModelAndView add_user_data(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,@RequestParam Map<String,String> map){
System.out.println("add_user_data");
ModelAndView modelAndView = new ModelAndView();
Cookie[]cookies = httpServletRequest.getCookies();
//为cookie设置过期时间
String account = "";
for (Cookie cookie : cookies) {
//当我们设置了cookie之后就只能通过https进行传输了不会在http里面进行传输
// cookie.setSecure(true);
//设置为1星期的过期时间
// cookie.setMaxAge(7*24*60*60);
System.out.println(cookie.getName() + "==" + cookie.getValue());
if("account".equals(cookie.getName())){
account = cookie.getValue();
}
}
//获取数据
String name = map.get("name");
// String sex = map.get("sex");
String contact_phone_number = map.get("contact_phone_number");
if(name != null || contact_phone_number != null){
//插入操作
user_data_operator.insertOne(account,name,contact_phone_number);
modelAndView.addObject("message","插入成功");
System.out.println("here");
//modelAndView.setViewName("redirect:data");
}else{
modelAndView.setViewName("add_user_data");
}
return modelAndView;
}
@RequestMapping("delete_user_data")
public ModelAndView delete_user_data(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,@RequestParam Map<String,String> map){
System.out.println("delete_user_data");
ModelAndView modelAndView = new ModelAndView();
Cookie[]cookies = httpServletRequest.getCookies();
//为cookie设置过期时间
String account = "";
for (Cookie cookie : cookies) {
//当我们设置了cookie之后就只能通过https进行传输了不会在http里面进行传输
// cookie.setSecure(true);
//设置为1星期的过期时间
// cookie.setMaxAge(7*24*60*60);
System.out.println(cookie.getName() + "==" + cookie.getValue());
if("account".equals(cookie.getName())){
account = cookie.getValue();
}
}
String name = map.get("name");
if(name != null ){
//删除操作
// user_data_operator
Integer col = user_data_operator.deleteOne(account,name);
if(col != 0){
modelAndView.addObject("message","删除成功");
}else{
modelAndView.addObject("message","此联系人不存在");
}
System.out.println("here");
// modelAndView.setViewName("redirect:data")
}else{
modelAndView.setViewName("delete_user_data");
}
return modelAndView;
}
@RequestMapping("find_user_data")
public ModelAndView find_user_data(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,@RequestParam Map<String,String> map){
System.out.println("find_user_data");
ModelAndView modelAndView = new ModelAndView();
Cookie[]cookies = httpServletRequest.getCookies();
//为cookie设置过期时间
String account = "";
for (Cookie cookie : cookies) {
//当我们设置了cookie之后就只能通过https进行传输了不会在http里面进行传输
// cookie.setSecure(true);
//设置为1星期的过期时间
// cookie.setMaxAge(7*24*60*60);
System.out.println(cookie.getName() + "==" + cookie.getValue());
if("account".equals(cookie.getName())){
account = cookie.getValue();
}
}
String name = map.get("name");
//查找完成后英爱回显数据
if(name != null ){
//查找操作
List<user_data> oneByName = user_data_operator.findOneByName(account, name);
modelAndView.addObject("message","查找完成");
modelAndView.addObject("user_data",oneByName);
System.out.println("here");
//modelAndView.setViewName("redirect:data");
}else{
modelAndView.setViewName("find_user_data");
}
return modelAndView;
}
}