@RestController无法使用redirect跳转到页面的问题

@RestController如果直接使用return "redirect:index";只会返回一个redirect:index并打印到页面上,并不会跳转到index这个页面

正确的方法应该使用ModelAndView这个类,把index.html封装进去,返回到这个类就可以了

@GetMapping("/login")
    public ModelAndView login(@RequestParam String uname, @RequestParam String pwd){
     //实例化并封装一个ModelAndView类 ModelAndView modelAndView
= new ModelAndView("index.html"); //实例化一个jedis对象 Jedis jedis = new Jedis("192.168.44.4",6379); //收到一次用户名视为登录一次,统计登录次数 String count_key="v:"+uname+":count"; String s=jedis.get(count_key); //使用wrapper来封装用户名密码到数据库查询,有结果就登录成功,否则登录失败 QueryWrapper<Users> wrapper = new QueryWrapper<>(); wrapper.eq("uname",uname) .eq("pwd",pwd); Users u = service.getOne(wrapper); if (u!=null){ System.out.println("用户登录成功"+u.getUname()); }else if (s==null){ //如果没有成功,新增一个有效时间是30s的缓存,为了方便只设置了30s,30分钟失效正常应该设置1800s jedis.setex(count_key,30,"1"); System.out.println("登录失败"); }else if (Integer.parseInt(s)<3){//如果登录尝试不足3次,每登录1次,记数增加1 jedis.incr(count_key); System.out.println("登录失败"+Integer.parseInt(s)+"次,超过3次将锁定30分钟"); }else { System.out.println("已失败3次,30分钟后再试"); jedis.close(); }
     //返回这个类
return modelAndView; }

参考文章:https://www.cnblogs.com/jack-zhou21235/p/12624833.html

posted @ 2022-10-23 22:06  伊万  阅读(511)  评论(0编辑  收藏  举报