【问题标题】:SpringMVC / HTTP Status 404 – Not Found / The origin server did not find a current representation for the target resourceSpringMVC / HTTP 状态 404 – 未找到 / 源服务器未找到目标资源的当前表示
【发布时间】:2021-10-02 09:28:48
【问题描述】:

我想创建一个 Spring MVC 项目。当我使用运行配置( http://localhost:8080/springmvc/hellohttp://localhost:8080/springmvc 在 tomcat localhost 运行项目时>http://localhost:8080/hello) 我收到了这个错误:

HTTP 状态 404 – 未找到
类型状态报告
消息请求的资源 [/springmvc] 不可用
说明 源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。
Apache Tomcat/9.0.50

这可能是什么原因? 到目前为止所做的解释如下:

我清理项目,删除服务器配置并创建新的,尝试使用 tomcat 版本但不起作用。问题如何解决?

>java -version

java 版本“11.0.12”2021-07-20 LTS
Java(TM) SE 运行时环境
18.9(内部版本 11.0.12+8-LTS-237)Java HotSpot(TM) 64 位服务器 VM 18.9(内部版本 11.0.12+8-LTS-237,混合模式)

  • 项目中JRE设置是这样的:

项目 -> 属性 -> Java 构建路径 -> 库 -> JRE 系统库 -> JavaSE-1.8

  • Eclipse 版本是:

面向 Java 开发人员的 Eclipse IDE(包括孵化组件)

版本:2020-12 (4.18.0)

HelloController.java

package com.me.spring.springmvc.controller;

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

@Controller
public class HelloController {  
    @RequestMapping("/hello")
    public ModelAndView hello() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        return modelAndView;
    }   
}

dispatcher-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:p="http://www.springframework.org/schema/p"
    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">


<context:component-scan base-package="com.me.spring.springmvc.controller" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        name="viewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Hello Spring MVC</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
    </servlet>  
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<h1>Hello from Spring MVC</h1>
</body>
</html>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.me.spring</groupId>
    <artifactId>springmvc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>springmvc Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <springframework.version>4.3.6.RELEASE</springframework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
  • 当我点击Servers / Apache Tomcat服务器中的开始按钮并尝试打开http://localhost:8080/来检查tomcat安装是否正确时;

在内部 Web 浏览器 (Eclipse) 中

  • Maven -> 更新项目已应用,但现在可以使用。

编辑:

当我右键单击项目时,“在服务器上运行”不存在;但从那里

https://www.eclipse.org/m2e-wtp/download/

通过这个链接

download m2e wtp

在 Eclipse 中添加 -> 安装新软件并下载 m2e wtp,然后出现 Run on Server。

当我在服务器上运行项目时,

http://localhost:8080/springmvc/ 按预期工作,但 http://localhost:8080/springmvc/hello 不起作用。发送此 url 请求时,

...
org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Mon Jul 26 17:14:53 EET 2021]; root of context hierarchy
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 881 ms
org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/springmvc/hello] in DispatcherServlet with name 'dispatcher'

【问题讨论】:

    标签: java spring eclipse spring-mvc tomcat


    【解决方案1】:

    这个问题终于解决了!!当我删除 Eclipse 缓存并更改 eclipse-workspace ,更新服务器配置,更新 maven 依赖项时,问题就解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-31
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      • 2019-09-29
      • 2019-10-26
      • 1970-01-01
      相关资源
      最近更新 更多