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

Tomcat 10部署Spring MVC WAR包遇HTTP 404错误求助

Tomcat 10部署Spring MVC WAR包出现404错误

在Tomcat 10上部署通过Maven构建的Spring MVC WAR包时,访问首页及所有页面均返回「HTTP Status 404 – Not Found」错误,已反复检查配置未发现异常。项目为标准Maven Web结构,源码位于src/main/java,视图文件存放于WEB-INF/views目录下,关键配置与代码如下:

关键配置文件与代码

web.xml

<display-name>spring-mvc-example</display-name>

<!-- 配置Spring MVC DispatcherServlet作为前端控制器 -->
<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>\WEB-INF\spring-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern> 
</servlet-mapping>
  
</web-app>

spring-servlet.xml

<!-- DispatcherServlet上下文:定义请求处理基础设施 -->

<!-- 启用Spring MVC @Controller编程模型 -->
<annotation-driven />
<context:component-scan base-package="com.example.springmvcexample.mvcexample" />

<!-- 将@Controller选择的视图解析为/WEB-INF/views目录下的.jsp资源 -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="WEB-INF\views" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

</beans:beans>

HomeController.java

package com.example.springmvcexample.mvcexample;

import java.text.DateFormat;
import java.util.Locale;
import java.util.Date;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.springmvcexample.model.User;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

@Controller
public class HomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        System.out.println("Home Page Requested, locale = " + locale);
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate);

        return "home";
    }

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public String user(@Validated User user, Model model) {
        System.out.println("User Page Requested");
        model.addAttribute("userName", user.getUserName());
        return "user";
    }
}

操作步骤

使用Maven命令.\mvnw tomcat7:deploy将包含多个.jsp文件的WAR包部署至Tomcat服务器,访问地址http://localhost:8080/myfirstapp/时仍出现404错误。

内容的提问来源于stack exchange,提问作者Vineet Jain

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 05:37:03