【问题标题】:Spring-MVC: HTTP Status 404 – Not FoundSpring-MVC:HTTP 状态 404 – 未找到
【发布时间】:2020-07-06 00:34:24
【问题描述】:

我使用 Tomcat v9.0 服务器创建了一个简单的 spring-mvc 项目,以检查一切是否正常。当我点击提交按钮时,从主页上,预计会在控制台中看到一条消息,但我收到以下消息:

HTTP 状态 404 - 未找到

描述:源服务器没有找到目标资源的当前表示或不愿意透露存在。

代码如下: web.xml

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>Spring-MVC</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Spring-MVC</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.mvc</groupId>
  <artifactId>SpringMVC</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVC 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>4.1.8.RELEASE</version>
    </dependency>

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

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

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

  </dependencies>
  <build>
    <finalName>SpringMVC</finalName>
  </build>
</project>

index.jsp:

<html>
<body>

<form action="add">
    <input type="text" name="t1"/><br>
    <input type="text" name="t2"/><br>
    <input type="submit"/>
</form>

</body>
</html>

控制器:

@Controller
public class AddController {

    @RequestMapping("/add")
    public void add() {
        System.out.println("Working!!");
    }

Spring-MVC-servlet.xml:

<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.spring"></ctx:component-scan>
</beans>

【问题讨论】:

    标签: spring-mvc servlets tomcat9


    【解决方案1】:

    您在此处定义表单操作的方式 -

    <form action="add">
        <input type="text" name="t1"/><br>
        <input type="text" name="t2"/><br>
        <input type="submit"/>
    </form>
    

    错了。

    表单操作的值应该是一个 URL。根据协议、主机和端口号,此值将不同于add

    如果您在 localhost 上使用 http 端口 8080 运行,那么这应该是 - http://localhost:8080/add。 它会根据您运行应用程序的端口而有所不同。如果您不知道,可能是 8080,因为这是 tomcat 的默认端口。

    【讨论】:

    • 是否需要写完整的URL,因为我在其他项目中没有做过,但我得到了所需的输出。
    • 是的,AFAIK 有必要编写完整的 URL,你在 github 上还有这些其他项目吗,因为我真的很想看看没有完整 URL 的表单如何工作。我的解决方案有效吗?
    • 对不起,我还没有维护 github 存储库。问题是关于 Tomcat v9,我降级到 v8。一旦我开始维护 github repo,我会 ping 你。感谢您的考虑。
    • 我已经把项目上传到了github。这是链接github.com/SatyamP07/Wallet-Web-App
    猜你喜欢
    • 2014-08-25
    • 1970-01-01
    • 2022-06-12
    • 2021-10-13
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多