Skip to content

Commit

Permalink
Add additional documentation about Collection<ModelAndView>
Browse files Browse the repository at this point in the history
  • Loading branch information
wimdeblauwe committed Nov 28, 2024
1 parent 73d041e commit eb81b87
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,20 @@ public View users(Model model) {
}
```

Example with `Collection<ModelAndView>`:

```java
@HxRequest
@GetMapping("/users")
public Collection<ModelAndView> users() {
return List.of(
new ModelAndView("users/list", Map.of("users", userRepository.findAll())),
new ModelAndView("users/count", Map.of("count", userRepository.count()))
);
}
```


### Exceptions

It is also possible to use `HtmxRequest` and `HtmxResponse` as method argument in handler methods annotated with `@ExceptionHandler`.
Expand All @@ -208,6 +222,16 @@ public String handleError(Exception ex, HtmxRequest htmxRequest, HtmxResponse ht
}
```

It is also possible to add annotations on an exception handler to set response headers.

```java
@ExceptionHandler(Exception.class)
@HxRetarget("#error-message")
public String handleError(Exception ex) {
return "error";
}
```

### Spring Security

The library has an `HxRefreshHeaderAuthenticationEntryPoint` that you can use to have htmx force a full page browser
Expand Down Expand Up @@ -257,6 +281,20 @@ public View users(Model model) {
}
```

This is also possible using `Collection<ModelAndView` as return type:

```java
@HxRequest
@GetMapping("/users")
public Collection<ModelAndView> test() {
return List.of(
new ModelAndView("users :: list", Map.of("users", userRepository.findAll())),
new ModelAndView("users :: count", Map.of("count", userRepository.count()))
);
}

```

#### Dialect

The Thymeleaf dialect has appropriate processors that enable Thymeleaf to perform calculations and expressions
Expand Down Expand Up @@ -320,6 +358,7 @@ You can use multiple values like this:

Links to articles and blog posts about this library:

* [Redirect attributes with Spring MVC and htmx](https://www.wimdeblauwe.com/blog/2024/11/19/redirect-attributes-with-spring-mvc-and-htmx/)
* [Release 1.0.0 and 2.0.0 of htmx-spring-boot-thymeleaf](https://www.wimdeblauwe.com/blog/2022/12/11/release-1.0.0-and-2.0.0-of-htmx-spring-boot-thymeleaf/)
* [Htmx authentication error handling](https://www.wimdeblauwe.com/blog/2022/10/04/htmx-authentication-error-handling/)
* [Thymeleaf and htmx with out of band swaps](https://www.wimdeblauwe.com/blog/2022/06/15/thymeleaf-and-htmx-with-out-of-band-swaps/)
Expand Down

0 comments on commit eb81b87

Please sign in to comment.