代码之家  ›  专栏  ›  技术社区  ›  Emin Çiftçi

未找到Tomcat 404 Http

  •  0
  • Emin Çiftçi  · 技术社区  · 7 年前

    我正在学习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";
        }
    }
    

    <%@ 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>
    

    Project looks like this

    我知道网站上有一个重复的主题。我试过了也没有解决。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Tibin    7 年前

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

        2
  •  0
  •   ForeverZer0    6 年前

    找不到您从浏览器请求的页面,因此您需要在以下模式中添加URL:

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