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

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

具体步骤如下

打开Project Structure,点击 Artifacts。
在这里插入图片描述
点击加号,选择 Library Files。
在这里插入图片描述
将这些依赖全部选中,然后OK。
在这里插入图片描述
这样之后,我的 404 问题就解决了,忽然感觉神清气爽,来瓶快乐水!如果对你有帮助,记得点赞关注我哦!

我是快斗,请多多指教!

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值