过去几天我一直在面对这个问题,但仍然无法解决这个问题。我已经阅读了堆栈溢出中的所有先前答案。我在浏览器中找不到404文件,并且警告:在控制台中名称为“ firstOne”的DispatcherServlet中,使用URI [/ chaljayrr / add]的HTTP请求找不到映射。
Web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>firstOne</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstOne</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
firstOne-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.chalja"></ctx:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:annotation-driven/>
</beans>
FirstController.java
package com.chalja;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FirstController {
@RequestMapping("/add")
public String add(){
return "display.jsp";
}
}
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>
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.chalja</groupId>
<artifactId>chaljayrr</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>chaljayrr 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-webmvc</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<finalName>chaljayrr</finalName>
</build>
</project>
disply.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
display page
</body>
</html>
答案 0 :(得分:0)
RequestMapping中提到的url是/add..try,使用浏览器/ REST客户端中的/ add。另外,在您的控制器方法中,只需返回“ display”。
由于已经配置了InternalResourceViewResolver的bean,它将自动找出jsp所在的路径-在您的情况下为/WEB-INF/display.jsp。