Spring 引导在多模块项目中找不到 jsp

Spring boot can't find jsp in multi modules project

我正在使用 spring 启动,并且为了举例,我决定将我的项目拆分为多个模块。我有 4 个模块,web、服务、存储库、域。除了 JSP、spring 找不到这些页面外,一切正常。但是当我没有将我的项目拆分为模块时它起作用了。我没有更改配置。

这是我的网络模块结构

Runner 位于 web 模块中,这里是:

@SpringBootApplication(scanBasePackages = "kz.epam.javalab.daimoncool")
public class WebApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }

}

这是我的 application.properties:

spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false

spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=
spring.datasource.password=
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.properties.hibernate.physical_naming_strategy=com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=
spring.mail.password=
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com

父 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>kz.epam.javalab.daimoncool</groupId>
    <artifactId>newsmanagementparent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>newsmanagementparent</name>
    <packaging>pom</packaging>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modules>
        <module>repository</module>
        <module>service</module>
        <module>web</module>
        <module>domain</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <module-version>0.0.1-SNAPSHOT</module-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.1-groovy-2.4</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>kz.epam.javalab.daimoncool</groupId>
                <artifactId>web</artifactId>
                <version>${module-version}</version>
            </dependency>
            <dependency>
                <groupId>kz.epam.javalab.daimoncool</groupId>
                <artifactId>service</artifactId>
                <version>${module-version}</version>
            </dependency>
            <dependency>
                <groupId>kz.epam.javalab.daimoncool</groupId>
                <artifactId>domain</artifactId>
                <version>${module-version}</version>
            </dependency>
            <dependency>
                <groupId>kz.epam.javalab.daimoncool</groupId>
                <artifactId>repository</artifactId>
                <version>${module-version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

和网络 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>kz.epam.javalab.daimoncool</groupId>
    <artifactId>web</artifactId>
    <packaging>war</packaging>

    <parent>
        <groupId>kz.epam.javalab.daimoncool</groupId>
        <artifactId>newsmanagementparent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>kz.epam.javalab.daimoncool</groupId>
            <artifactId>domain</artifactId>
        </dependency>

        <dependency>
            <groupId>kz.epam.javalab.daimoncool</groupId>
            <artifactId>service</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- Need this to compile JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!-- Need this to compile JSP -->
        <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>4.6.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

如果你能帮助我,我会很高兴)我再说一遍,当没有模块时它可以工作,不改变配置,只是添加模块它不起作用。

Result:

Intellij IDEA 还向我显示 index.jsp 没有视图解析器,即使我显式添加了视图解析器 bean

这里是项目的githublink: https://github.com/DaimonCool/newsmanagementmultimodules

我发现了问题。我需要更改 intellij IDEA 中的工作目录。 Spring boot runner 采用根路径,然后查找 jsp(在我的例子中它是父项目)。但是我的运行器在 web 模块中,所以我需要 spring boot runner 来将根路径更改为 web 模块(通过更改工作目录),而不是父级。