JSF Showcase

f:param

f:param Facelet sends a named parameter value if used in any commandButton, commandLink, outputLink or button. These parameters will be sent as form parameters when doing the POST/GET requests. In order to get them, you can use ExternalContext getRequestParameterMap method for getting the sent value.

It's important to notice the differences between f:param and f:attribute:
  • f:attribute stores the key-values in the component itself, so, in order to get those attributes, the reference to the UIComponent should be available. For getting the f:param values is enough to use the getRequestParameterMap method mentioned before.
  • f:param should be used for sending String parameter values. If it's intented to use Object, then use f:attribute instead

General Usage

Source Code

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

	<h:form>
		<h:messages id="feedback" globalOnly="true" layout="table" />
		<h:commandLink actionListener="#{commandBackingBean.parameterActionListener}" value="#{i18n['link']}">
			<f:ajax execute="@form" render="feedback" />
			<f:param name="parameter" value="12345"/>
		</h:commandLink>
	</h:form>

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

	private static final Logger logger = LoggerFactory.getLogger(CommandBackingBean.class);

	// Injections
	@ManagedProperty(value = "#{commandModelBean}")
	private CommandModelBean commandModelBean;

	public void actionListener(ActionEvent actionEvent) {

		FacesContext facesContext = FacesContext.getCurrentInstance();
		PhaseId phaseId = facesContext.getCurrentPhaseId();
		logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());

		String phaseName = phaseId.toString();
		FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
				" phase of the JSF lifecycle.");
		facesContext.addMessage(null, facesMessage);
	}

	public void ajaxListener(AjaxBehaviorEvent ajaxBehaviorEvent) {

		FacesContext facesContext = FacesContext.getCurrentInstance();
		PhaseId phaseId = facesContext.getCurrentPhaseId();
		logger.debug("ajaxListener: phaseId=[{0}]", phaseId.toString());

		String phaseName = phaseId.toString();
		FacesMessage facesMessage = new FacesMessage("The ajaxListener method was called during the " + phaseName +
				" phase of the JSF lifecycle.");
		facesContext.addMessage(null, facesMessage);
	}

	public void attributeActionListener(ActionEvent actionEvent) {

		FacesContext facesContext = FacesContext.getCurrentInstance();
		PhaseId phaseId = facesContext.getCurrentPhaseId();
		logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());

		String value = (String) actionEvent.getComponent().getAttributes().get("attribute");

		String phaseName = phaseId.toString();
		FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
				" phase of the JSF lifecycle.The attribute value is " + value);
		facesContext.addMessage(null, facesMessage);
	}

	public void attributesActionListener(ActionEvent actionEvent) {

		FacesContext facesContext = FacesContext.getCurrentInstance();
		PhaseId phaseId = facesContext.getCurrentPhaseId();
		logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());

		Map<String, Object> attributes = actionEvent.getComponent().getAttributes();
		String value1 = (String) attributes.get("attribute1");
		String value2 = (String) attributes.get("attribute2");

		String phaseName = phaseId.toString();
		FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
				" phase of the JSF lifecycle.The attributes value are " + value1 + " and " + value2);
		facesContext.addMessage(null, facesMessage);
	}

	public void feedbackListener(ActionEvent actionEvent) {

		String value = "";

		List<UIComponent> children = actionEvent.getComponent().getChildren();

		for (UIComponent uiComponent : children) {

			if (uiComponent instanceof UIOutput) {
				value = (String) ((UIOutput) uiComponent).getValue();
			}
		}

		FacesContext facesContext = FacesContext.getCurrentInstance();
		logger.debug("feedbackListener: You selected the '" + value + "' menu item.");

		FacesMessage facesMessage = new FacesMessage("You selected the '" + value + "' menu item.");
		facesContext.addMessage(null, facesMessage);
	}

	public Map<String, Object> getAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("attribute1", "value1");
		attributes.put("attribute2", "value2");

		return attributes;
	}

	public void parameterActionListener() {

		FacesContext facesContext = FacesContext.getCurrentInstance();
		PhaseId phaseId = facesContext.getCurrentPhaseId();
		logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());

		Map<String, String> requestParameterMap = facesContext.getExternalContext().getRequestParameterMap();
		String value = requestParameterMap.get("parameter");

		String phaseName = phaseId.toString();
		FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
				" phase of the JSF lifecycle.The parameter value is " + value);
		facesContext.addMessage(null, facesMessage);
	}

	public void selectionListener(ActionEvent actionEvent) {

		UICommand uiCommand = (UICommand) actionEvent.getComponent();
		Customer customer = (Customer) uiCommand.getValue();
		commandModelBean.setSelectedCustomer(customer);
	}

	public void setCommandModelBean(CommandModelBean commandModelBean) {
		this.commandModelBean = commandModelBean;
	}
}
Liferay Faces Bridge Implementation 4.1.0 + Showcase Common 3.0.1 + Liferay Faces Util 3.1.0 + Mojarra 2.2.14