Note

  • h:outputlink is intended to be used to render a simple <a href="...">hyperlink</a> that requests a URL via HTTP GET.
  • h:link is intended to be used to render a hyperlink that navigates to other JSF views via HTTP GET, or perhaps to re-render the same view with different view parameters via f:param child tags.

h:outputLink

HtmlOutputLink is a UIOutput component that renders an <a> element (hyperlink).

Conversion Usage

The value attribute can be converted via the converter attribute or by specifying an f:converter type of child tag. In addition, custom user feedback can be specified via the converterMessage attribute.

Link to a map of China

Source Code

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

	<h:form>
		<h:outputLabel value="#{i18n['country']}" />
		<h:selectOneMenu id="selectOneMenuId" value="#{outputLinkModelBean.country}">
			<f:selectItems value="#{countryService.allCountries}" var="country" itemValue="#{country}"
				itemLabel="#{country.countryName}" />
			<f:ajax render="selectOneMenuId outputLinkId" />
			<f:converter converterId="com.liferay.faces.showcase.converter.CountryConverter" />
		</h:selectOneMenu>
		<br />
		<h:outputLink id="outputLinkId" converter="com.liferay.faces.showcase.converter.CountryGmapConverter"
			target="_blank" value="#{outputLinkModelBean.country.countryId}">
			<h:outputText value="#{i18n['link-to-a-map-of']} #{outputLinkModelBean.country.countryName}" />
		</h:outputLink>
	</h:form>

</ui:composition>
@ManagedBean
@RequestScoped
public class OutputLinkModelBean {

	@ManagedProperty(value = "#{countryService}")
	private CountryService countryService;

	// Private properties
	private Country country;

	public Country getCountry() {
		return country;
	}

	public CountryService getCountryService() {
		return countryService;
	}

	@PostConstruct
	public void postConstruct() {
		this.country = countryService.getAllCountries().get(0);
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public void setCountryService(CountryService countryService) {
		this.countryService = countryService;
	}
}
@FacesConverter(value = "com.liferay.faces.showcase.converter.CountryGmapConverter")
public class CountryGmapConverter implements Converter {

	// Static field must be declared volatile in order for the double-check idiom to work (requires JRE 1.5+)
	private static volatile Map<Long, Country> countryMap;

	@Override
	public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {

		// no-op
		return null;
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {

		Country country = getCountryMap(facesContext).get(value);

		String URL = "https://www.google.com/maps/place/";

		try {
			return URL + URLEncoder.encode(country.getCountryName(), "UTF-8");
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();

			return null;
		}
	}

	protected Map<Long, Country> getCountryMap(FacesContext facesContext) {

		Map<Long, Country> countryMap = CountryGmapConverter.countryMap;

		// First check without locking (not yet thread-safe)
		if (countryMap == null) {

			synchronized (CountryGmapConverter.class) {

				countryMap = CountryGmapConverter.countryMap;

				// Second check with locking (thread-safe)
				if (countryMap == null) {

					ELResolver elResolver = facesContext.getApplication().getELResolver();
					ELContext elContext = facesContext.getELContext();
					CountryService countryService = (CountryService) elResolver.getValue(elContext, null,
							"countryService");
					countryMap = CountryGmapConverter.countryMap = countryService.getCountryMap();
				}
			}
		}

		return countryMap;
	}
}
Liferay Faces Bridge Implementation 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20