You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC 3.0配置异常求助:手动输入URL才可运行,点击返回报404错误

Fixing the 404 Error for /hello.do in Spring MVC 3.0

Hey there! As someone who’s stumbled through Spring MVC’s early setup hurdles too, let’s break down why you’re hitting that 404 when trying to use /hello.do instead of directly accessing hello.jsp. Here’s what’s likely going wrong and how to fix it:

Common Issues & Solutions

1. Your DispatcherServlet Isn’t Mapping to .do Requests

The DispatcherServlet is Spring MVC’s "front controller"—it needs explicit instructions to handle requests ending with .do. Check your web.xml file to confirm the mapping is set correctly:

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!-- Tells the servlet to handle all .do requests -->
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

2. Your Controller Isn’t Properly Mapped to /hello.do

Make sure your controller class is annotated with @Controller and has a @RequestMapping targeting /hello.do. For Spring 3.0’s annotation-based approach, your controller should look like this:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping("/hello.do")
    public String showHelloPage(Model model) {
        // Pass data to your view
        model.addAttribute("greeting", "Hello from Spring MVC!");
        // Return the view name (your resolver will handle the file path)
        return "hello";
    }
}

3. Missing or Misconfigured View Resolver

Spring needs a view resolver to locate your JSP files when the controller returns a view name. Add this to your springmvc-servlet.xml (the file named after your DispatcherServlet in web.xml):

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- Path to your JSP directory (adjust to match your project structure) -->
    <property name="prefix" value="/WEB-INF/views/"/>
    <!-- File extension for your view files -->
    <property name="suffix" value=".jsp"/>
</bean>

With this setup, returning "hello" from your controller will tell Spring to look for /WEB-INF/views/hello.jsp—move your JSP here (instead of the root) to enforce access through the controller.

4. Controller Classes Aren’t Being Scanned

If you’re using annotations like @Controller, you need to tell Spring to scan your package for these classes. Add this to your springmvc-servlet.xml:

<!-- Replace with your actual controller package path -->
<context:component-scan base-package="com.yourproject.controller"/>

Quick Verification Checklist

  • Confirm hello.jsp lives at /WEB-INF/views/hello.jsp (matches your view resolver’s prefix/suffix)
  • Check that your project’s deployment structure includes the WEB-INF folder correctly
  • Test accessing http://localhost:8080/SpringMVC/hello.do directly in your browser—if it works, the "back" link issue was likely due to an incorrect URL in your JSP

内容的提问来源于stack exchange,提问作者ed u

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:26:52