【问题标题】:Error404:The origin server did not find a current representation for the target resource or is not willing to disclose that one existsError404:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示
【发布时间】:2021-09-28 05:54:44
【问题描述】:

我是 springboot 新手,正在尝试使用 tomcat 9.0.53 将控制器连接到网页。虽然与服务器的连接成功,但是我无法在服务器上显示视图,它给了我这个错误。错误 404:源服务器没有找到目标资源的当前表示或不愿意透露存在。

这是我的控制器类:

package com.emusicstore.controller;

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

@Controller
public class homeController {
    @RequestMapping("/home")
    public String home()
    {
        return "home";
    }
}

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

Dispatcher_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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.emusicstore"/>
    <mvc:annotation-driven />


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp"/>
    </bean>


</beans>

请帮我找出根本问题。

【问题讨论】:

    标签: spring-mvc


    【解决方案1】:

    确保您检查您的 tomcat 日志,它通常会给出有关问题的想法,现在看起来将名称从“Dispatcher_Servlet.xml”更改为“dispatcher-servlet。 xml' 还要检查 dispatcher-servlet.xml 是放在 WEB-INF 文件夹之后

    如果你使用 maven 那么:

    1)Maven 清理

    2)Maven 安装

    3)再次运行项目

    <?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>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> //make sure the servlet name is correct i.e Case sensitive
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    

    【讨论】:

    • 嘿,谢谢你的线索。但我只保留了 dispatcher-servlet,在这里发布问题时我出错了。文件名是 dispatcher-servlet.xml,我已经检查过了它匹配的 url 模式。我正在研究 spring。但问题尚未解决
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2017-10-21
    • 1970-01-01
    • 2018-04-07
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多