Skip to content

Commit

Permalink
[fix] string-based redirections with flash attributes not working
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaggi committed Nov 19, 2024
1 parent d535857 commit fc7e550
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
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

0 comments on commit fc7e550

Please sign in to comment.