SpringMVC 通过Controller访问报404(源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示)

2022-07-27,,,,

最近在学习 SpringMVC,今天在学习的过程中遇到了一个页面报 404 的问题,具体情况是:在地址栏直接访问页面无异常,而通过 controller 访问则报 404,排查了两三个小时,终于找到了问题,我觉得有必要记录一下!

具体程序代码:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springmvc-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">

    <context:component-scan base-package="com.it.controller"/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

ControllerTest:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ControllerTest1 {
    @RequestMapping("/home")
    public String e1(Model model) {
        System.out.println("访问到了吗??");
        model.addAttribute("message", "再来一遍!!");
        return "home";
    }
}

home.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>practice</title>
</head>
<body>
<h1>欢迎访问</h1>
${message}
</body>
</html>

就是这样的一个程序,在浏览器的地址栏直接输入 http://localhost:8080/jsp/home.jsp 可以正常访问,并有一个 <h1> 的标题显示为欢迎访问;然而通过 http://localhost:8080/home 访问却报了 404 的错误!!

于是我在网上各种查,折腾了两个小时,也没找着错误。。。后来,终于,我发现错误了!!!
我这里使用的是 maven 项目,在 pom.xml 中导入依赖了,但是并没有被部署到 web 容器中,所以我们只需要将这些依赖部署到 tomcat 服务器中就可以了。

具体步骤如下

打开Project Structure,点击 Artifacts。

点击加号,选择 Library Files。

将这些依赖全部选中,然后OK。

这样之后,我的 404 问题就解决了,忽然感觉神清气爽,来瓶快乐水!如果对你有帮助,记得点赞关注我哦!

我是快斗,请多多指教!

本文地址:https://blog.csdn.net/qq_46127363/article/details/109851729

《SpringMVC 通过Controller访问报404(源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示).doc》

下载本文的Word格式文档,以方便收藏与打印。