我將制作一個 Spring MVC Web 應用程式,其中應用程式是垂直分離的。每個模塊都將封裝與相同特性相關的功能。
讓我們考慮一個虛構的簡單示例:
- feature-1:用戶可以添加/洗掉/編輯笛卡爾坐標(x,y)。
- feature-2:在平面圖中顯示坐標。
所以我在這里考慮的垂直分離是一個模塊用于功能 1,另一個模塊用于功能 2。
- 模塊 1:包括添加/洗掉/編輯的物體、持久性和視圖
- 模塊 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時,頁面無法決議。這意味著控制器尚未與我的應用程式一起啟動。有誰知道我該如何解決這個問題?
uj5u.com熱心網友回復:
通過添加以下注釋解決了問題 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);
}
}
并添加了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/410281.html
標籤:
