java - 未达到 WebApplicationInitializer

标签 java spring maven spring-mvc

我正在使用 Spring 为学校项目构建 BlogPost。我可以访问我的 index.jsp,但是当我映射到其他 Controller 时,我得到一个 404。当我在我的 WebApplicationInitializer 中的 @OnStartUp 方法中设置断点时,它永远不会到达。有人知道我在这里缺少什么吗?谢谢!

这是我的代码:

网络应用初始化器:

public class Initializer implements WebApplicationInitializer{
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext dispatcherContext = getContext();
        servletContext.addListener(new ContextLoaderListener(dispatcherContext));
        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }

    private WebApplicationContext getContext(){
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("be.kdg.BlogPostSpringMVC.configuration");
        return context;
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>be.kdg.BlogPostWebApp</groupId>
<artifactId>BlogPostWebApp</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>be.kdg.spring.BlogPost</groupId>
        <artifactId>BlogPost</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

以及注入(inject)的 BlogPost 的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>be.kdg.spring.BlogPost</groupId>
<artifactId>BlogPost</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
</dependencies>

最佳答案

context.setConfigLocation("be.kdg.BlogPostSpringMVC.configuration") 更改为 context.scan("be.kdg.BlogPostSpringMVC.configuration")

setConfigLocation()用于指定spring配置文件的位置。

关于java - 未达到 WebApplicationInitializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26740023/

相关文章:

java - 添加大型查询时如何修复 SpreadSheetAddRows 函数崩溃?

java - 将现有的 Jersey Postgres 应用程序迁移到 Spring Boot

java - Flink使用gradle代替maven的优缺点

java - Content-Length header 已存在

java - 使 Eclipse 鼓励特定的关键字顺序

javascript - Spring Boot获取复选框值

hibernate - 哪个版本的 Spring + Hibernate + JBoss 工作

java - 使用 Spring Boot 在 Angular 中成功调用 OPTIONS 后,未调用 GET 请求

java - Java Maven 项目的 Marginalia/Docco/etc?

java - 尽管通过 @Test(testName = "sth") 设置测试名称,ITestResult getTestName() 返回 null