英文:
Spring 5 application not showing static html pages
问题
这是 Spring 5 Web 应用的配置和问题描述。已经设置了 RESTful API,但静态内容无法访问,返回 404 Not Found 错误。
Spring 5 配置开始:
@Configuration
@ComponentScan(basePackages = "com.tomholmes.app")
@EnableWebMvc
public class MyAppConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public ServletContainerInitializer initializer() {
return new ServletContainerInitializer() {
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
ctx.setInitParameter("isLog4jAutoInitializationDisabled", "true");
ctx.setInitParameter("log4jConfiguration", "classpath:log4j2.xml");
ctx.setInitParameter("log4jContextName", "My App");
}
};
}
}
Web 配置,用于处理静态资源:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
}
项目结构:
/src/main/java/com/tomholmes/app
/src/main/resources
/src/main/resources/static
/src/main/resources/static/index.html
/src/main/resources/static/css/mine.css
/src/main/resources/static/js/myjavascript.js
问题描述了无法访问静态内容,应用部署在 Tomcat 9 上。已经检查了应用文件的部署情况,但访问 http://localhost:8080/myapp/ 或 http://localhost:8080/myapp/index.html 时返回 404 错误。希望能够尽快解决问题。
英文:
I am creating a small Spring 5 Web Application, NOT a Spring Boot application. For this small work project, this is both the front-end (UI) and the back-end with RESTful API (Controllers).
I have done this before, so I thought I wouldn't have any problem making a web-app that had RESTful endpoints, but also able to server out some static content. The problem is that my RESTful API's work, but the static content is not coming up and is a 404 Not Found issue.
I've gone out to the internet where I see a lot of older messages and articles. Even here on StackOverflow, a lot of the messages are older. Even though this is not Spring Boot, I am using annotations in the code, and I am trying to use the proper Java Configuration classes and not an applicationContext.html file. So many of the old messages and articles refer to classes that are long since deprecated and gone. I just hope I am not in the same position again.
Here is the Spring 5 Configuration start:
@Configuration
@ComponentScan(basePackages = "com.tomholmes.app")
@EnableWebMvc
public class MyAppConfig. {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public ServletContainerInitializer initializer() {
return new ServletContainerInitializer()
{
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
ctx.setInitParameter("isLog4jAutoInitializationDisabled", "true");
ctx.setInitParameter("log4jConfiguration", "classpath:log4j2.xml");
ctx.setInitParameter("log4jContextName", "My App");
}
};
}
}
Here is the Web Configuration, this should be pointing to the right place:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
}
And here is my structure as it is exploded from the WAR file:
/src/main/java/com/tomholmes/app
/src/main/resources
/src/main/resources/static
/src/main/resources/static/index.html
/src/main/resources/static/css/mine.css
/src/main/resources/static/js/myjavascript.js
I am using Gradle, not maven, to build this app at work, and it builds successfully.
I am deploying the WAR file to the Tomcat 9 webapps directory, and I can start Tomcat up.
I can see Tomcat running, the app is exploded/deployed and I can see the exploded files.
/webapps/myapp/WEB-INF
/webapps/myapp/WEB-INF/classes
/webapps/myapp/WEB-INF/classes/com
/webapps/myapp/WEB-INF/classes/log4j2.xml
/webapps/myapp/WEB-INF/classes/static
/webapps/myapp/WEB-INF/classes/static/css
/webapps/myapp/WEB-INF/classes/static/index.html
/webapps/myapp/WEB-INF/classes/static/js
/webapps/myapp/WEB-INF/lib
I can see from the Tomcat Manager that the app is running, I can go to that, and there is not page that comes up from:
http://localhost:8080/myapp/ and there is no page.
So, when I try to call:
http://localhost:8080/myapp/index.html
I was expecting something to be there, and again, there is a 404 Error - Not Found.
If there is any more information I can provide, please let me know. I really hope that this is something simple I can fix quickly. I can't imagine that this would be a hard fix.
Thanks!
专注分享java语言的经验与见解,让所有开发者获益!
评论