简体   繁体   中英

HTTP Status 404 on First Spring MVC Project

for several days I'm trying run my First application in Spring MVC , I start this project on spring tool suits with pivotal server.

Project i rewrite from Spring in Action book . Ecipse run it automatically on link http://localhost:8081/Splittr/ . Maybe someone see any error in this project.

Project looks like this

-src
 -main
  -spittr
     -config
      RootConfig.java
      SpittrWebAppInitializer.java
      WebConfig.java
     -web
      HomeController.java
   -webapp
        -WEB-INF
         -views
           home.jsp

SpittrWebAppInitializer.java

package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

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

    @Override
    protected Class<?>[] getRootConfigClasses(){
        return new Class<?>[] {RootConfig.class};
    }
    @Override
    protected Class<?>[] getServletConfigClasses(){
        return new Class<?>[] {WebConfig.class};
    }

}

RootConfig.java

package spittr.config;

@Configuration
@ComponentScan(basePackages={"spittr"},
excludeFilters ={
        @Filter(type=FilterType.ANNOTATION, value = EnableWebMvc.class)
})
public class RootConfig {

}

WebConfig

package spittr.config;

@Configuration
@EnableWebMvc
@ComponentScan("spittr.web") =
public class WebConfig extends WebMvcConfigurerAdapter{

    @Bean
    public ViewResolver viewResolver(){
        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();
    }

}

HomeController

package spittr.web;

@Controller
public class HomeController {

    @RequestMapping(value="/", method =GET)
    public String home(){
        return "home";
    }

}

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Spittr</title>
<link rel="stylesheet"
type="text/css"
href="<c:url value="/resources/style.css" />" >
</head>
<body>
<h1>Welocme in  Spittr</h1>
<a href="<c:url value="/spittles" />">Spittle</a> |
<a href="<c:url value="/spitter/register" />">Register</a>
</body>
</html>

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spitttr.web</groupId>
  <artifactId>Splittr</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Splittr Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
  <dependencies>
    <dependency>
      <groupId>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>
    <scope>provided</scope>
</dependency>
    <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.0.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>Splittr</finalName>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>
    </plugins>
  </build>
</project>

请在@ComponentScan("spittr.*")上尝试@ComponentScan("spittr.*")

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