直接访问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";
}20
收起
正在回答 回答被采纳积分+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";这两种用法的区别吗
)
4. SSM到Spring Boot入门与综合实战
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星