【问题标题】:HTTP Status 404 on First Spring MVC Project第一个 Spring MVC 项目上的 HTTP 状态 404
【发布时间】:2017-03-10 16:49:28
【问题描述】:

几天来,我尝试在 Spring MVC 中运行我的第一个应用程序,我在带有关键服务器的 spring 工具套装上启动了这个项目。

我从 Spring in Action book 重写的项目。 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 {

}

网络配置

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();
    }

}

家庭控制器

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>

【问题讨论】:

  • 您是否已将包含网页的文件夹添加到类路径和部署程序集?
  • 我通过 maven 构建项目,但我真的不知道这是否足够。在项目属性的部署程序集中,我的源代码为 src/main/webapp
  • 网页是否在src/main/webapp中?此外,如果您在 apache 服务器中运行,请检查运行配置。右键单击项目 > 运行 > 运行配置。单击参数选项卡。有一个称为工作目录的块。这是指定工作空间的地方。如果您的项目在默认工作区中,则默认值是默认值就可以了。否则,单击其他并选择您的项目所在的工作区。
  • 是的,home.jsp 在 src/main/webapp 中,工作目录指向默认目录我使用关键服务器
  • 首先尝试运行localhost:8081,如果仍然不行,请分享您的 pom.xml 和 web.xml。

标签: java spring jsp spring-mvc


【解决方案1】:

请在您的 RootConfig.java 上尝试@ComponentScan("spittr.*")

【讨论】:

    猜你喜欢
    • 2016-03-17
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2015-11-25
    相关资源
    最近更新 更多