springboot中如何设置默认网页
这篇文章主要介绍spring boot中如何设置默认网页,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的忻府网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
一共两布,第一步,创建Interceptor拦截
package com.cy.example.config; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class LoginInterceptor implements HandlerInterceptor { private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class); public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub //获取session HttpSession session = request.getSession(true); logger.info("----进入登录拦截器--url:"+request.getServletPath()+"-----"); if(session.getAttribute(WebConfig.LOGIN_USER) == null){ logger.info("------跳转到login页面-----"); response.sendRedirect(request.getContextPath()+"/index"); return false; }else{ session.setAttribute(WebConfig.LOGIN_USER, session.getAttribute(WebConfig.LOGIN_USER)); return true; } } }
第二步,注册创建的拦截器
package com.cy.example.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebConfig extends WebMvcConfigurerAdapter { public static String LOGIN_USER = "loginUser"; public WebConfig() { super(); } //因为新加了拦截器,这里需要重新设置资源地址 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations( "classpath:/static/"); registry.addResourceHandler("/templates/**").addResourceLocations( "classpath:/templates/"); super.addResourceHandlers(registry); } @Override public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void addInterceptors(InterceptorRegistry registry) { // 拦截规则:除了login,其他都拦截判断,excludePathPatterns是排除拦截的路径,一个是登录验证地址,一个是登录页 registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/index","/system/user/validate"); super.addInterceptors(registry); } }
以上是“spring boot中如何设置默认网页”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
网页标题:springboot中如何设置默认网页
文章地址:http://abwzjs.com/article/pihops.html