首页 > 解决方案 > Eclipse IDE - Spring MVC - 状态 404 - 请求的资源不可用 - RequestMapping 问题(不是 Maven)

问题描述

我正在学习 Spring MVC Web 应用程序开发,并尝试使用 Controller 和 RequestMapping 从一个 jsp 文件导航到另一个。第一个页面“index.jsp”显示在 http://localhost:8080/ 没有任何问题。

但是,Controller 和 RequestMapping 方法不起作用,这很明显,因为 System.out.Println() 不会在控制台中打印任何内容。因此,在导航到下一个 jsp 页面时,错误被抛出为“状态 404 - 请求的资源不可用”。

以下是项目中的文件。我正在使用 spring 3.0.1、Eclipse IDE 4.7.1a 和 Tomcat 8.0 服务器。

项目结构

--springMvcWeb
    --Java Resources
        --src
            --springMvcWeb 
                --HelloController.java
    --WebContent
        --META-INF
        --WEB-INF
            --jsp
                --final.jsp
                --viewPage.jsp
            --lib
                --dispatcher-servlet.xml
                --web.xml
            --index.jsp   

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>SpringMVC</display-name>
    
    <!--  The below servlet should match the dispatcher-servlet.xml file -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="springMvcWeb" />

    <!--Provide support for conversion, formatting and validation-->
    <mvc:annotation-driven />
    
    <!-- Define Spring MVC view resolver -->
    <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>  

HelloController.java

package springMvcWeb;

import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController {

    @RequestMapping("/hello")  
    public String redirect()  
    {  
        System.out.println("about to display viewpage.jsp");
        return "viewPage";  
    }     
    @RequestMapping("/helloagain")  
    public String display()  
    {  
        System.out.println("about to display final.jsp");
        return "final";  
    }  
}

索引.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>  
<body>  
<a href="hello">Click here...</a>  
</body>  
</html> 

视图页面.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>  
<body>  
<a href="helloagain">Click viewPage</a>  
</body>  
</html>  

最终的.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>  
<body>  
<p>Welcome to Spring MVC Tutorial</p>  
</body>  
</html> 

错误页面:https ://i.stack.imgur.com/ixS8G.jpg

标签: javaspringeclipsespring-mvcrequest-mapping

解决方案


您正在返回return "viewpage"; 您的家庭控制器,但在您的项目结构图像中,它是 viewPage。所以我认为你应该在你的回报中输入与你的项目相同的文件名,因为它是区分大小写的。


推荐阅读