使用Springboot整合MVC进行web开发,实现简单的页面跳转功能
@Configuration
public class MyMVCConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/toLiginPage").setViewName("login");
registry.addViewController("/login.html").setViewName("login");
}
}
Springboot注册自定义拦截器Interceptor:
@Component
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
String uri = request.getRequestURI();
Object loginUser = request.getSession().getAttribute("loginUser");
if(uri.startsWith("/admin")&&null==loginUser){
response.sendRedirect("/toLoginPage");
return false;
}
return true;
}
}
|