Sping MVC 404错误

时间:2018-02-01 05:31:43

标签: java spring spring-mvc

我收到404错误。我在控制台上看不到任何错误消息。我不完全确定我的错误在哪里,但我相信视图不是来自控制器。

如何定义控制器并从控制器返回视图。

这是我的文件夹结构:

├── SpringMVC ├── +WebContent ├── web-inf ├── -view ├── -hello.jsp ├── -web.xml ├── -springMVC-servlet.xml

这是我的控制器:

  package com.demo.springmvc
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    @EnableWebMvc
    @Controller
    public class HomeController {

        @RequestMapping(value = "/hello", method = RequestMethod.GET)
           public String printHello(ModelMap model) {
              model.addAttribute("message", "Hello Spring MVC Framework!");
              return "hello";
    }
    }

我的hello.jsp

         <%@ page language="java" contentType="text/html; charset=UTF-8"
                pageEncoding="UTF-8"%>
            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Insert title here</title>
            </head>
            <body>
            <h2>${message}</h2>
            </body>
            </html>

我的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="com.demo.springmvc" />

                <!-- 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.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>
          <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>
          <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.jsp</url-pattern>
          </servlet-mapping>
        </web-app>

错误:

INFO: Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.luv2code.springmvc.HomeController.printHello(org.springframework.ui.ModelMap)
Feb 01, 2018 1:29:56 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Feb 01 01:29:54 EST 2018]; root of context hierarchy
Feb 01, 2018 1:29:56 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Feb 01 01:29:54 EST 2018]; root of context hierarchy
Feb 01, 2018 1:29:56 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFO: Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.luv2code.springmvc.HomeController.printHello(org.springframework.ui.ModelMap)
Feb 01, 2018 1:29:56 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Feb 01 01:29:54 EST 2018]; root of context hierarchy
Feb 01, 2018 1:29:57 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 2301 ms
Feb 01, 2018 1:29:57 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Feb 01, 2018 1:29:57 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Feb 01, 2018 1:29:57 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 5985 ms

2 个答案:

答案 0 :(得分:0)

您可以将springMVC-servlet.xml重命名为spring-mvc-demo-servlet.xml

答案 1 :(得分:0)

这可能是您的文件夹结构,您的web.xml应该如下所示

Folder Structure of Project

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>spring-mvc-demo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>