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):
I will post all my relevant code to be clear:
index.html (you can see I tried many paths _ )
<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: 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;
}
Thanks a lot!!!
UPDATE: **ALSO UPDATED AppConfig my structure: structure with index file
如果使用spring,建议不要使用'src/main/resource'
,所以请使用工件sprint-sample-mvc
进行更正。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.