【问题标题】:getting error 404 The origin server did not find a current representation for the target resource or is not willing to disclose that one exists得到错误 404 源服务器没有找到目标资源的当前表示或不愿意透露存在的表示
【发布时间】:2020-06-11 12:39:23
【问题描述】:

我只是在处理无法正常工作的数据流。 代码如下:

index.jsp

<html>
  <body>
    <h2>Hello World!</h2>    
    <form action="add">
      <input type = "text" name  = "t1"><br>
      <input type = "text" name  = "t2"><br>
      <input type = "submit">    
    </form>
  </body>
</html>

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>Archetype Created Web Application</display-name>

  <servlet>
  <servlet-name>shivam</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  </servlet>

  <servlet-mapping>
  <servlet-name>shivam</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.springmvc</groupId>
  <artifactId>First_Spring_MVC_project</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>First_Spring_MVC_project Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

  </dependencies>
  <build>
    <finalName>First_Spring_MVC_project</finalName>
  </build>
</project>

虽然我创建了一个编写代码的 java 类。 AddController.java

package com.springmvc;

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

@Controller
public class AddController
{
    @RequestMapping("add")
    public void add()
    {
        System.out.println("i am here");
    }

}

和 shivam-servlet.xml 文件

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

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


    <ctx:annotation-config></ctx:annotation-config>
    <ctx:component-scan base-package="com.springmvc"></ctx:component-scan>
</beans>

错误:

HTTP 状态 404 – 未找到 类型 状态 报告 描述 来源 服务器没有找到目标资源的当前表示 或者不愿意透露它的存在。

Apache Tomcat/9.0.34

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    删除你的

    <ctx:annotation-config></ctx:annotation-config>
        <ctx:component-scan base-package="com.springmvc"></ctx:component-scan>
    

    添加

    <!-- Add support for component scanning -->
        <context:component-scan base-package="com.springmvc" />
    
        <!-- Add support for conversion, formatting and validation support -->
        <mvc:annotation-driven/>
    
        <!-- 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>
    

    .. 之后确保在您的 WEB-INF 文件夹中,创建视图文件夹,并在该文件夹中放置您的 jsp 页面( index.jsp )

    之后 从

    更改您的控制器
    @Controller
    public class AddController
    {
        @RequestMapping("add")
        public void add()
        {
            System.out.println("i am here");
        }
    
    }
    

     @Controller
    @RequestMapping("/")
        public class AddController
        {
            @RequestMapping("/")
            public String add()
            {
                System.out.println("i am here");
        return "index";
            }
    
        }
    

    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-crud-demo</display-name>
    
      <absolute-ordering />
    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    
      <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/shivam-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>
    

    你的 shivam-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:tx="http://www.springframework.org/schema/tx"
        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
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!-- Add support for component scanning -->
        <context:component-scan base-package="com.springmvc" />
    
        <!-- Add support for conversion, formatting and validation support -->
        <mvc:annotation-driven/>
    
        <!-- 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、shivam-servlet.xml 和视图文件夹位于 WEB-INF 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-27
      • 2023-03-26
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 2019-10-14
      • 1970-01-01
      相关资源
      最近更新 更多