Class HtmxResponse

java.lang.Object
io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxResponse

public final class HtmxResponse extends Object
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:
  • Constructor Details

    • HtmxResponse

      public HtmxResponse()
      Create a new HtmxResponse.
  • Method Details

    • builder

      @Deprecated public static HtmxResponse.Builder builder()
      Deprecated.
      use HtmxResponse as handler method argument and HtmxView, HtmxRedirectView or HtmxLocationRedirectView as handler method return type instead. Will be removed in 4.0.
      Return a builder to build a HtmxResponse.
      Returns:
      the builder
    • addTrigger

      @Deprecated public HtmxResponse addTrigger(String eventName)
      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

      public void addTrigger(String eventName, Object eventDetail)
      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
      eventDetail - details along with the event
      Since:
      3.6.0
      See Also:
    • addTriggerAfterSettle

      public void addTriggerAfterSettle(String eventName)
      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

      public void addTriggerAfterSettle(String eventName, Object eventDetail)
      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
      eventDetail - details along with the event
      Since:
      3.6.0
      See Also:
    • addTriggerAfterSwap

      public void addTriggerAfterSwap(String eventName)
      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

      public void addTriggerAfterSwap(String eventName, Object eventDetail)
      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
      eventDetail - 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:
    • 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

      public void setPushUrl(String url)
      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:
    • setReplaceUrl

      public void setReplaceUrl(String url)
      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

      public void setReselect(String cssSelector)
      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

      public void setReswap(HtmxReswap reswap)
      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

      public void setRetarget(String cssSelector)
      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 public HtmxResponse addTemplate(String template)
      Deprecated.
      use HtmxResponse.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 public HtmxResponse addTemplate(org.springframework.web.servlet.View template)
      Deprecated.
      use HtmxResponse.Builder.view(View) instead. Will be removed in 4.0.
      Append the rendered template or fragment as a resolved View.
      Parameters:
      template - must not be null.
      Returns:
      same HtmxResponse for chaining
    • addTemplate

      @Deprecated public HtmxResponse addTemplate(org.springframework.web.servlet.ModelAndView template)
      Deprecated.
      use HtmxResponse.Builder.view(ModelAndView) instead. Will be removed in 4.0.
      Append the rendered template or fragment as a ModelAndView.
      Parameters:
      template - must not be null.
      Returns:
      same HtmxResponse for chaining
    • addTrigger

      @Deprecated public HtmxResponse addTrigger(String eventName, String eventDetail, HxTriggerLifecycle step)
      Deprecated.
      use HtmxResponse.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 public HtmxResponse pushHistory(String url)
      Deprecated.
      use HtmxResponse.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:
    • browserRedirect

      @Deprecated public HtmxResponse browserRedirect(String url)
      Deprecated.
      use HtmxResponse.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 public HtmxResponse browserRefresh(boolean refresh)
      Deprecated.
      use HtmxResponse.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 public HtmxResponse retarget(String cssSelector)
      Deprecated.
      use HtmxResponse.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 public HtmxResponse reswap(HxSwapType swapType)
      Deprecated.
      use reswap(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 public HtmxResponse and(HtmxResponse otherResponse)
      Deprecated.
      use HtmxResponse.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 public String getHeaderReswap()
      Deprecated.
      use getReswap() instead. Will be removed in 4.0.
    • getTemplates

      @Deprecated public Collection<org.springframework.web.servlet.ModelAndView> getTemplates()
      Deprecated.
      use getViews() instead. Will be removed in 4.0.
    • getLocation

      @Deprecated public HtmxLocation getLocation()
      Deprecated.
      Replaced by HtmxLocationRedirectView. Will be removed in 4.0.
    • getPushUrl

      public String getPushUrl()
    • getRedirect

      @Deprecated public String getRedirect()
      Deprecated.
      Replaced by HtmxRedirectView. Will be removed in 4.0.
    • getReplaceUrl

      public String getReplaceUrl()
    • getReselect

      public String getReselect()
    • getReswap

      public HtmxReswap getReswap()
    • getRetarget

      public String getRetarget()
    • getTriggers

      public Map<String,String> getTriggers()
    • getTriggersAfterSettle

      public Map<String,String> getTriggersAfterSettle()
    • getTriggersAfterSwap

      public Map<String,String> getTriggersAfterSwap()
    • getViews

      @Deprecated public Collection<org.springframework.web.servlet.ModelAndView> getViews()
      Deprecated.
      Replaced by HtmxView. Will be removed in 4.0.
    • isRefresh

      @Deprecated public boolean isRefresh()
      Deprecated.
      Replaced by HtmxRefreshView. Will be removed in 4.0.
    • isContextRelative

      public boolean isContextRelative()