My linux world » Java Spring – Web Fragment

Java Spring - Web Fragment


Contents

Prerequistes

Please read : Java spring – quickstart

Fragment 1

In fragment 1, create the following controller :

@Controller
@RequestMapping("/fragment1")
public class Fragment1Controller {
 
	@RequestMapping("/helloworld")
	public ModelAndView method() {
		String message = "Hello world from spring controller fragment 1 !";
		return new ModelAndView("fragment1/helloworld", "message", message);
	}
 
}

Fragment 2

In fragment 2, create the following controller :

@Controller
@RequestMapping("/fragment2")
public class Fragment2Controller {
 
	@RequestMapping("/helloworld")
	public ModelAndView method() {
		String message = "Hello world from spring controller fragment 2 !";
		return new ModelAndView("fragment2/helloworld", "message", message);
	}
 
}

webapp

Configuration

If you use maven, add the dependencies in your pom.xml :

<dependency>
  <groupId>${project.groupId}</groupId>
  <artifactId>web-fragment-1-jar</artifactId>
  <version>${project.version}</version>
</dependency>
 
<dependency>
  <groupId>${project.groupId}</groupId>
  <artifactId>web-fragment-2-jar</artifactId>
  <version>${project.version}</version>
</dependency>

Request

Now if you request http://localhost:8080/fragment1/helloworld, you will have the following message :

Hello world from spring controller fragment 1 !

Now if you request http://localhost:8080/fragment2/helloworld, you will have the following message :

Hello world from spring controller fragment 2 !

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.