【问题标题】:Tomcat 404 Http Not FoundTomcat 404 未找到 Http
【发布时间】:2018-01-10 02:59:31
【问题描述】:

我正在学习 Spring MVC,并构建了第一个“hello world”示例。但我无法访问该页面。它说“源服务器没有找到目标资源的当前表示,或者不愿意透露存在的表示。”我正在使用 Tocat 9.0(我也尝试过 Tomcat 8 仍然无法正常工作)。所以下面是我的代码

控制器:

package com.emin.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";
    }
}

jsp文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h2>Spring MVC Demo</h2>
</body>
</html>

servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.emin.springdemo.mvc" />

    <!-- 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>

web.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>

    <!-- 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>

</web-app>

我知道网站中有重复的主题。我试过他们也没有达到解决方案。

【问题讨论】:

    标签: spring-mvc tomcat http-status-code-404


    【解决方案1】:

    未找到您从浏览器请求的页面,因此您需要按以下格式添加 URL:

    localhost:(Port number) /project name/(request mapping at controller) /(request mapping at method)
    

    尝试映射 URL 并重新启动服务器。

    【讨论】:

      【解决方案2】:

      问题可能是破折号,因为它不是有效的 Java 标识符尝试重命名 jsp 并返回文件名。

      【讨论】:

      • 是的,它有效,但为什么破折号无效。我用破折号命名了很多。
      猜你喜欢
      • 2018-05-06
      • 2020-07-31
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2021-09-12
      • 1970-01-01
      相关资源
      最近更新 更多