First Spring MVC项目的HTTP状态404

时间:2016-10-27 21:02:35

标签: java spring jsp spring-mvc

我正在尝试在Spring MVC中运行我的第一个应用程序几天,我使用关键服务器启动Spring工具套件。

项目我从Spring in Action书中重写。 Ecipse在链接http://localhost:8081/Splittr/上自动运行它。也许有人在这个项目中看到任何错误。

项目看起来像这样

-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>

1 个答案:

答案 0 :(得分:0)

请尝试使用RootConfig.java上的@ComponentScan("spittr.*")