我使用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>
答案 0 :(得分:0)
您在此处定义表单操作的方式-
<form action="add">
<input type="text" name="t1"/><br>
<input type="text" name="t2"/><br>
<input type="submit"/>
</form>
错了。
表单操作的值应为URL。根据协议,主机和端口号的不同,此值将不同于add
。
如果您使用8080端口在localhost上运行,则应为-http://localhost:8080/add
。
这取决于您运行应用程序的端口。如果您不知道,可能是8080,因为那是tomcat的默认端口。