繁体   English   中英

HTTP状态码404 Spring MVC

[英]Http status code 404 spring mvc

我是Spring MVC的新手,我只是在尝试一个基本示例。 但是我收到Http Stats 404错误。 我的档案如下:

这是一个行家项目。

/Web-Inf/spring-mvc-demo-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">

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="springdemo" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

然后我有WEB-INF / 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">

    <display-name>spring-mvc-demo</display-name>

    <!-- Spring MVC Configs -->

    <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
    <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/spring-mvc-demo-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

/WEB-INF/view/main-page.jsp

<html>

<body>

<h1>Welcome to home page!!!!</h1>

</body>

</html>

然后src / HomeController.java

package springmvcdemo;

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

@Component
public class HomeController {

    @RequestMapping("/")
    public String startPage(){
        return "main-page";
    }
}

我正在Eclipse IDE中创建一个动态Web项目,它是一个Maven项目,我要在其上运行它的服务器是Tomcat 8.0.44

有人可以帮助我消除错误吗?

我在spring-mvc-demo-servlet.xml中看到软件包名称与控制器所在的软件包不匹配。

弹簧MVC-演示servlet.xml中: -

 <context:component-scan base-package="springdemo" />

HomeController中,

package springmvcdemo;

由于您的servlet名称是web.xml中的dispatcher。 spring将尝试加载名为dispatcher-servlet.xml的配置文件

在dispatcher-servlet.xml中,您必须提供组件扫描

您的示例似乎没有正确的xsd版本,可能是由于jar文件版本

最好试试这个例子

Spring 4 MVC Hello World示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM