Issue
I am going to make a Spring MVC web application in which the application is separated vertically. Each module is going to encapsulate the functionalities related to the same feature.
Let consider an imaginary simple example:
- feature-1: User can add/remove/edit Cartesian coordinate (x,y).
- feature-2: Show the coordinates in a plane chart.
So the vertical separation that I considered here is one module for feature-1 and another module for feature-2.
- Module 1: includes the entities, persistence and views for add/remove/edit
- Module 2: includes the service to read coordination from persistence and a view for the chart.
I also considered another module that is just index page and a controller that loads that index page in which it is just a container for the other modules to load their view there. This index page will have a navigation bar for add coordinate and show chart. These navigation should hand over the control to another controller which is in another module.
I build this project structure:
─── 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
This is the parent pom.xml (unrelated parts are omitted):
<?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>
And this is add-coordinate 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>
Here is the controller for add-coordinate:
@Controller
public class CoordinateController {
@RequestMapping("/coordinate")
public String addCoordinatePage() {
return "coordinate";
}
}
Now, index page is loaded properly, but the problem is that when the nav bar for add coordinate is clicked and the request is sent to http://localhost:8080/coordinate
, the page cannot be resolved. Which means the controller has not been started with my application. Does anyone know how I can resolve this problem?
Solution
The problem solved by adding the following annotation to 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);
}
}
And added the
Answered By - Poorya Hosseini
Answer Checked By - Robin (JavaFixing Admin)