Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] string-based redirections with flash attributes not working #146

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ except for some control flow response headers such as [HX-Redirect](https://htmx
##### Special view name prefixes
For these views, there is also a special view name handling if you prefer to return a view name instead of a view instance.

* Redirect URLs can be specified via `htmx:redirect:`, e.g. `htmx:redirect:/path`, which causes htmx to perform a redirect to the specified URL.
* Location redirect URLs can be specified via `htmx:location:`, e.g. `htmx:location:/path`, which causes htmx to perform a client-side redirect without reloading the entire page.
* A refresh of the current page can be specified using `htmx:refresh`.
* Redirect URLs can be specified via `redirect:htmx:`, e.g. `redirect:htmx:/path`, which causes htmx to perform a redirect to the specified URL.
* Location redirect URLs can be specified via `redirect:htmx:location:`, e.g. `redirect:htmx:location:/path`, which causes htmx to perform a client-side redirect without reloading the entire page.
* A refresh of the current page can be specified using `refresh:htmx`.

```java
@HxRequest
Expand All @@ -139,7 +139,7 @@ public String user(@PathVariable Long id, @ModelAttribute @Valid UserForm form,
redirectAttributes.addFlashAttribute("successMessage", "User has been successfully updated.");
htmxResponse.addTrigger("user-updated");

return "htmx:redirect:/user/list";
return "redirect:htmx:/user/list";
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ public class HtmxViewResolver extends WebApplicationObjectSupport implements Vie
* Prefix for special view names that specify a redirect URL
* that htmx should navigate to.
*/
public static final String REDIRECT_URL_PREFIX = "htmx:redirect:";
public static final String REDIRECT_URL_PREFIX = "redirect:htmx:";

/**
* Prefix for special view names that specify a redirect URL that
* htmx should navigate to without a full page reload.
*/
public static final String LOCATION_URL_PREFIX = "htmx:location:";
public static final String LOCATION_URL_PREFIX = "redirect:htmx:location:";

/**
* Prefix for special view names that specify a refresh of the current page.
*/
public static final String REFRESH_VIEW_NAME = "htmx:refresh";
public static final String REFRESH_VIEW_NAME = "refresh:htmx";

private int order = Ordered.LOWEST_PRECEDENCE;

Expand Down Expand Up @@ -62,19 +62,19 @@ public View resolveViewName(String viewName, Locale locale) throws Exception {
return new HtmxRefreshView();
}

if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
RedirectView view = new HtmxRedirectView(redirectUrl, isRedirectContextRelative());
if (viewName.startsWith(LOCATION_URL_PREFIX)) {
String redirectUrl = viewName.substring(LOCATION_URL_PREFIX.length());
RedirectView view = new HtmxLocationRedirectView(redirectUrl, isRedirectContextRelative());
String[] hosts = getRedirectHosts();
if (hosts != null) {
view.setHosts(hosts);
}
return view;
}

if (viewName.startsWith(LOCATION_URL_PREFIX)) {
String redirectUrl = viewName.substring(LOCATION_URL_PREFIX.length());
RedirectView view = new HtmxLocationRedirectView(redirectUrl, isRedirectContextRelative());
if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
RedirectView view = new HtmxRedirectView(redirectUrl, isRedirectContextRelative());
String[] hosts = getRedirectHosts();
if (hosts != null) {
view.setHosts(hosts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public void testLocationRedirectViewNamePrefixContextRelative() throws Exception
.andExpect(header().string("HX-Location", "/contextpath/path"));
}

@Test
public void testLocationRedirectViewNamePrefixFlashAttributes() throws Exception {

mockMvc.perform(get("/location-redirect-view-name-prefix-flash-attributes").headers(htmxRequest()))
.andExpect(status().isOk())
.andExpect(header().string("HX-Location", "/path"))
.andExpect(flash().attribute("flash", "test"));
}

@Test
public void testLocationRedirectWithContextData() throws Exception {

Expand Down Expand Up @@ -148,6 +157,15 @@ public void testRedirectViewNamePrefixContextRelative() throws Exception {
.andExpect(header().string("HX-Redirect", "/contextpath/test"));
}

@Test
public void testRedirectViewNamePrefixFlashAttributes() throws Exception {

mockMvc.perform(get("/contextpath/redirect-view-name-prefix-flash-attributes").contextPath("/contextpath").headers(htmxRequest()))
.andExpect(status().isOk())
.andExpect(header().string("HX-Redirect", "/contextpath/test"))
.andExpect(flash().attribute("flash", "test"));;
}

@Test
public void testRedirectWithContextPath() throws Exception {

Expand Down Expand Up @@ -216,7 +234,15 @@ public HtmxLocationRedirectView locationRedirectExposingModelAttributes(Redirect
@HxRequest
@GetMapping("/location-redirect-view-name-prefix")
public String locationRedirectViewNamePrefix() {
return "htmx:location:/path";
return "redirect:htmx:location:/path";
}

@HxRequest
@GetMapping("/location-redirect-view-name-prefix-flash-attributes")
public String locationRedirectWithViewNamePrefixFlashAttributes(RedirectAttributes redirectAttributes) {

redirectAttributes.addFlashAttribute("flash", "test");
return "redirect:htmx:location:/path";
}

@HxRequest
Expand Down Expand Up @@ -308,10 +334,18 @@ public HtmxRedirectView redirectExposingModelAttributes(RedirectAttributes attri
return new HtmxRedirectView("/test");
}

@HxRequest
@GetMapping("/redirect-view-name-prefix-flash-attributes")
public String redirectWithViewNamePrefixFlashAttributes(RedirectAttributes redirectAttributes) {

redirectAttributes.addFlashAttribute("flash", "test");
return "redirect:htmx:/test";
}

@HxRequest
@GetMapping("/redirect-view-name-prefix")
public String redirectViewNamePrefix() {
return "htmx:redirect:/test";
return "redirect:htmx:/test";
}

@HxRequest
Expand All @@ -331,7 +365,7 @@ public HtmxRefreshView refresh() {
@HxRequest
@GetMapping("/refresh-view-name")
public String refreshViewName() {
return "htmx:refresh";
return "refresh:htmx";
}

@HxRequest
Expand Down