java - 运行 Spring MVC 基本 Web 应用程序时显示 404 错误
问题描述
我正在学习 Spring MVC。这是我在网上找到并尝试在本地运行的基本代码。我已经添加了所有依赖项,但我仍然收到此错误:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
这是代码:
这是包含表单的登录页面。在提交时,控制器将被调用。
索引.jsp
<html>
<body>
<form action="hello">
UserName : <input type="text" name="name"/> <br><br>
Password : <input type="text" name="pass"/> <br><br>
<input type="submit" name="submit">
</form>
</body>
</html>
这是具有 spring 依赖项的 maven xml。
pom.xml
添加了以下两个依赖项:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
这是控制器。密码为“admin”时登录成功。
HelloController.java
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String display(HttpServletRequest req,Model m)
{
//read the provided form data
String name=req.getParameter("name");
String pass=req.getParameter("pass");
if(pass.equals("admin"))
{
String msg="Hello "+ name;
//add a message to the model
m.addAttribute("message", msg);
return "viewpage";
}
else
{
String msg="Sorry "+ name+". You entered an incorrect password";
m.addAttribute("message", msg);
return "errorpage";
}
}
}
web.xml
<web-app>
<display-name>SpringMVC</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml
<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: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">
<!-- Provide support for component scanning -->
<context:component-scan base-package="com.javatpoint" />
<!--Provide support for conversion, formatting and validation -->
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
视图页面.jsp
<html>
<body>
${message}
</body>
</html>
错误页面.jsp
<html>
<body>
${message}
<br><br>
<jsp:include page="/index.jsp"></jsp:include>
</body>
</html>
解决方案
在您的 web.xml<context:component-scan base-package="com.javatpoint" />
中,但我package com.javatpoint;
在您的 .xml 中看不到。您HelloController
需要创建一个包并将您的控制器放入其中并在 web.xml 中进行设置。
尝试创建com.javatpoint
包并将控制器放入其中。
而且你还设置<property name="prefix" value="/WEB-INF/jsp/"></property>
了你需要把你的viewpage.jsp
and放进errorpage.jsp
去/WEB-INF/jsp
。我不知道您是否在 WEB-INF 中创建 jsp 目录。
有时控制器找不到您的 jsp 导致 404。
我使用您的代码创建了一个项目,我工作得很好。
推荐阅读
- python - 如何永远保存字典的键?
- dynamics-crm - Dynamics 365 WebAPI - 如何让用户访问实体?
- c++ - 遍历链表时无限循环
- reactjs - 如何从图表中显示当前值?
- laravel - 从存储文件夹返回图像
- django - Django:在登录模板上显示不同的标题
- python - read_csv:我的所有数据都被读取为对象/字符串
- css - 将鼠标悬停在父元素上时如何更改首字母的 CSS 样式?
- awk - 根据第一列内容分隔字段,在第二列中匹配并在 awk 中减去第四列值
- python - 将列表中的 1 个数组分配给 var,并将列表中的其余数组分配给另一个 var