-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathExampleController.java
More file actions
68 lines (58 loc) · 2.13 KB
/
ExampleController.java
File metadata and controls
68 lines (58 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package de.tschuehly.example.fragments;
import de.tschuehly.spring.viewcomponent.core.IViewContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
/**
* Example controller demonstrating fragment rendering usage.
*
* Different endpoints return different ViewContext types from the same ViewComponent,
* causing different fragments to be rendered.
*/
@Controller
public class ExampleController {
private final ButtonComponent buttonComponent;
public ExampleController(ButtonComponent buttonComponent) {
this.buttonComponent = buttonComponent;
}
/**
* Returns a primary button - renders the PrimaryButton fragment
*/
@GetMapping("/button/submit")
IViewContext submitButton() {
return buttonComponent.primary("Submit Form", "/api/submit");
}
/**
* Returns a secondary button - renders the SecondaryButton fragment
*/
@GetMapping("/button/cancel")
IViewContext cancelButton() {
return buttonComponent.secondary("Cancel", "/api/cancel");
}
/**
* Returns a danger button - renders the DangerButton fragment
*/
@GetMapping("/button/delete")
IViewContext deleteButton() {
return buttonComponent.danger(
"Delete Account",
"/api/delete-account",
"Are you sure you want to delete your account? This action cannot be undone."
);
}
/**
* Example showing nested component rendering with fragments.
*
* The page component can receive different button variants
* as child components.
*/
@GetMapping("/page/submit-form")
IViewContext submitFormPage() {
var submitButton = buttonComponent.primary("Submit", "/submit");
var cancelButton = buttonComponent.secondary("Cancel", "/cancel");
// In a real application, you might have a PageComponent that accepts
// button components as parameters
// return pageComponent.render(submitButton, cancelButton);
return submitButton; // Simplified for this example
}
}