直接访问department/list没有跳转到to_login页面

直接访问department/list没有跳转到to_login页面

直接访问department/list没有跳转到to_login页面,反而跳转到地址department/to_login了,这是为什么,为什么没有重定向到to_login页面,前面的department怎么来的?

@Controller("globalController")
public class GlobalController {
    @Autowired
    private GlobalBiz globalBiz;

    @RequestMapping("/to_login")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(HttpSession session, @RequestParam String sn, @RequestParam String password){
        Employee employee =globalBiz.login(sn,password);
        if(employee ==null){
            return "redirect:to_login";
        }
        session.setAttribute("employee",employee);
        return "redirect:self";
    }
    @RequestMapping("/self")
    public String self(){
        return "self";
    }


public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果url中带有login ,直接放行(登陆相关的路径放行)
        String url = request.getRequestURI();
        if (url.toLowerCase().indexOf("login") >=0){
            return true;
        }
        //不是登陆相关的路径,登陆成功后,才能访问(session中存在员工对象,则登陆成功放行!)
        HttpSession session = request.getSession();
        if(session.getAttribute("employee")!= null){
            return true;
        }
        //剩下的情况就不放行了。重定向到重新登录
        response.sendRedirect("to_login");
        return false;
    }
@Controller("departmentController")
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentBiz departmentBiz;
    @RequestMapping("/list")
    public String list(Map<String ,Object> map){
            map.put("list",departmentBiz.getAll());
        return "department_list";
    }

    @RequestMapping("/to_add")
    public String toAdd(Map<String ,Object> map){
        map.put("department",new Department());
        return "department_add";
    }

    @RequestMapping("/add")
    public String add(Department department){
        departmentBiz.add(department);
        //重定向到list这个而控制器,如果直接访问department_list是没有值的
        return "redirect:list";
    }
    @RequestMapping(value = "/to_update",params = "sn")
    public String toUpdate(String sn,Map<String ,Object> map){
        map.put("department",departmentBiz.get(sn));
        return "department_update";
    }

    @RequestMapping("/update")
    public String update(Department department){
        departmentBiz.edit(department);
        //重定向到list这个而控制器,如果直接访问department_list是没有值的
        return "redirect:list";
    }

    @RequestMapping(value = "/remove",params = "sn")
    public String remove(String sn){
        departmentBiz.remove(sn);
        return "redirect:list";
    }


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
提问者 莫失聪聪3779259 2019-12-30 17:14:41

找到问题了是这里少了个/

//剩下的情况就不放行了。重定向到重新登录
        response.sendRedirect("/to_login");

但是为什么controller中的 只需要

return "redirect:list";

而不用在list前面加/ 就可以直接重定向了。而response.sendRedirect("/to_login"); 却需要加/才能重定向? 是因为一个地址是localhost:8080/department/list ,一个地址是localhost:8080/to_login 吗?老师能讲下redirect吗?(或者是response.sendRedirect("/to_login"); 和return "redirect:list";这两种用法的区别吗

  • 同学你好。这是因为SpringMVC视图解析器对redirect代表的请求,是以相对路径的方式重新拼接请求路径的。1、在执行return "redirect:list";之前,当前的请求路径是“localhost:8080/department/add”,相对路径就解析为“localhost:8080/department/add”2、而在拦截器中,response.sendRedirect是要跳转到根目录的,即localhost:8080/to_login,和当前路径没有关系。在这里“/”开头代表项目的web根目录。即localhost:8080
    2019-12-30 19:36:02
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师