Class A<T extends A<T>>

Direct Known Subclasses:
Anchor, Hyperlink, SkipLink

public class A<T extends A<T>> extends StyledContainerElement<T>
HTML <a> element for hyperlinks.

This class represents an anchor element that creates hyperlinks to other web pages, files, locations within the same page, email addresses, or any other URL. Supports various HTML5 link attributes including target, download, and relationship specifications.

Features:

  • Create hyperlinks to URLs
  • Target specification (_blank, _self, etc.)
  • Download attribute for file downloads
  • Language and media query support
  • Relationship (rel) attribute for link semantics
  • Multiple constructors for convenient initialization

Usage:

 A link = new A("https://example.com", "Visit Example");
 link.setTarget(Target.BLANK);

 A download = new A("/files/document.pdf");
 download.setDownload("document.pdf");
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • A

      public A()
      Creates a new anchor element with no href.
    • A

      public A(String href)
      Creates a new anchor element with the specified URL.
      Parameters:
      href - The URL that the hyperlink points to.
    • A

      public A(String href, String text)
      Creates a new anchor element with the specified URL and display text.
      Parameters:
      href - The URL that the hyperlink points to.
      text - The display text for the link.
    • A

      public A(String href, String target, String text)
      Creates a new anchor element with the specified URL, target, and display text.
      Parameters:
      href - The URL that the hyperlink points to.
      target - The browsing context in which to open the link (e.g., "_blank").
      text - The display text for the link.
    • A

      public A(String href, Target target, String text)
      Creates a new anchor element with the specified URL, target constant, and display text.
      Parameters:
      href - The URL that the hyperlink points to.
      target - The Target constant specifying where to open the link.
      text - The display text for the link.
  • Method Details

    • setDownload

      public final T setDownload(String filename)
      Sets the download attribute.

      Indicates that the linked resource should be downloaded rather than navigated to. The value specifies the suggested filename; an empty string uses the server's default filename.

      Parameters:
      filename - The filename for download, or empty string for default.
      Returns:
      This element for method chaining.
    • setHref

      public final T setHref(String url)
      Sets the URL that the hyperlink points to.

      If the URL starts with "/" (absolute path within the application), the servlet context path will be automatically prepended to ensure the link works correctly regardless of how the application is deployed.

      Parameters:
      url - The URL to link to.
      Returns:
      This element for method chaining.
    • setHrefLang

      public final T setHrefLang(String languageCode)
      Sets the hreflang attribute.

      Specifies the language of the linked resource as a BCP 47 language tag. This is advisory only and does not affect browser behavior.

      Parameters:
      languageCode - The language of the linked resource.
      Returns:
      This element for method chaining.
    • setMedia

      public final T setMedia(String mediaQuery)
      Sets the media attribute specifying the media query for which the hyperlink is optimized.

      Specifies the media/device the linked resource is designed for. The value is a media query that determines when the hyperlink is most relevant.

      Parameters:
      mediaQuery - The media query string.
      Returns:
      This element for method chaining.
    • setPing

      public final T setPing(String ping)
      Sets the ping attribute.

      Specifies a space-separated list of URLs to be notified when the link is followed. The browser sends a POST request to each URL for tracking purposes.

      Parameters:
      ping - Space-separated URLs to ping when link is followed.
      Returns:
      This element for method chaining.
    • setReferrerPolicy

      public final T setReferrerPolicy(String referrerPolicy)
      Sets the referrerpolicy attribute.

      Specifies how much referrer information to send when following this link. Common values include "no-referrer", "origin", and "strict-origin-when-cross-origin".

      Parameters:
      referrerPolicy - The referrer policy value.
      Returns:
      This element for method chaining.
    • setRel

      public final T setRel(Relationship relationship)
      Sets the rel attribute using the Relationship enum.
      Parameters:
      relationship - The link relationship.
      Returns:
      This element for method chaining.
    • setRel

      public final T setRel(String relationship)
      Sets the rel attribute.

      Specifies the relationship between the current document and the linked resource. Common values include "noopener", "noreferrer", "external", and "author".

      Parameters:
      relationship - The relationship of the linked resource.
      Returns:
      This element for method chaining.
    • setTarget

      public final T setTarget(Target target)
      Sets the target attribute using the Target enum.
      Parameters:
      target - The target browsing context.
      Returns:
      This element for method chaining.
    • setTarget

      public final T setTarget(String target)
      Sets the target attribute.

      Specifies the browsing context in which to display the linked resource. Values include "_self" (same context), "_blank" (new tab/window), "_parent" (parent context), and "_top" (top-level context).

      Parameters:
      target - The browsing context for navigation (_self, _blank, _parent, _top).
      Returns:
      This element for method chaining.
    • setType

      public final T setType(String mediaType)
      Sets the MIME type of the linked resource.

      Specifies the media type of the linked resource as a MIME type (e.g., "text/html", "application/pdf"). This is advisory and does not affect browser behavior.

      Parameters:
      mediaType - The MIME type of the linked resource.
      Returns:
      This element for method chaining.
    • getDownload

      protected String getDownload()
      Returns the download attribute value.
      Returns:
      The download filename, or null if not set.
    • getHref

      protected String getHref()
      Returns the href attribute value.
      Returns:
      The hyperlink URL, or null if not set.
    • getHrefLang

      protected String getHrefLang()
      Returns the hreflang attribute value.
      Returns:
      The linked resource language, or null if not set.
    • getPing

      protected String getPing()
      Returns the ping attribute value.
      Returns:
      The ping URLs, or null if not set.
    • getReferrerPolicy

      protected String getReferrerPolicy()
      Returns the referrerpolicy attribute value.
      Returns:
      The referrer policy, or null if not set.
    • getRel

      protected String getRel()
      Returns the rel attribute value.
      Returns:
      The relationship value, or null if not set.
    • getTarget

      protected String getTarget()
      Returns the target attribute value.
      Returns:
      The browsing context, or null if not set.