I have these buttons:
<form>
<input type="submit" class="button" name="Test 1" value="Test 1">
<input type="submit" class="button" name="Test 2" value="Test 2">
<input type="submit" class="button" name="Test 3" value="Test 3">
<input type="submit" class="button" name="Test 4" value="Test 4">
<input type="submit" class="button" name="Test 5" value="Test 5">
</form>
I want each button to map to a separate controller like so:
@GetMapping("/panels")
public void test1() {
System.out.println("TEST 1!");
}
@GetMapping("/panels")
public void test2() {
System.out.println("TEST 2!");
}
@GetMapping("/panels")
public void test3() {
System.out.println("TEST 3!");
}
@GetMapping("/panels")
public void test4() {
System.out.println("TEST 4!");
}
@GetMapping("/panels")
public void test5() {
System.out.println("TEST 5!");
}
How do I do that? I haven’t found an explanation that works and/or that I understand. (I’m very new to Spring Boot.)
>Solution :
You can try something like the following
<form>
<button type="submit" formaction="http://localhost:8080/all-panels/panel1">Test1</button>
<button type="submit" formaction="http://localhost:8080/all-panels/panel2">Test2</button>
<button type="submit" formaction="http://localhost:8080/all-panels/panel3">Test3</button>
<button type="submit" formaction="http://localhost:8080/all-panels/panel4">Test4</button>
<button type="submit" formaction="http://localhost:8080/all-panels/panel5">Test5</button>
</form>
And then use a controller like
@RestController
@RequestMapping("all-panels")
public class PanelsController {
@GetMapping("/panel1")
public void test1() {
System.out.println("TEST 1!");
}
@GetMapping("/panel2")
public void test2() {
System.out.println("TEST 2!");
}
@GetMapping("/panel3")
public void test3() {
System.out.println("TEST 3!");
}
@GetMapping("/panel4")
public void test4() {
System.out.println("TEST 4!");
}
@GetMapping("/panel5")
public void test5() {
System.out.println("TEST 5!");
}
}
Keep in mind that while you develop your application you normally have both backend and frontend in your machine so the following will work formaction="http://localhost:8080/all-panels/panel1"
When however deployed for production the frontend will normally be the client sitting in his own computer. So then for this to work you will have to do
formaction="{backend-url}:{backend-port}/all-panels/panel1"
where {backend-url} is the domain where this is deployed and {backend-port} the port that your backend is listening to.