简体   繁体   中英

Spring 4 mvc HTTP 404 rsource not found

I am new in Spring and i try a simple mvc example with java configuration. I receive an HTT 404 error when requesting my home page.

Spring 4, Tomcat 8.5, Eclipse Here comes my code

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>SpringProject</groupId>
    <artifactId>SpringProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>SpringProject</name>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <spring.version>4.3.4.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupd>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
</project>

DispacherServlet.java

package arnaud.spring.configuration;

public class DispatcherServletForMVC extends AbstractAnnotationConfigDispatcherServletInitializer {

    protected String[] getServletMappings(){
        return new String[]{"/"};
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        System.out.println("dans initialisation root");
        return null;        
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        System.out.println("dans initialisation servlet");
        return new Class<?>[] {WebConfig.class};
    }
}

WebConfig class

package arnaud.spring.configuration;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="arnaud.spring")
public class WebConfig extends WebMvcConfigurerAdapter {

    //define the view resolver
    @Bean
    public ViewResolver viewResolver(){
        System.out.println("view resolver");
        InternalResourceViewResolver resolver= new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
        configurer.enable();
        System.out.println("default handling");
    }
}

Now my controller...

package arnaud.spring.controllers;
@Controller
public class HomeController {

    @RequestMapping(value="/",method=RequestMethod.GET)
    public String home(){
        System.out.println("dans controller");
        return "HomePage";
    }
}

My HomePage.jsp located in WEB-INF/views

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                      "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix ="c" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Home Page</title>
    </head>
    <body>
        <a ref="<c:url value="/listinvoice"/>">list invoice</a>
        <a ref="<c:url value="/listinvoice/register"/>">register</a>
    </body>
</html>

Finally what i can see in the Eclipse console

mars 03, 2017 4:05:48 PM org.apache.tomcat.util.digester.SetPropertiesRule begin AVERTISSEMENT: [SetPropertiesRule]{Server/Service/Engine/Host/Context}
Setting property 'source' to 'org.eclipse.jst.jee.server:SpringMVCProject' did not find a matching property. mars 03, 2017 4:05:48 PM org.apache.catalina.startup.VersionLoggerListener log INFOS: Server version: Apache Tomcat/8.5.6 mars 03, 2017 4:05:48 PM org.apache.catalina.startup.VersionLoggerListener log INFOS: Server built: Oct 6 2016 20:15:31 UTC mars 03, 2017 4:05:48 PM org.apache.catalina.startup.VersionLoggerListener log INFOS: Server number: 8.5.6.0 mars 03, 2017 4:05:48 PM org.apache.catalina.startup.VersionLoggerListener log INFOS: OS Name: Windows 10 mars 03, 2017 4:05:48 PM org.apache.catalina.startup.VersionLoggerListener log INFOS: OS Version: 10.0 mars 03, 2017 4:05:48 PM org.apache.catalina.startup.VersionLoggerListener log INFOS: Architecture: amd64 mars 03, 2017 4:05:48 PM org.apache.catalina.startup.VersionLoggerListener log INFOS: Java Home: C:\\Program Files\\Java\\jre1.8.0_111 mars 03, 2017 4:05:48 PM org.apache.catalina.startup.VersionLoggerListener log INFOS: JVM Version: 1.8.0_111-b14 mars 03, 2017 4:05:48 PM org.apache.catalina.startup.VersionLoggerListener log INFOS: JVM Vendor: Oracle Corporation mars 03, 2017 4:05:48 PM org.apache.catalina.startup.VersionLoggerListener log mars 03, 2017 4:05:50 PM org.apache.catalina.core.ApplicationContext log INFOS: 1 Spring WebApplicationInitializers detected on classpath dans initialisation root dans initialisation servlet mars 03, 2017 4:05:51 PM org.apache.catalina.core.ApplicationContext log INFOS: Initializing Spring FrameworkServlet 'dispatcher' mars 03, 2017 4:05:51 PM org.springframework.web.servlet.DispatcherServlet initServletBean INFOS: FrameworkServlet 'dispatcher': initialization started mars 03, 2017 4:05:51 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh INFOS: Refreshing WebApplicationContext for namespace 'dispatcher-servlet':
startup date [Fri Mar 03 16:05:51 CET 2017]; root of context hierarchy mars 03, 2017 4:05:51 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions INFOS: Registering annotated classes: [class arnaud.spring.configuration.WebConfig] mars 03, 2017 4:05:52 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache INFOS: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Mar 03 16:05:51 CET 2017]; root of context hierarchy mars 03, 2017 4:05:52 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register INFOS: Mapped "{[/],methods=[GET]}" onto public java.lang.String arnaud.spring.controllers.HomeController.home() default handling mars 03, 2017 4:05:52 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler INFOS: Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler] view resolver mars 03, 2017 4:05:53 PM org.springframework.web.servlet.DispatcherServlet initServletBean INFOS: FrameworkServlet 'dispatcher': initialization completed in 2087 ms mars 03, 2017 4:05:55 PM org.apache.jasper.servlet.TldScanner scanJars INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. mars 03, 2017 4:05:57 PM org.apache.coyote.AbstractProtocol start INFOS: Starting ProtocolHandler [http-nio-8085] mars 03, 2017 4:05:57 PM org.apache.coyote.AbstractProtocol start INFOS: Starting ProtocolHandler [ajp-nio-8008] mars 03, 2017 4:05:57 PM org.apache.catalina.startup.Catalina start INFOS: Server startup in 9320 ms

I call the page with the following url, my tomcat in Eclipe in on port 8085

http://localhost:8085/SpringMVCProject/ or http://localhost:8085/SpringMVCProject

with same result:HTTP 404

Thanks very much for helping me solving this issue.

You need to Map your Dispatcher Servlet in the web.xml and your web config class this is using the Default dispatcher you need to replace with yours

       <servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>
    org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
  <param-name>contextClass</param-name>
  <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>arnaud.spring.configuration.WebConfig</param-value>
</init-param>

Like this

 <servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>
        arnaud.spring.configuration.DispatcherServletForMVC
</servlet-class>
<init-param>
  <param-name>contextClass</param-name>
  <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>arnaud.spring.configuration.WebConfig</param-value>
</init-param>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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