Spring MVC与Angular2部署Tomcat:Angular index.html作欢迎页遇404求助
Hey there, let's work through why you're hitting that 404 error when trying to set your Angular 2 index.html as the Spring MVC welcome page. I've dealt with this exact scenario before, so here are the most common fixes to get it working:
1. Double-Check Your index.html File Location
First, confirm where your index.html lives relative to your Spring MVC project's web root:
- For traditional project structures, the web root is usually
WebContent(Eclipse) orsrc/main/webapp(IntelliJ). - If you copied the entire
distfolder into the web root, your welcome file path needs to reflect that. Update yourweb.xmllike this:<welcome-file-list> <welcome-file>dist/index.html</welcome-file> </welcome-file-list> - Alternatively, copy all contents of the
distfolder directly into the web root, soindex.htmlsits at the top level—then your originalwelcome-fileconfig will work.
2. Fix DispatcherServlet Interception of Static Resources
The #1 culprit here is Spring's DispatcherServlet mapping. If it's set to /*, it intercepts every request—including calls to static files like index.html, CSS, and JS. Spring will try to find a Controller mapping for /index.html instead of serving the actual file.
Add static resource handling to your Spring config to fix this:
XML Configuration (spring-servlet.xml)
<mvc:resources mapping="/**" location="/"/> <mvc:default-servlet-handler/>
<mvc:resources>tells Spring to serve any request matching/**directly from your web root.<mvc:default-servlet-handler>falls back to your server's default servlet (like Tomcat'sDefaultServlet) for edge cases with static files.
Java Annotation-Based Configuration
Add this method to your @Configuration class:
@Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("/"); } }
3. Check for Conflicting View Resolvers
If you have an InternalResourceViewResolver set up, it might be overriding your welcome page path. For example, if the resolver has a prefix like /WEB-INF/views/, Spring will look for index.html there instead of your web root.
You can either:
- Adjust the view resolver to exclude static paths, or
- Remove the resolver entirely if you don't need server-side views (since you're using Angular for client-side routing).
4. Confirm Server Deployment
Sometimes the issue is just that your updated files aren't deployed to the server:
- For Tomcat, check the
webapps/[your-project-name]directory to make sureindex.html(and all Angular assets) are present. - Refresh or redeploy the project in your IDE to ensure the latest files are copied over.
5. Test Direct Access
Try accessing http://localhost:[port]/[your-project]/index.html (or http://localhost:[port]/[your-project]/dist/index.html if it's in the dist folder) directly. If this works but the root path (/) doesn't, double-check your welcome-file-list configuration to make sure the server recognizes the welcome file.
内容的提问来源于stack exchange,提问作者Robert

