Spring-Boot 添加视图控制器转发到登录页面Css文件无法引入的解决方法

问题:直接访问 localhost:8080/ 跳到系统登录页面,发现样式没有引入,经排查,样式的路径是正确的,当我点击登录,通过Controller转发,发现样式是正常的,那为什么直接访问不走Controller样式就不正常了呢?

细节1:直接访问 localhost:8080/ 跳到系统登录页面在spring-boot中的配置类如下

package com.mengxuegu.springbootweb.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.mengxuegu.springbootweb.component.MyLocaleResolve;
import com.mengxuegu.springbootweb.interceptor.LoginHandlerInterceptor;

/**
 * 
 * 配置spring mvc
 * 
 * */
@Configuration
public class MySpringMvcConfiguration {
	
	@Bean
	public WebMvcConfigurer webMvcConfigure() {
		return new WebMvcConfigurer() {								
			//添加视图控制	
			@Override
			public  void addViewControllers(ViewControllerRegistry registry) {
				//发送/index.html请求,转发视图到登录页面
				registry.addViewController("/index.html").setViewName("/main/login");
				registry.addViewController("/").setViewName("/main/login");
				registry.addViewController("/main.html").setViewName("/main/index");
	
			}	
			
			//添加拦截器
			public void addInterceptors(InterceptorRegistry registry) {
				registry.addInterceptor(new LoginHandlerInterceptor())
				// 拦截所有请求
				.addPathPatterns("/**")
				//排除不需要拦截的请求
				.excludePathPatterns("/", "/index.html", "/login");
			}			
		};
	}
	
	//国际化信息解析器
	@Bean
	public LocaleResolver localeResolver() {
		return new MyLocaleResolve();
	}
	
}

当我直接访问 localhost:8080/视图控制器就为我转发到了  main 文件夹下的 login.html页面

拦截器中也排除了 / 路径,但访问 localhost:8080/ 即 发送 /请求 跳转到 login.html页面就是没有样式,但通过Controller转发的却能正常显示,于是我就这样自己写了一个Controller,如下

/**
 * 主页面Controller
 * 
 * */

@Controller
public class MainController {
	
	/**
	 * localhost:port/ 请求  转发到登录页面
	 * */
	@GetMapping(value="")
	public String index() {
		return "main/login";
	}
		

}

这样写了以后,样式依然没有引入,此时我想到了是不是拦截器 设置的问题,于是我把拦截器排除了对静态资源访问路径的拦截,如下

package com.mengxuegu.springbootweb.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.mengxuegu.springbootweb.component.MyLocaleResolve;
import com.mengxuegu.springbootweb.interceptor.LoginHandlerInterceptor;

/**
 * 
 * 配置spring mvc
 * 
 * */
@Configuration
public class MySpringMvcConfiguration {
	
	@Bean
	public WebMvcConfigurer webMvcConfigure() {
		return new WebMvcConfigurer() {								
			//添加视图控制	
			@Override
			public  void addViewControllers(ViewControllerRegistry registry) {
				//发送/index.html请求,转发视图到登录页面
				registry.addViewController("/index.html").setViewName("/main/login");
				registry.addViewController("/").setViewName("/main/login");
				registry.addViewController("/main.html").setViewName("/main/index");
	
			}	
			
			//添加拦截器
			public void addInterceptors(InterceptorRegistry registry) {
				registry.addInterceptor(new LoginHandlerInterceptor())
				// 拦截所有请求
				.addPathPatterns("/**")
				//排除不需要拦截的请求
				.excludePathPatterns("/", "/index.html", "/login","/css/**", "/img/**", "/js/**");
			}			
		};
	}
	
	//国际化信息解析器
	@Bean
	public LocaleResolver localeResolver() {
		return new MyLocaleResolve();
	}
	
}

果不其然,样式成功引入,直接 localhost:8080访问登录页面,页面样式显示正常。


 


解决方法总结:

步骤1:自己写了一个Controller转发到请求页面

步骤2:拦截器排除了对静态资源访问路径的拦截

温馨提示:你的页面访问路径要写对,排除的拦截静态资源路径要写对。


 

我是通过这种方式解决的,不知道大家还有什么更好的方式?欢迎大家补充、批评、指正。 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值