好吧,我一直在网上寻找解决方案2天,不管我尝试了什么都没用,我希望这里有人可以解决我的问题:
我有一个Spring项目,当我用服务器加载并打开它时,我无法将CSS文件加载到index.html中(是的,我不使用jsp):
我将发布所有相关代码以使其清晰明了:
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:
@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:
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;
}
非常感谢!
更新:**还更新了AppConfig 我的结构: structure with index file
答案 0 :(得分:0)
如果您使用的是Spring,建议不要使用'src/main/resource'
,那么请使用工件sprint-sample-mvc
进行更正。