Class HtmxResponse
java.lang.Object
io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxResponse
A holder for htmx-related response headers that can be used as method argument in controllers.
Example usage in an @Controller:
@GetMapping(value = "/user")
public String view(@RequestParam Long id, HtmxResponse htmxResponse) {
htmxResponse.addTrigger("user-viewed");
return "/user/view";
}
- Author:
- Oliver Drotbohm, Clint Checketts, Sascha Woo
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddTemplate(String template) Deprecated.useHtmxResponse.Builder.view(String)instead.addTemplate(org.springframework.web.servlet.ModelAndView template) Deprecated.useHtmxResponse.Builder.view(ModelAndView)instead.addTemplate(org.springframework.web.servlet.View template) Deprecated.useHtmxResponse.Builder.view(View)instead.addTrigger(String eventName) Deprecated.Return value is changed to void in 4.0.voidaddTrigger(String eventName, Object eventDetail) Adds an event that will be triggered once the response is received.addTrigger(String eventName, String eventDetail, HxTriggerLifecycle step) Deprecated.useHtmxResponse.Builder.trigger(String, Object)instead.voidaddTriggerAfterSettle(String eventName) Adds an event that will be triggered after the settling step.voidaddTriggerAfterSettle(String eventName, Object eventDetail) Adds an event that will be triggered after the settling step.voidaddTriggerAfterSwap(String eventName) Adds an event that will be triggered after the swap step.voidaddTriggerAfterSwap(String eventName, Object eventDetail) Adds an event that will be triggered after the swap step.and(HtmxResponse otherResponse) Deprecated.useHtmxResponse.Builder.and(HtmxResponse)instead.browserRedirect(String url) Deprecated.useHtmxResponse.Builder.redirect(String)instead.browserRefresh(boolean refresh) Deprecated.useHtmxResponse.Builder.refresh()instead.static HtmxResponse.Builderbuilder()Deprecated.useHtmxResponseas handler method argument andHtmxView,HtmxRedirectVieworHtmxLocationRedirectViewas handler method return type instead.Deprecated.usegetReswap()instead.Deprecated.Replaced byHtmxLocationRedirectView.Deprecated.Replaced byHtmxRedirectView.Collection<org.springframework.web.servlet.ModelAndView>Deprecated.usegetViews()instead.Collection<org.springframework.web.servlet.ModelAndView>getViews()Deprecated.Replaced byHtmxView.booleanbooleanDeprecated.Replaced byHtmxRefreshView.voidPrevents the browser history stack from being updated.pushHistory(String url) Deprecated.useHtmxResponse.Builder.pushUrl(String)instead.reswap(HxSwapType swapType) Deprecated.usereswap(HxSwapType)instead.Deprecated.useHtmxResponse.Builder.retarget(String)instead.voidsetContextRelative(boolean contextRelative) Set whether URLs used in the htmx response that starts with a slash ("/") should be interpreted as relative to the current ServletContext, i.e.voidsetPushUrl(String url) Pushes a new URL into the history stack of the browser.voidsetReplaceUrl(String url) Allows you to replace the most recent entry, i.e.voidsetReselect(String cssSelector) Set a CSS selector that allows you to choose which part of the response is used to be swapped in.voidsetReswap(HtmxReswap reswap) Allows you to specify how the response will be swapped.voidsetRetarget(String cssSelector) Set a CSS selector that updates the target of the content update to a different element on the page
-
Constructor Details
-
HtmxResponse
public HtmxResponse()Create a new HtmxResponse.
-
-
Method Details
-
builder
Deprecated.useHtmxResponseas handler method argument andHtmxView,HtmxRedirectVieworHtmxLocationRedirectViewas handler method return type instead. Will be removed in 4.0.Return a builder to build aHtmxResponse.- Returns:
- the builder
-
addTrigger
Deprecated.Return value is changed to void in 4.0.Adds an event that will be triggered once the response is received.Multiple trigger were automatically be merged into the same header.
- Parameters:
eventName- the event name- See Also:
-
addTrigger
Adds an event that will be triggered once the response is received.Multiple trigger were automatically be merged into the same header.
- Parameters:
eventName- the event nameeventDetail- details along with the event- Since:
- 3.6.0
- See Also:
-
addTriggerAfterSettle
Adds an event that will be triggered after the settling step.Multiple triggers were automatically be merged into the same header.
- Parameters:
eventName- the event name- Since:
- 3.6.0
- See Also:
-
addTriggerAfterSettle
Adds an event that will be triggered after the settling step.Multiple triggers were automatically be merged into the same header.
- Parameters:
eventName- the event nameeventDetail- details along with the event- Since:
- 3.6.0
- See Also:
-
addTriggerAfterSwap
Adds an event that will be triggered after the swap step.Multiple triggers were automatically be merged into the same header.
- Parameters:
eventName- the event name- Since:
- 3.6.0
- See Also:
-
addTriggerAfterSwap
Adds an event that will be triggered after the swap step.Multiple triggers were automatically be merged into the same header.
- Parameters:
eventName- the event nameeventDetail- details along with the event- Since:
- 3.6.0
- See Also:
-
preventHistoryUpdate
public void preventHistoryUpdate()Prevents the browser history stack from being updated.- Since:
- 3.6.0
- See Also:
-
- HX-Push-Url Response Header documentation
- HX-Replace-Url Response Header
-
setContextRelative
public void setContextRelative(boolean contextRelative) Set whether URLs used in the htmx response that starts with a slash ("/") should be interpreted as relative to the current ServletContext, i.e. as relative to the web application root. Default is "true": A URL that starts with a slash will be interpreted as relative to the web application root, i.e. the context path will be prepended to the URL.- Parameters:
contextRelative- whether to interpret URLs in the htmx response as relative to the current ServletContext
-
setPushUrl
Pushes a new URL into the history stack of the browser.If you want to prevent the history stack from being updated, use
preventHistoryUpdate().- Parameters:
url- the URL to push into the history stack. The URL can be any URL in the same origin as the current URL.- Since:
- 3.6.0
- See Also:
-
- HX-Push Response Header documentation
- history.pushState()
-
setReplaceUrl
Allows you to replace the most recent entry, i.e. the current URL, in the browser history stack.If you want to prevent the history stack from being updated, use
preventHistoryUpdate().- Parameters:
url- the URL to replace in the history stack. The URL can be any URL in the same origin as the current URL.- Since:
- 3.6.0
- See Also:
-
setReselect
Set a CSS selector that allows you to choose which part of the response is used to be swapped in. Overrides an existing hx-select on the triggering element.- Parameters:
cssSelector- the CSS selector- Since:
- 3.6.0
- See Also:
-
setReswap
Allows you to specify how the response will be swapped. See hx-swap for possible values.- Parameters:
reswap- the reswap options.- Since:
- 3.6.0
- See Also:
-
setRetarget
Set a CSS selector that updates the target of the content update to a different element on the page- Parameters:
cssSelector- the CSS selector- Since:
- 3.6.0
- See Also:
-
addTemplate
Deprecated.useHtmxResponse.Builder.view(String)instead. Will be removed in 4.0.Append the rendered template or fragment.- Parameters:
template- must not be null or empty.- Returns:
- same HtmxResponse for chaining
-
addTemplate
Deprecated.useHtmxResponse.Builder.view(View)instead. Will be removed in 4.0.Append the rendered template or fragment as a resolvedView.- Parameters:
template- must not be null.- Returns:
- same HtmxResponse for chaining
-
addTemplate
Deprecated.useHtmxResponse.Builder.view(ModelAndView)instead. Will be removed in 4.0.Append the rendered template or fragment as aModelAndView.- Parameters:
template- must not be null.- Returns:
- same HtmxResponse for chaining
-
addTrigger
@Deprecated public HtmxResponse addTrigger(String eventName, String eventDetail, HxTriggerLifecycle step) Deprecated.useHtmxResponse.Builder.trigger(String, Object)instead. Will be removed in 4.0.Set a HX-Trigger (or HX-Trigger-After-Settle or HX-Trigger-After-Swap headers. Multiple trigger were automatically be merged into the same header.- Parameters:
eventName- must not be null or empty.eventDetail- can be null.step- must not be null or empty.- Returns:
- same HtmxResponse for chaining
- See Also:
-
pushHistory
Deprecated.useHtmxResponse.Builder.pushUrl(String)instead. Will be removed in 4.0.Pushes a new url into the history stack- Parameters:
url- must not be null or empty. false prevents the browser history from being updated- Returns:
- same HtmxResponse for chaining
- See Also:
-
- HX-Push Response Header documentation
-
browserRedirect
Deprecated.useHtmxResponse.Builder.redirect(String)instead. Will be removed in 4.0.Can be used to do a client-side redirect to a new location- Parameters:
url- can be a relative or an absolute url- Returns:
- same HtmxResponse for chaining
-
browserRefresh
Deprecated.useHtmxResponse.Builder.refresh()instead. Will be removed in 4.0.If set to "true" the client side will do a full refresh of the page- Parameters:
refresh- boolean to indicate full refresh or not.- Returns:
- same HtmxResponse for chaining
-
retarget
Deprecated.useHtmxResponse.Builder.retarget(String)instead. Will be removed in 4.0.Set a CSS selector that updates the target of the content update to a different element on the page- Parameters:
cssSelector- must not be null or empty.- Returns:
- same HtmxResponse for chaining
-
reswap
Deprecated.usereswap(HxSwapType)instead. Will be removed in 4.0.Set a new swap to specify how the response will be swapped- Parameters:
swapType- must not be null.- Returns:
- same HtmxResponse for chaining
-
and
Deprecated.useHtmxResponse.Builder.and(HtmxResponse)instead. Will be removed in 4.0.- Parameters:
otherResponse- Another HtmxResponse that will be merged into this response.- Returns:
- this for chaining
-
getHeaderReswap
Deprecated.usegetReswap()instead. Will be removed in 4.0. -
getTemplates
Deprecated.usegetViews()instead. Will be removed in 4.0. -
getLocation
Deprecated.Replaced byHtmxLocationRedirectView. Will be removed in 4.0. -
getPushUrl
-
getRedirect
Deprecated.Replaced byHtmxRedirectView. Will be removed in 4.0. -
getReplaceUrl
-
getReselect
-
getReswap
-
getRetarget
-
getTriggers
-
getTriggersAfterSettle
-
getTriggersAfterSwap
-
getViews
Deprecated.Replaced byHtmxView. Will be removed in 4.0. -
isRefresh
Deprecated.Replaced byHtmxRefreshView. Will be removed in 4.0. -
isContextRelative
public boolean isContextRelative()
-
HtmxResponseas handler method argument andHtmxView,HtmxRedirectVieworHtmxLocationRedirectViewas handler method return type instead.