Java Spring Web应用在Eclipse运行时出现404错误求助
Hey there, let's work through that 404 issue you're hitting. Since Tomcat confirms your app is deployed and there are no console errors, we can focus on a few key configuration and mapping checks:
Verify DispatcherServlet Mapping in web.xml
Double-check your<servlet-mapping>entry for the DispatcherServlet. The<url-pattern>determines which requests get routed to Spring. If your pattern is too restrictive (like*.do) but you're requesting a URL without that suffix, you'll get a 404. A common setup for catching all requests looks like this:<servlet> <servlet-name>springDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcher</servlet-name> <url-pattern>/</url-pattern> <!-- Catches all requests except static resources --> </servlet-mapping>Check Controller Request Mapping Annotations
Make sure your controller classes are annotated with@Controller(not just@Component), and that the@RequestMappingvalues on both the class and method match your request URL exactly. Spelling mistakes or mismatched paths are super common here. For example:@Controller @RequestMapping("/user") // Base path for all methods in this controller public class UserController { @RequestMapping("/profile") // Full path: /user/profile public String showProfile(Model model) { // Logic here return "profile"; // Maps to your view file (e.g., /WEB-INF/views/profile.jsp) } }If you're requesting
http://localhost:8080/your-app/user/profile, this should work—don't forget the app's context path!Validate View Resolver Configuration
Even if your request reaches the controller, a missing or misconfigured view resolver can cause a 404 when trying to render the response. For JSP views, ensure you have anInternalResourceViewResolverset up (either in XML or via Java config):
XML example:<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <!-- Path to your JSPs --> <property name="suffix" value=".jsp"/> <!-- File extension --> </bean>If your controller returns "profile", this resolver will look for
/WEB-INF/views/profile.jsp—if that file doesn't exist, you'll get a 404.Confirm Your App's Context Path
In Eclipse, when deploying to Tomcat, the app's context path isn't always the root (/). Go to your Tomcat server configuration → "Web Modules" tab, and check the "Path" value for your app. If it's/my-spring-app, your full request URL needs to include that, likehttp://localhost:8080/my-spring-app/user/profile.Check Tomcat Logs for Clues
Sometimes console logs don't show the full picture, but Tomcat's local logs (found in[Tomcat install dir]/logs/) might have more details. Look atlocalhost.logfor any view resolution errors, orlocalhost_access_log.*to confirm exactly which URL your browser is requesting—this can help spot typos you might have missed.Static Resource Handling (If Applicable)
If you're trying to access CSS, JS, or images, make sure Spring isn't blocking those requests. For XML config, add:<mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>For Java config, implement
WebMvcConfigurerand overrideaddResourceHandlers:@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/static/"); }This tells Spring to route static resource requests to the correct folder instead of trying to map them to a controller.
内容的提问来源于stack exchange,提问作者sagar

