JSF Showcase
f:viewParam
f:viewParam lets JSF developer to process a parameter in the current view. As it behaves like f:param, it supports other interesting features like usingConverter
or Validator
.
General Usage
Click on the link to see how the viewParam populates the backing bean value converted throughCountryConverter
.
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"> <f:metadata> <f:viewParam name="countryId" value="#{viewMetadataBackingBean.viewCountry}"> <f:converter converterId="com.liferay.faces.showcase.converter.CountryConverter" /> </f:viewParam> </f:metadata> <h:form> <h:messages/> <h:link includeViewParams="true" value="#{i18n['link']}"> <f:param name="countryId" value="1" /> </h:link> <h:outputText value="#{viewMetadataBackingBean.viewCountry.countryName}" /> </h:form> </ui:composition>
@ManagedBean @RequestScoped public class ViewMetadataBackingBean { private String viewActionText; private Country viewCountry; public String getViewActionText() { return viewActionText; } public Country getViewCountry() { return viewCountry; } public void setViewActionText(String viewActionText) { this.viewActionText = viewActionText; } public void setViewCountry(Country viewCountry) { this.viewCountry = viewCountry; } public String viewAction() { PhaseId phaseId = FacesContext.getCurrentInstance().getCurrentPhaseId(); String phaseName = phaseId.toString(); this.viewActionText = "View action was executed in phase " + phaseName; return null; } }
@FacesConverter(value = "com.liferay.faces.showcase.converter.CountryConverter") public class CountryConverter 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) { Object countryObject = null; if ((value != null) && !"".equals(value)) { Long countryId = Long.parseLong(value); countryObject = getCountryMap(facesContext).get(countryId); } return countryObject; } @Override public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) { String strValue = ""; if (value != null) { Country country = (Country) value; strValue = Long.toString(country.getCountryId()); } return strValue; } protected Map<Long, Country> getCountryMap(FacesContext facesContext) { Map<Long, Country> countryMap = CountryConverter.countryMap; // First check without locking (not yet thread-safe) if (countryMap == null) { synchronized (CountryConverter.class) { countryMap = CountryConverter.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 = CountryConverter.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