在服务器上运行时,Spring MVC HTTP Status 404错误

时间:2020-04-04 20:11:28

标签: java spring tomcat model-view-controller

我一直在关注在线教程,以学习Spring MVC的基础知识,并且当我尝试在本地Tomcat服务器上运行项目时遇到一些问题。我已经在这里阅读了多次讨论,但是在当前答案中找不到解决方案,这就是我要问的原因。我遵循的教程是利用Eclipse,而我使用的是intellij,在整个项目设置中我也可能出错。

每当我尝试运行我的项目时,都会显示未找到HTTP状态-404错误。通过将其包含在xml文件中的welcome-file-list中,我能够成功加载主菜单页面,但是我希望扫描我的控制器以确定要从哪个jsp文件中提取文件。下面是我的代码:

这是我的spring-mvc-demo-servlet.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="com.luv2code.springdemo" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

这是myweb.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

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

    <absolute-ordering />

    <!-- Spring MVC Configs -->

    <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--
    <welcome-file-list>
        <welcome-file>/WEB-INF/view/main-menu.jsp</welcome-file>
    </welcome-file-list>

    -->


</web-app>

这是我的HomeController.java文件

package com.luv2code.springdemo.mvc;

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

@Controller
public class HomeController {

    @RequestMapping("/")
    public String showPage() {
        return "main-menu";
    }
}

这是我的main-menu.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1" isELIgnored="false" %>
<!DOCTYPE html>
<html>

<body>

<H2>Spring MVC Demo - Home Page</H2>

</body>

</html>

这是我的项目结构的图像:

Project Structure

这是我如何设置Tomcat服务器的图片:

Run Configuration

提前谢谢大家。我非常感谢您的帮助。我意识到这可能是一个简单的答案,但是我无法解决这个问题。

0 个答案:

没有答案
相关问题