Spring MVC 中的垂直分隔

Vertical separation in Spring MVC

我将制作一个 Spring MVC web 应用程序,其中应用程序是垂直分隔的。每个模块将封装与相同特性相关的功能。

让我们考虑一个假想的简单例子:

  1. feature-1: 用户可以add/remove/edit直角坐标(x,y).
  2. feature-2: 在平面图中显示坐标

所以我在这里考虑的垂直分离是一个模块用于功能 1,另一个模块用于功能 2。

  1. 模块 1:包括 add/remove/edit
  2. 的实体、持久性和视图
  3. 模块 2:包括从持久性和图表视图中读取协调的服务。

我还考虑了另一个只是索引页面的模块和一个加载该索引页面的控制器,其中它只是一个容器,供其他模块在那里加载它们的视图。此索引页面将有一个导航栏,用于添加坐标显示图表。这些导航应该将控制权移交给另一个模块中的另一个控制器。

我构建这个项目结构:

─── project
    │
    ├── add-coordinate
    │    ├── src
    │    │   └── main
    │    │       ├── java
    │    │       │   └── com.example.coordinate.add
    │    │       │       ├── controller
    │    │       │       │   └── CoordinateController.java
    │    │       │       ├── persistence
    │    │       │       │   └── CoordinateRepository.java
    │    │       │       └── entity
    │    │       │           └── coordinate.java
    │    │       └── resources
    │    │           └── static
    │    │               ├── css
    │    │               │   └── style.css
    │    │               └── templates
    │    │                   └── coordinate.html
    │    └── pom.xml
    │
    ├── show-coordinate
    │    ├── src
    │    │   └── main
    │    │       ├── java
    │    │       │   └── com.example.coordinate.show
    │    │       │       ├── controller
    │    │       │       │   └── ShowCoordinateController.java
    │    │       │       └── service
    │    │       │           └── CoordinateService.java
    │    │       └── resources
    │    │           └── static
    │    │               ├── css
    │    │               │   └── style.css
    │    │               └── templates
    │    │                   └── showCoordinate.html
    │    └── pom.xml
    │
    ├── index-coordinate
    │    ├── src
    │    │   └── main
    │    │       ├── java
    │    │       │   └── com.example.coordinate.index
    │    │       │       ├── controller
    │    │       │       │   └── IndexController.java
    │    │       │       └── IndexApplication.java
    │    │       └── resources
    │    │           └── static
    │    │               ├── css
    │    │               │   └── style.css
    │    │               └── templates
    │    │                   └── index.html
    │    └── pom.xml
    │
    └── pom.xml

这是父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">

    <groupId>com.example</groupId>
    <artifactId>coordinate</artifactId>
    <version>${revison}</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
    </parent>

    <properties>
        ...
        <!--    Project Version -->
        <build.type>SNAPSHOT</build.type>
        <variance.version>1.0.0</variance.version>
        <revison>${variance.version}-${build.type}</revison>
    </properties>

    <modules>
        <module>index-coordinate</module>
        <module>add-coordinate</module>
        <module>show-coordinate</module>
    </modules>

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

这是加坐标pom:

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

    <artifactId>add-coordinate</artifactId>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>coordinate</artifactId>
        <version>${revison}</version>
    </parent>

    <properties>
        <start-class>com.example.coordinate.index.IndexApplication</start-class>
    </properties>

    <dependencies>
        <dependency>-->
            <groupId>com.example</groupId>
            <artifactId>coordinate-index</artifactId>
            <version>${revison}</version>
        </dependency>
        <!--other dependecies like Spring Boot, Sprong MVC, DB, ...-->
        ...
    </dependencies>

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

添加坐标的控制器如下:

@Controller
public class CoordinateController {

    @RequestMapping("/coordinate")
    public String addCoordinatePage() {
        return "coordinate";
    }
}

现在索引页面可以正常加载了,但是问题是点击添加坐标的导航栏,请求发送到http://localhost:8080/coordinate,页面无法解析。这意味着我的应用程序尚未启动控制器。有谁知道我该如何解决这个问题?

通过在 IndexApplication

中添加以下注释解决了问题
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.coordinate.*"})
@EnableJpaRepositories(basePackages = {"com.example.coordinate.*"})
@EntityScan(basePackages = {"com.example.coordinate.*"})
public class IndexApplication {

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

并添加了