Skip to content

Commit 58cddcc

Browse files
authored
Merge pull request #146 from xhaggi/fix/string-based-redirects
[fix] string-based redirections with flash attributes not working
2 parents d535857 + dd8335d commit 58cddcc

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ except for some control flow response headers such as [HX-Redirect](https://htmx
120120
##### Special view name prefixes
121121
For these views, there is also a special view name handling if you prefer to return a view name instead of a view instance.
122122

123-
* Redirect URLs can be specified via `htmx:redirect:`, e.g. `htmx:redirect:/path`, which causes htmx to perform a redirect to the specified URL.
124-
* 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.
125-
* A refresh of the current page can be specified using `htmx:refresh`.
123+
* Redirect URLs can be specified via `redirect:htmx:`, e.g. `redirect:htmx:/path`, which causes htmx to perform a redirect to the specified URL.
124+
* 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.
125+
* A refresh of the current page can be specified using `refresh:htmx`.
126126

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

142-
return "htmx:redirect:/user/list";
142+
return "redirect:htmx:/user/list";
143143
}
144144
```
145145

htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxViewResolver.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ public class HtmxViewResolver extends WebApplicationObjectSupport implements Vie
2222
* Prefix for special view names that specify a redirect URL
2323
* that htmx should navigate to.
2424
*/
25-
public static final String REDIRECT_URL_PREFIX = "htmx:redirect:";
25+
public static final String REDIRECT_URL_PREFIX = "redirect:htmx:";
2626

2727
/**
2828
* Prefix for special view names that specify a redirect URL that
2929
* htmx should navigate to without a full page reload.
3030
*/
31-
public static final String LOCATION_URL_PREFIX = "htmx:location:";
31+
public static final String LOCATION_URL_PREFIX = "redirect:htmx:location:";
3232

3333
/**
3434
* Prefix for special view names that specify a refresh of the current page.
3535
*/
36-
public static final String REFRESH_VIEW_NAME = "htmx:refresh";
36+
public static final String REFRESH_VIEW_NAME = "refresh:htmx";
3737

3838
private int order = Ordered.LOWEST_PRECEDENCE;
3939

@@ -62,19 +62,19 @@ public View resolveViewName(String viewName, Locale locale) throws Exception {
6262
return new HtmxRefreshView();
6363
}
6464

65-
if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
66-
String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
67-
RedirectView view = new HtmxRedirectView(redirectUrl, isRedirectContextRelative());
65+
if (viewName.startsWith(LOCATION_URL_PREFIX)) {
66+
String redirectUrl = viewName.substring(LOCATION_URL_PREFIX.length());
67+
RedirectView view = new HtmxLocationRedirectView(redirectUrl, isRedirectContextRelative());
6868
String[] hosts = getRedirectHosts();
6969
if (hosts != null) {
7070
view.setHosts(hosts);
7171
}
7272
return view;
7373
}
7474

75-
if (viewName.startsWith(LOCATION_URL_PREFIX)) {
76-
String redirectUrl = viewName.substring(LOCATION_URL_PREFIX.length());
77-
RedirectView view = new HtmxLocationRedirectView(redirectUrl, isRedirectContextRelative());
75+
if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
76+
String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
77+
RedirectView view = new HtmxRedirectView(redirectUrl, isRedirectContextRelative());
7878
String[] hosts = getRedirectHosts();
7979
if (hosts != null) {
8080
view.setHosts(hosts);

htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerMethodTest.java

+37-3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ public void testLocationRedirectViewNamePrefixContextRelative() throws Exception
7474
.andExpect(header().string("HX-Location", "/contextpath/path"));
7575
}
7676

77+
@Test
78+
public void testLocationRedirectViewNamePrefixFlashAttributes() throws Exception {
79+
80+
mockMvc.perform(get("/location-redirect-view-name-prefix-flash-attributes").headers(htmxRequest()))
81+
.andExpect(status().isOk())
82+
.andExpect(header().string("HX-Location", "/path"))
83+
.andExpect(flash().attribute("flash", "test"));
84+
}
85+
7786
@Test
7887
public void testLocationRedirectWithContextData() throws Exception {
7988

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

160+
@Test
161+
public void testRedirectViewNamePrefixFlashAttributes() throws Exception {
162+
163+
mockMvc.perform(get("/contextpath/redirect-view-name-prefix-flash-attributes").contextPath("/contextpath").headers(htmxRequest()))
164+
.andExpect(status().isOk())
165+
.andExpect(header().string("HX-Redirect", "/contextpath/test"))
166+
.andExpect(flash().attribute("flash", "test"));;
167+
}
168+
151169
@Test
152170
public void testRedirectWithContextPath() throws Exception {
153171

@@ -216,7 +234,15 @@ public HtmxLocationRedirectView locationRedirectExposingModelAttributes(Redirect
216234
@HxRequest
217235
@GetMapping("/location-redirect-view-name-prefix")
218236
public String locationRedirectViewNamePrefix() {
219-
return "htmx:location:/path";
237+
return "redirect:htmx:location:/path";
238+
}
239+
240+
@HxRequest
241+
@GetMapping("/location-redirect-view-name-prefix-flash-attributes")
242+
public String locationRedirectWithViewNamePrefixFlashAttributes(RedirectAttributes redirectAttributes) {
243+
244+
redirectAttributes.addFlashAttribute("flash", "test");
245+
return "redirect:htmx:location:/path";
220246
}
221247

222248
@HxRequest
@@ -308,10 +334,18 @@ public HtmxRedirectView redirectExposingModelAttributes(RedirectAttributes attri
308334
return new HtmxRedirectView("/test");
309335
}
310336

337+
@HxRequest
338+
@GetMapping("/redirect-view-name-prefix-flash-attributes")
339+
public String redirectWithViewNamePrefixFlashAttributes(RedirectAttributes redirectAttributes) {
340+
341+
redirectAttributes.addFlashAttribute("flash", "test");
342+
return "redirect:htmx:/test";
343+
}
344+
311345
@HxRequest
312346
@GetMapping("/redirect-view-name-prefix")
313347
public String redirectViewNamePrefix() {
314-
return "htmx:redirect:/test";
348+
return "redirect:htmx:/test";
315349
}
316350

317351
@HxRequest
@@ -331,7 +365,7 @@ public HtmxRefreshView refresh() {
331365
@HxRequest
332366
@GetMapping("/refresh-view-name")
333367
public String refreshViewName() {
334-
return "htmx:refresh";
368+
return "refresh:htmx";
335369
}
336370

337371
@HxRequest

0 commit comments

Comments
 (0)