首页 > 解决方案 > 运行 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>  

标签: javaspringservlets

解决方案


在您的 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.jspand放进errorpage.jsp/WEB-INF/jsp。我不知道您是否在 WEB-INF 中创建 jsp 目录。

有时控制器找不到您的 jsp 导致 404。

我使用您的代码创建了一个项目,我工作得很好。


推荐阅读