Spring MVC ModelAndView出现404错误求助(OSX+Spring MVC4+IntelliJ环境)
Hey there, let's dig into this 404 issue you're hitting with Spring MVC 4 on macOS using IntelliJ. Since the server isn't throwing errors, it's likely a mapping or configuration mismatch—let's go through the most common culprits step by step:
1. Verify Controller Request Mapping Alignment
First, double-check your Controller's mapping annotations to make sure they match the URL you're accessing:
- Ensure your class has the
@Controllerannotation (easy to forget!) - Confirm the combination of class-level and method-level
@RequestMappingmatches/board/showArticleList(remember, the server's context path "server" is separate from the controller mappings)
Example of a correctly mapped Controller:
@Controller @RequestMapping("/board") public class BoardController { @RequestMapping("/showArticleList") public ModelAndView showArticleList() { // Make sure the view name here matches what your resolver expects ModelAndView mav = new ModelAndView("test"); // Add your model data if needed return mav; } }
Watch out for typos (e.g., showArticleLists instead of showArticleList) or restricted request methods—if you added method = RequestMethod.POST but are using a GET request, you'll get a silent 404.
2. Validate Dispatcher Servlet & Context Path Configuration
Context Path Check
In IntelliJ's Run Configuration, go to the Deployment tab and confirm the Application context is set to /server. If it's empty, your URL should be http://localhost:8080/board/showArticleList instead of including "server".
Dispatcher Servlet Mapping
Check your web.xml (or Java config) to ensure the DispatcherServlet is mapped correctly to handle all requests:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
If you mapped it to *.do or another suffix, your URL would need to include that (e.g., /server/board/showArticleList.do), which would cause a 404 if omitted.
3. Fix View Resolver Configuration in dispatcher-servlet.xml
Since you're using ModelAndView, the view resolver needs to correctly locate your test.jsp file. Verify your resolver has the right prefix and suffix pointing to your JSP's location:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- Adjust prefix to match where your test.jsp lives --> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean>
For example, if test.jsp is in WEB-INF/views/, ModelAndView("test") will resolve to /WEB-INF/views/test.jsp. If the path is wrong (e.g., prefix set to /WEB-INF/jsp/ but your JSP is in views), you'll get a 404 with no server error.
4. Resolve IntelliJ Deployment Sync Issues
Sometimes IntelliJ fails to sync updated files to your server's deployment directory. Try these fixes:
- Click the Build Project button in the top toolbar to force a rebuild
- In your Run Configuration, enable Update resources on frame deactivation under the Server tab
- Manually clean your server's work directory (e.g., Tomcat's
work/Catalina/localhost/serverfolder) and redeploy
5. Dig Deeper into Server Logs
Even if you don't see obvious errors, check your server's logs (e.g., Tomcat's localhost.log or catalina.out) for subtle warnings. You might find lines like:
Could not resolve view with name 'test' in servlet with name 'dispatcher'
This would confirm a view resolution issue rather than a mapping problem.
Start with checking the request mapping and view resolver first—those are the most common offenders here. Let me know if any of these steps fix your issue, or if you can share snippets of your code/config if you're still stuck!
内容的提问来源于stack exchange,提问作者errorMessage

