You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This project provides annotations, helper classes and a [Thymeleaf](https://www.thymeleaf.org/) dialect
8
-
to make it easy to work with [htmx](https://htmx.org/)
9
-
in a [Spring Boot](https://spring.io/projects/spring-boot) application.
7
+
The project simplifies the integration of [htmx](https://htmx.org/) with [Spring Boot](https://spring.io/projects/spring-boot) / [Spring Web MVC](https://docs.spring.io/spring-framework/reference/web/webmvc.html) applications.
8
+
It provides a set of views, annotations, and argument resolvers for controllers to easily handle htmx-related request and response headers.
9
+
This ensures seamless interaction between the frontend and backend, especially for dynamic content updates via htmx.
10
10
11
-
More information about htmx can be viewed on [their website](https://htmx.org/).
11
+
Additionally, the project includes a custom [Thymeleaf](https://www.thymeleaf.org/) dialect to enable smooth rendering of htmx-specific attributes within Thymeleaf templates.
12
+
With these tools, developers can quickly implement htmx-driven interactions, such as AJAX-based partial page updates, with minimal configuration.
12
13
13
14
## Maven configuration
14
15
@@ -45,7 +46,7 @@ Provides a [Thymeleaf](https://www.thymeleaf.org/) dialect to easily work with h
45
46
46
47
The included Spring Boot Auto-configuration will enable the htmx integrations.
@@ -57,8 +58,8 @@ The following method is called only if the request was made by htmx.
57
58
```java
58
59
@HxRequest
59
60
@GetMapping("/users")
60
-
publicStringhtmxRequest(){
61
-
return"partial";
61
+
publicStringusers() {
62
+
return"view";
62
63
}
63
64
```
64
65
@@ -70,8 +71,8 @@ or [HxRequest#triggerName](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spr
70
71
```java
71
72
@HxRequest("my-element")
72
73
@GetMapping("/users")
73
-
publicStringhtmxRequest(){
74
-
return"partial";
74
+
publicStringusers() {
75
+
return"view";
75
76
}
76
77
```
77
78
If you want to restrict the invocation of a controller method to having a specific target element defined,
@@ -80,34 +81,66 @@ use [HxRequest#target](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-
80
81
```java
81
82
@HxRequest(target="my-target")
82
83
@GetMapping("/users")
83
-
publicStringhtmxRequest(){
84
-
return"partial";
84
+
publicStringusers() {
85
+
return"view";
85
86
}
86
87
```
87
88
88
-
#### Using HtmxRequest to access HTTP request headers sent by htmx
89
+
###Request Headers
89
90
90
-
The [HtmxRequest](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxRequest.html) object can be used as controller method parameter to access the various [htmx Request Headers](https://htmx.org/reference/#request_headers).
91
+
To access the various [htmx Request Headers](https://htmx.org/reference/#request_headers) in a controller method, you can use the class [HtmxRequest](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxRequest.html)
There are two ways to set [htmx Response Headers](https://htmx.org/reference/#response_headers) on controller methods.
106
-
The first is to use annotations, e.g. `@HxTrigger`, and the second is to use the class [HtmxResponse](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxResponse.html) as the return type of the controller method.
107
+
There are two ways to set [htmx Response Headers](https://htmx.org/reference/#response_headers) in controller methods. The first is to use [HtmxResponse](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxResponse.html)
108
+
as controller method argument in combination with different Views e.g. [HtmxRedirectView](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxRedirectView.html)
109
+
as return value. The second is to use annotations, e.g. `@HxTrigger` to set the necessary response headers. The first method is more flexible and allows you to dynamically set the response headers based on the request.
107
110
108
-
See [Response Headers Reference](https://htmx.org/reference/#response_headers) for the related htmx documentation.
111
+
#### HtmxResponse and Views
112
+
113
+
Most of the [htmx Response Headers](https://htmx.org/reference/#response_headers) can be set by using [HtmxResponse](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxResponse.html) as controller method argument,
114
+
except for some control flow response headers such as [HX-Redirect](https://htmx.org/headers/hx-redirect/). For these response headers, you have to use a corresponding view as return value of the controller method.
115
+
116
+
*[HtmxRedirectView](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxRedirectView.html) - sets the [HX-Redirect](https://htmx.org/headers/hx-redirect/) header to do a client-side redirect.
117
+
*[HtmxLocationRedirectView](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxLocationRedirectView.html) - sets the [HX-Location](https://htmx.org/headers/hx-location/) header to do a client-side redirect without reloading the whole page.
118
+
*[HtmxRefreshView](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxRefreshView.html) - sets the [HX-Refresh](https://htmx.org/headers/hx-refresh/) header to do a client-side refresh of the current page.
@@ -122,115 +155,69 @@ The following annotations are currently supported:
122
155
123
156
>**Note** Please refer to the related Javadoc to learn more about the available options.
124
157
125
-
#### Examples
126
-
127
158
If you want htmx to trigger an event after the response is processed, you can use the annotation `@HxTrigger` which sets the necessary response header [HX-Trigger](https://htmx.org/headers/hx-trigger/).
128
159
129
160
```java
130
161
@HxRequest
131
162
@HxTrigger("userUpdated") // the event 'userUpdated' will be triggered by htmx
132
163
@GetMapping("/users")
133
-
publicString hxUpdateUser(){
134
-
return"partial";
135
-
}
136
-
```
137
-
138
-
If you want to do the same, but in a more flexible way, you can use `HtmxResponse` as the return type in the controller method instead.
139
-
```java
140
-
@HxRequest
141
-
@GetMapping("/users")
142
-
publicHtmxResponse hxUpdateUser(){
143
-
returnHtmxResponse.builder()
144
-
.trigger("userUpdated") // the event 'userUpdated' will be triggered by htmx
145
-
.view("partial")
146
-
.build();
164
+
publicString users() {
165
+
return"view";
147
166
}
148
167
```
149
168
150
-
### Out Of Band Swaps
169
+
### HTML Fragments
151
170
152
-
htmx supports updating multiple targets by returning multiple partials in a single response, which is called [Out Of Band Swaps](https://htmx.org/docs/#oob_swaps).
153
-
For this purpose, use [HtmxResponse](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxResponse.html)
154
-
as the return type of a controller method, where you can add multiple templates.
171
+
In Spring MVC, view rendering typically involves specifying one view and one model. However, in htmx a common capability is to send multiple HTML fragments that
172
+
htmx can use to update different parts of the page, which is called [Out Of Band Swaps](https://htmx.org/docs/#oob_swaps). For this, controller methods can return
An `HtmxResponse` can be formed from view names, as above, or fully resolved `View` instances, if the controller knows how
169
-
to do that, or from `ModelAndView` instances (resolved or unresolved). For example:
190
+
An `HtmxView` can be formed from view names, as above, or fully resolved `View` instances, if the controller knows how
191
+
to do that, or from `ModelAndView` instances (resolved or unresolved). Each fragment can have its own model, which is merged with the controller model before rendering.
The Thymeleaf integration for Spring supports the specification of a [Markup Selector](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-c-markup-selector-syntax)
262
247
for views. The Markup Selector will be used for selecting the section
@@ -270,13 +255,16 @@ fragment `count` (th:fragment="count") from the template `users`.
270
255
271
256
```java
272
257
@HxRequest
273
-
@GetMapping("/partials/main-and-partial")
274
-
public HtmxResponse getMainAndPartial(Model model){
0 commit comments