java - 我正在使用教程学习spring,但我被卡住了。我无法解决问题并在浏览器中出现 404 错误
问题描述
过去几天我一直面临这个问题,但仍然无法解决这个问题。我已经阅读了堆栈溢出中的所有先前答案。我在浏览器中找不到 404 文件,并且警告:在 DispatcherServlet 中找不到带有 URI [/chaljayrr/add] 的 HTTP 请求的映射,控制台中的名称为 'firstOne'。
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";
}
}
索引.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>
显示.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
display page
</body>
</html>
解决方案
RequestMapping 中提到的 url 是 /add..try using /add from browser / REST client。此外,在您的控制器方法中只返回“显示”。
由于您已经配置了 InternalResourceViewResolver 的 bean,它会自动找出 jsp 所在的路径 - 在您的情况下是 /WEB-INF/display.jsp。
推荐阅读
- node.js - TypeOrm 和 MongoDB:插入/保存时忽略列别名
- github - 访问 Github 工作流的工件
- html - 如何使文本与文本输入字段的左上角对齐?
- openstack - 使用 qcow2 映像时无法创建 Cinder 卷。Openstack/Packstack
- javascript - 如何在按钮内的 onClick 中使用以便在 React 中传递变量
- c++ - 使用lambda时的c ++模板成员函数实例化
- python - 每天发送和接收短信(Python)
- android - 使用线圈加载图像时显示进度条?
- sql - SQL [Tidyquery in R] - 如何添加 BETWEEN 日期以及现有的 AND 条件?
- asp.net - Application Insights 服务无法从本地服务器获取指标