【问题标题】:Spring MVC - HTTP Status 404 - The origin server did not find a current representation for the target resourceSpring MVC - HTTP 状态 404 - 源服务器没有找到目标资源的当前表示
【发布时间】:2019-01-06 20:56:02
【问题描述】:

尝试创建 Spring MVC hello world web 应用。它当前返回 HTTP 状态 404。404 表示找不到文件,但我不知道为什么。我认为我的 servlet.xml 指向适当的文件夹(组件扫描)。我认为所有文件的名称都正确(没有拼写错误)。请参阅图片以了解错误。

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="helloworld" />

    <mvc:annotation-driven/>

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

</beans>

web.xml

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

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

HelloWorldController.java

package helloworld;

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

@Controller
public class HelloWorldController {

    @RequestMapping("/")
    public String showPage() {
        return "hello-world";
    }
}

hello-world.jsp

<!DOCTYPE html>
<html><body>Hello World!</body></html>

【问题讨论】:

  • 是的,servlet.xml 和 web.xml 不在 WEB-INF 下。

标签: spring-mvc jsp


【解决方案1】:

servlet.xml 和 web.xml 不在 WEB-INF 下。

【讨论】:

    【解决方案2】:

    RequestMapping 指向控制器中的“/”。尝试拨打http://localhost:8080/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-31
      • 2017-10-06
      • 2021-10-02
      • 2021-10-20
      • 2018-01-07
      • 2018-07-13
      • 2020-04-15
      • 2017-11-11
      相关资源
      最近更新 更多