【问题标题】:Vertical separation in Spring MVCSpring MVC 中的垂直分离
【发布时间】:2022-01-07 10:38:22
【问题描述】:

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

让我们考虑一个虚构的简单示例:

  1. feature-1:用户可以添加/删除/编辑笛卡尔坐标(x,y)。
  2. feature-2:在平面图中显示坐标。

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

  1. 模块 1:包括添加/删除/编辑的实体、持久性和视图
  2. 模块 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时,页面无法解析。这意味着控制器尚未与我的应用程序一起启动。有谁知道我该如何解决这个问题?

【问题讨论】:

    标签: spring spring-boot maven spring-mvc


    【解决方案1】:

    问题通过在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);
        }
    }
    

    并添加了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-19
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多