简体   繁体   English

春季-无法加载资源(CSS)

[英]Spring - Can't load resources (CSS)

Well I have been looking for a solution for 2 days all over the net and no matter what I tried nothing worked, than I gonna hope someone here could fix my problem: 好吧,我一直在网上寻找解决方案两天了,不管我什么都没尝试,我希望这里有人可以解决我的问题:

I have Spring project which when I load with server and open it up I can't load css file into index.html (yes I don't use jsp): 我有一个Spring项目,当我加载服务器并打开它时,我无法将CSS文件加载到index.html中(是的,我不使用jsp):

I will post all my relevant code to be clear: 我将发布所有相关代码以使其清晰:

index.html (you can see I tried many paths _ ) index.html (您可以看到我尝试了许多路径_

<html>
<head>
    <title>CouponSystem Home Page</title>

    <link rel="stylesheet" href="couponsystem/resources/css/style.css" type="text/css" />
    <link rel="stylesheet" href="/couponsystem/resources/style.css" type="text/css" />
    <link rel="stylesheet" href="couponsystem/resources/style.css" type="text/css" />
    <link rel="stylesheet" href="resources/css/style.css" type="text/css" />
    <link rel="stylesheet" href="/resources/css/style.css" type="text/css" />
    <link rel="stylesheet" href="resources/style.css" type="text/css" />
    <link rel="stylesheet" href="/resources/style.css" type="text/css" />
</head>
<body>
<h2>CouponSystem- Home Page</h2>
<hr>

<div class="red-text">Red text</div>
    <br>
    <div class="green-text">Green text</div>
<hr>
</body>
</html>

AppConfig: AppConfig的:

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.orel.couponsystem")
@PropertySource({ "classpath:persistence-mysql.properties" })
public class AppConfig extends WebMvcConfigurationSupport implements 
ApplicationContextAware {

@Autowired
private Environment env;

private ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
}

@Bean
public ViewResolver viewResolver() {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine((ISpringTemplateEngine) templateEngine());
    resolver.setCharacterEncoding("UTF-8");
    return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setEnableSpringELCompiler(true);
    engine.setTemplateResolver(templateResolver());
    return engine;
}

private ITemplateResolver templateResolver() {
    SpringResourceTemplateResolver resolver = new 
  SpringResourceTemplateResolver();
    resolver.setApplicationContext(applicationContext);
    resolver.setPrefix("/WEB-INF/view/");
    resolver.setTemplateMode(TemplateMode.HTML);
    return resolver;
    }

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

my project structure: my project structure: 我的项目结构: 我的项目结构:

在此处输入图片说明

style.css style.css文件

body{
    background-color:red;
}
.button {
  font-size: 20px;
  background: #ccc;
}

.red-text {
  color: red;
  font-size: 30px;
}

.green-text {
  color: green;
  font-size: 20px;
}

Thanks a lot!!! 非常感谢!!!

UPDATE: **ALSO UPDATED AppConfig my structure: structure with index file 更新: **还更新了AppConfig我的结构: 具有索引文件的结构

如果使用spring,建议不要使用'src/main/resource' ,所以请使用工件sprint-sample-mvc进行更正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM