Is it possible to call a method defined in method-signature programmatically? - jsf

Suppose I have a composite component
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:composite="http://java.sun.com/jsf/composite"
`enter code here` xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<composite:interface componentType="editableLabel">
<composite:attribute name="value" required="true" type="java.lang.String"/>
<composite:attribute name="oldValue" type="java.lang.String"/>
<composite:attribute name="editMode" required="false" default="#{false}" type="java.lang.Boolean"/>
<composite:attribute name="updateListener" required="false" method-signature="void actionListener()"/>
<composite:attribute name="cancelListener" required="false" method-signature="void actionListener()"/>
</composite:interface>
<composite:implementation>
<h:panelGroup id="editableLabelComponent">
<h:panelGroup rendered="#{cc.attrs.editMode}">
<p:inputText value="#{cc.attrs.value}"/>
<p:commandButton value="Update" actionListener="#{cc.update}" update="editableLabelComponent"/>
<p:commandButton value="Cancel" actionListener="#{cc.cancel}" update="editableLabelComponent"/>
</h:panelGroup>
<p:commandLink>
<p:outputLabel id="display" value="#{cc.attrs.value}" rendered="#{!cc.attrs.editMode}"/>
<p:ajax event="click" listener="#{cc.toggleEditMode}" update="editableLabelComponent"/>
</p:commandLink>
</h:panelGroup>
</composite:implementation>
</h:body>
</html>
with a FacesComponent
import org.apache.commons.lang3.StringUtils;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.component.FacesComponent;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIComponentBase;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import java.io.Serializable;
/**
* Created by labraham on 1/14/16.
*/
#FacesComponent(value = "editableLabel")
public class EditableLabel extends UIComponentBase implements Serializable, NamingContainer {
private static final long serialVersionUID = 108467781935083432L;
public static final String VALUE_ATTRIBUTE = "#{cc.attrs.value}";
public static final String OLD_VALUE_ATTRIBUTE = "#{cc.attrs.oldValue}";
public static final String EDIT_MODE_ATTRIBUTE = "#{cc.attrs.editMode}";
private FacesContext context;
private ELContext elContext;
#Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
/**
*
*/
public void update() {
ValueExpression oldValue = getAttr(OLD_VALUE_ATTRIBUTE, String.class);
String value = (String) getAttr(VALUE_ATTRIBUTE, String.class).getValue(getElContext());
oldValue.setValue(getElContext(), value);
toggleEditMode();
}
/**
*
*/
public void cancel() {
ValueExpression value = getAttr(VALUE_ATTRIBUTE, String.class);
String oldValue = (String) getAttr(OLD_VALUE_ATTRIBUTE, String.class).getValue(getElContext());
value.setValue(getElContext(), oldValue);
toggleEditMode();
}
/**
*
*/
public void toggleEditMode() {
if (StringUtils.isEmpty((String) getAttr(OLD_VALUE_ATTRIBUTE, String.class).getValue(getElContext()))) {
getAttr(OLD_VALUE_ATTRIBUTE, String.class).setValue(getElContext(),
getAttr(VALUE_ATTRIBUTE, String.class).getValue(getElContext())
);
}
ValueExpression editmode = getAttr(EDIT_MODE_ATTRIBUTE, Boolean.class);
editmode.setValue(getElContext(), !((Boolean) editmode.getValue(getElContext())));
}
/**Get value expression for a given attr.*/
private ValueExpression getAttr(final String attribute, final Class<?> attributeType) {
return getContext().getApplication().getExpressionFactory()
.createValueExpression(getElContext(), attribute, attributeType);
}
/**Get ElContext.*/
public ELContext getElContext() {
if (this.elContext == null) {
elContext = getContext().getELContext();
}
return elContext;
}
public void setElContext(final ELContext elContext) {
this.elContext = elContext;
}
/**Get faces context*/
public FacesContext getContext() {
if (this.context == null) {
context = FacesContext.getCurrentInstance();
}
return context;
}
public void setContext(final FacesContext context) {
this.context = context;
}
}
now all instances of this widget will have the baseline functionality for cancel and update as defined by the cancel() and update() functions. However in addition this basic functionality the user may want some additional functionality (for instance calling a webservice on update) which they will add by some sort of backing bean method in the updateListener and/or cancelListener attributes.
My question is Is it possible to call the method that the user attached to the listener attributes programmatically from inside the FacesComponent or am I going about this the wrong way?

Yes, it's available as a MethodExpression typed attribute. All composite attributes are available the usual way by the inherited UIComponent#getAttributes() map. See also How to access Composite Component attribute values in the backing UIComponent?
E.g., to get #{cc.attrs.updateListener} and invoke with no params:
MethodExpression updateListener = (MethodExpression) getAttributes().get("updateListener");
updateListener.invoke(getFacesContext().getELContext(), null);
Unrelated to the concrete problem, the way how you deal with ValueExpression attributes is clumsy. To get #{cc.attrs.oldvalue}, just do:
String oldvalue = (String) getAttributes().get("oldvalue");
To set #{cc.attrs.value}, just do:
getAttributes().put("value", oldvalue);
Having FacesContext and ELContext as instance variables does technically fortunately not harm in this specific construct, but this is fishy, you'd better get them in method local scope. The FacesContext is just available by inherited UIComponent#getFacesContext().
See also:
How can I create a JSF composite component that allows me to read/write to the attributes?
java.lang.IllegalStateException at com.sun.faces.context.FacesContextImpl.assertNotReleased

Related

JSF can't get a renderer for a custom composite input component

I'm building my custom JSF composite component in order to input periodicities. I've got this model:
public class Periodicity implements Serializable {
private PeriodicityType type;
private Integer value = 0;
public PeriodicityExchange() {
super();
}
/**Getter and setters**/
}
Where PeriodicityType is an enum containing DAY, WEEK, MONTH, YEAR.
The input component for this model consists of a p:spinner element to select a numeric value and a h:selectOneMenu to select the periodicity type. The component works well, but now I want to have a java class backing it, so I have modified my facelet file:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:composite="http://xmlns.jcp.org/jsf/composite"
xmlns:p="http://primefaces.org/ui">
<composite:interface componentType="periodicityInput">
<composite:attribute name="value" type="model.Periodicity" />
</composite:interface>
<composite:implementation>
<div class="col-xs-2">
<p:spinner binding="#{cc.periodicityValue}"
value="#{cc.attrs.value.value}" min="0" />
</div>
<div class="col-xs-3">
<h:selectOneMenu binding="#{cc.periodicityType}"
value="#{cc.attrs.value.type}" styleClass="form-control">
<f:selectItem itemLabel="No periodicity" noSelectionOption="true" />
<f:selectItem itemLabel="Days" itemValue="DAY" />
<f:selectItem itemLabel="Weeks" itemValue="WEEK" />
<f:selectItem itemLabel="Months" itemValue="MONTH" />
<f:selectItem itemLabel="Years" itemValue="YEAR" />
</h:selectOneMenu>
</div>
</composite:implementation>
</html>
And that's my java class (based in this BalusC's tutorial). It's registered as a component using the faces-config.xml file:
public class PeriodicityInput extends UIInput implements NamingContainer {
private UIInput periodicityValue;
private UIInput periodicityType;
#Override
public void encodeBegin(FacesContext context) throws IOException {
super.encodeBegin(context);
}
#Override
protected Object getConvertedValue(FacesContext context, Object newSubmittedValue)
throws ConverterException {
try {
return new PeriodicityExchange(
Integer.parseInt(newSubmittedValue.toString().split("-")[0]),
newSubmittedValue.toString().split("-")[1]);
} catch (Exception ex) {
throw new ConverterException(ex);
}
}
public String getFamily() {
return COMPONENT_FAMILY;
}
public UIInput getPeriodicityType() {
return periodicityType;
}
public UIInput getPeriodicityValue() {
return periodicityValue;
}
#Override
public Object getSubmittedValue() {
return periodicityValue.getSubmittedValue() + "-" + periodicityType.getSubmittedValue();
}
public void setPeriodicityType(UIInput periodicityType) {
this.periodicityType = periodicityType;
}
public void setPeriodicityValue(UIInput periodicityValue) {
this.periodicityValue = periodicityValue;
}
}
The issue is that as soon as I use the componentType attribute to bind my component to the java class, the component stops rendering. encodeBegin gets called, but doing some further debugging, I've noticed that when JSF tries to get the renderer for the composite component, it returns null. The method getRenderer(FacesContext) from the UIComponentBase class tries to access context.getRenderKit().getRenderer(getFamily(), rendererType), where the family evaluates to javax.faces.Input and rendererType to javax.faces.Composite.
I'm using Mojarra 2.2.12. What am I missing here?
where the family evaluates to javax.faces.Input
This is not correct. It's supposed to be javax.faces.NamingContainer, the constant value of UINamingContainer.COMPONENT_FAMILY.
The COMPONENT_FAMILY below is likely messed up during refactoring with static imports:
#Override
public String getFamily() {
return COMPONENT_FAMILY;
}
The above is actually inherited from UIInput while it has to be UINamingContainer. Better specify it with FQN.
#Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}

How can I create a JSF composite component that binds a Collection to both an h:selectOneMenu and h:selectManyListbox?

I'm trying to create a composite component that will allow the user to toggle between a h:singleSelectMenu and h:selectManyListbox. I have it working sort of. It works as long as the value field points to a Collection...it does NOT work if the value field is null.
singleMultiSelect.xhtml
<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE html>
<ui:composition
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:ace="http://www.icefaces.org/icefaces/components"
>
<cc:interface componentType="singleMultiSelect">
<!-- The initial list of objects -->
<cc:attribute name="list" type="java.util.List" required="true"/>
<!-- The selected objects -->
<cc:attribute name="selected" type="java.util.Collection" required="true"/>
<!-- whether to display the selectOneMenu (true) or selectManyBox (false) -->
<cc:attribute name="singleSelect" type="java.lang.Boolean"
required="false" default="true"/>
</cc:interface>
<cc:implementation>
<span id="#{cc.clientId}">
<ace:checkboxButton id="singleSelectChkBx"
value="#{cc.attrs.singleSelect}">
<ace:ajax render="#{cc.clientId}"/>
</ace:checkboxButton>
<h:selectOneMenu id="selectOneMenu"
rendered="#{cc.attrs.singleSelect}"
value="#{cc.singleSelected}">
<f:selectItems value="#{cc.attrs.list}"/>
</h:selectOneMenu>
<h:selectManyListbox id="selectManyListbox"
rendered="#{! cc.attrs.singleSelect}"
value="#{cc.attrs.selected}">
<f:selectItems value="#{cc.attrs.list}"/>
</h:selectManyListbox>
</span>
</cc:implementation>
</ui:composition>
SingleMultiSelect.java
public class SingleMultiSelect extends UINamingContainer {
public SingleMultiSelect() {
super();
}
/**
* Converts the Object selected within the selectOneMenu to the list
* used by the component.
*
* #param singleSelected
*/
public void setSingleSelected(Object singleSelected) {
getSelected().clear();
if(singleSelected != null) {
getSelected().add(singleSelected);
}
}
/**
* Converts the collection used by the component to a single
* Object selected within the selectOneMenu.
*
* #return
*/
public Object getSingleSelected() {
return getSelected().size() > 0 ? getSelected().iterator().next() : null;
}
private Collection getSelected() {
return (Collection) getAttributes().get("selected");
}
}
I tried writing to the attributes map but that didn't work
public void setSingleSelected(Object singleSelected) {
HashSet selected = new HashSet();
selected.add(singleSelected);
((Collection) getAttributes()).put("selected", selected);
}
getAttributes().put("selected", selected);
You're basically overriding the underlying ValueExpression object #{cc.attrs.selected} with a HashSet. In other words, the EL expression became a "hardcoded" value and can't reach the bean property anymore.
You should be obtaining the ValueExpression and invoke the setter via setValue() call.
getValueExpression("selected").setValue(context.getElContext(), selected);
Unrelated to the concrete problem, when implementing a backing component, it's better to think like an UI component, not a backing bean. Don't manipulate the model directly (the composite attributes), but the composite component's own components which you reach via binding. Moreover, do not touch the getters/setters. Replace the <h:selectOneMenu value> as below:
<h:selectOneMenu binding="#{cc.singleSelected}" ...>
private UISelectOne singleSelected; // +getter+setter
#Override
public void processUpdates(FacesContext context) {
super.processUpdates(context);
if (getAttributes().get("singleSelect") == Boolean.TRUE) {
HashSet selected = new HashSet();
if (singleSelected.getValue() != null) {
selected.add(singleSelected.getValue());
}
getValueExpression("selected").setValue(context.getELContext(), selected);
}
}
#Override
public void encodeBegin(FacesContext context) throws IOException {
if (getAttributes().get("singleSelect") == Boolean.TRUE) {
Collection selected = (Collection) getAttributes().get("selected");
if (selected != null && !selected.isEmpty()) {
singleSelected.setValue(selected.iterator().next());
} else {
singleSelected.setValue(null);
}
}
super.encodeBegin(context);
}

How do I pass an attribute from composite component to backing bean by using backing component?

I have the following code in my facelet page:
<hc:rangeChooser1 id="range_chooser"
from="#{testBean.from}"
to="#{testBean.to}"
listener="#{testBean.update}"
text="#{testBean.text}">
<f:ajax event="rangeSelected"
execute="#this"
listener="#{testBean.update}"
render=":form:growl range_chooser"/>
</hc:rangeChooser1>
This is my composite component:
<ui:component xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:p="http://primefaces.org/ui">
<cc:interface componentType="rangeChooser">
<!-- Define component attributes here -->
<cc:clientBehavior name="rangeSelected" event="change" targets="hiddenValue"/>
<cc:attribute name="from" type="java.util.Calendar"/>
<cc:attribute name="to" type="java.util.Calendar"/>
<cc:attribute name="text" type="java.lang.String"/>
</cc:interface>
<cc:implementation>
<div id="#{cc.clientId}">
...
<p:inputText id="hiddenValue" value="#{cc.attrs.text}"/>
...
</div>
</cc:implementation>
</ui:component>
How do I pass attributes from, to and text from composite component to backing bean? I mean inject these values in backing component, and not through
<p:inputText id="hiddenValue" value="#{cc.attrs.text}"/>
Update: there's more correct definition what do I need: Be able to mutate objects which I pass from the backing bean to the composite component inside a backing component of the composite component. So when I perform process or execute my composite component I get the updated values.
This is my backing component:
#FacesComponent("rangeChooser")
public class RangeChooser extends UIInput implements NamingContainer {
private String text;
private Calendar from;
private Calendar to;
#Override
public void encodeBegin(FacesContext context) throws IOException{
super.encodeBegin(context);
}
public String getText() {
String text = (String)getStateHelper().get(PropertyKeys.text);
return text;
}
public void setText(String text) {
getStateHelper().put(PropertyKeys.text, text);
}
/*
same getters and setters for Calendar objects, from and to
*/
}
I just can't realize how do I move on? In general I need to take a value from <p:inputText id="hiddenValue" value="#{cc.attrs.text}"/> and convert it to two Calendars object from and to.
It will be great if somebody can point me toward right direction from here. I know that I need to use getAttributes().put(key,value) but don't know where to put this code. Thank you in advance.
Based on your comments this is what you expect.
Note that even if this implementation works, it is conceptually incorrect!
You are considering from and to as INPUTS (not input components, but input values) and text as OUTPUT. This is not the way JSF is intended to work!
However, here it is
<cc:interface componentType="rangeComponent">
<cc:attribute name="from" />
<cc:attribute name="to" />
<cc:clientBehavior name="rangeSelected" event="dateSelect" targets="from to"/>
</cc:interface>
<cc:implementation>
<div id="#{cc.clientId}">
<p:calendar id="from" value="#{cc.attrs.from}"/>
<p:calendar id="to" value="#{cc.attrs.to}"/>
</div>
</cc:implementation>
used in page:
<h:form>
<e:inputRange from="#{rangeBean.from}" to="#{rangeBean.to}" text="#{rangeBean.text}">
<p:ajax event="rangeSelected" process="#namingcontainer" update="#form:output" listener="#{rangeBean.onChange}" />
</e:inputRange>
<h:panelGrid id="output" columns="1">
<h:outputText value="#{rangeBean.from}"/>
<h:outputText value="#{rangeBean.to}"/>
<h:outputText value="#{rangeBean.text}"/>
</h:panelGrid>
</h:form>
with this backing component:
#FacesComponent("rangeComponent")
public class RangeComponent extends UINamingContainer
{
#Override
public void processUpdates(FacesContext context)
{
Objects.requireNonNull(context);
if(!isRendered())
{
return;
}
super.processUpdates(context);
try
{
Date from = (Date) getValueExpression("from").getValue(context.getELContext());
Date to = (Date) getValueExpression("to").getValue(context.getELContext());
ValueExpression ve = getValueExpression("text");
if(ve != null)
{
ve.setValue(context.getELContext(), from + " - " + to);
}
}
catch(RuntimeException e)
{
context.renderResponse();
throw e;
}
}
}
with this backing bean:
#ManagedBean
#ViewScoped
public class RangeBean implements Serializable
{
private static final long serialVersionUID = 1L;
private Date from = new Date(1000000000);
private Date to = new Date(2000000000);
private String text = "range not set";
public void onChange(SelectEvent event)
{
Messages.addGlobalInfo("[{0}] changed: [{1}]", event.getComponent().getId(), event.getObject());
}
// getters/setters
}
I rewrote the code using BalusC tecnique (and without PrimeFaces):
the form:
<h:form>
<e:inputRange value="#{rangeBean.range}">
<p:ajax event="change" process="#namingcontainer" update="#form:output"
listener="#{rangeBean.onChange}" />
</e:inputRange>
<h:panelGrid id="output" columns="1">
<h:outputText value="#{rangeBean.range}" />
</h:panelGrid>
</h:form>
the composite:
<cc:interface componentType="rangeComponent">
<cc:attribute name="value" />
<cc:clientBehavior name="change" event="change" targets="from to"/>
</cc:interface>
<cc:implementation>
<div id="#{cc.clientId}">
<h:inputText id="from" binding="#{cc.from}">
<f:convertDateTime type="date" pattern="dd/MM/yyyy" />
</h:inputText>
<h:inputText id="to" binding="#{cc.to}">
<f:convertDateTime type="date" pattern="dd/MM/yyyy" />
</h:inputText>
</div>
</cc:implementation>
the backing component:
#FacesComponent("rangeComponent")
public class RangeComponent extends UIInput implements NamingContainer
{
private UIInput from;
private UIInput to;
#Override
public String getFamily()
{
return UINamingContainer.COMPONENT_FAMILY;
}
#Override
public void encodeBegin(FacesContext context) throws IOException
{
String value = (String) getValue();
if(value != null)
{
String fromString = StringUtils.substringBefore(value, "-");
String toString = StringUtils.substringAfter(value, "-");
try
{
from.setValue(from.getConverter().getAsObject(context, from, fromString));
}
catch(Exception e)
{
from.setValue(new Date());
}
try
{
to.setValue(to.getConverter().getAsObject(context, to, toString));
}
catch(Exception e)
{
to.setValue(new Date());
}
}
super.encodeBegin(context);
}
#Override
public Object getSubmittedValue()
{
return (from.isLocalValueSet() ? from.getValue() : from.getSubmittedValue()) + "-" + (to.isLocalValueSet() ? to.getValue() : to.getSubmittedValue());
}
#Override
protected Object getConvertedValue(FacesContext context, Object submittedValue)
{
return from.getSubmittedValue() + "-" + to.getSubmittedValue();
}
public UIInput getFrom()
{
return from;
}
public void setFrom(UIInput from)
{
this.from = from;
}
public UIInput getTo()
{
return to;
}
public void setTo(UIInput to)
{
this.to = to;
}
}
and the managed bean:
#ManagedBean
#ViewScoped
public class RangeBean implements Serializable
{
private static final long serialVersionUID = 1L;
private String range = "01/01/2015-31/12/2015";
public void onChange(AjaxBehaviorEvent event)
{
Messages.addGlobalInfo("[{0}] changed: [{1}]", event.getComponent().getId(), event.getBehavior());
}
public String getRange()
{
return range;
}
public void setRange(String range)
{
this.range = range;
}
}
Note that managed bean only retains range property for get/set. From and to are gone, the backing component derives and rebuilds them itself.

Switch validator on component

How can i switch my validator in a component dependence on a boolean.
I have a selectBooleanCheckbox and when its true i want use FirstValidator else i want use SecondValidator. I found nothing about this "special" case.
Nothing special only for example code:
Xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
</h:head>
<h:body>
<h:form>
<h:selectBooleanCheckbox value="#{testBean.firstValidator}"/>
<h:inputText>
<f:validator validatorId="FirstValidator" />
</h:inputText>
<h:commandButton value="Test" />
<h:messages />
</h:form>
</h:body>
</html>
Bean:
#ManagedBean
#ViewScoped
public class TestBean implements Serializable{
private boolean firstValidator;
public boolean isfirstValidator() {
return firstValidator;
}
public void setfirstValidator(boolean firstValidator) {
this.firstValidator = firstValidator;
}
}
Validator1:
#FacesValidator("FirstValidator")
public class FirstValidator implements Validator{
#Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
String valueAsString = (String) value;
if(valueAsString.contains("a")){
throw new ValidatorException(new FacesMessage("Fail!"));
}
}
}
Validator2:
#FacesValidator("SecondValidator")
public class SecondValidator implements Validator{
#Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
String valueAsString = (String) value;
if(valueAsString.contains("b")){
throw new ValidatorException(new FacesMessage("Fail2!"));
}
}
}
Just set/disable the validator depending on checkbox's value. You only need to make sure that you pick the checked value as available during view build time (which runs during restore view phase). So you definitely can't use the model value #{testBean.firstValidator} for this (which is only set during update model values phase). You'd need to determine the HTTP request parameter instead. It's empty if the checkbox is unchecked, otherwise it's not empty.
First bind the checkbox component to the view (not to a bean!) via binding attribute:
<h:selectBooleanCheckbox binding="#{checkbox}" ... />
This way the request parameter can be dynamically obtained as #{param[checkbox.clientId]}.
Now you can use either conditional setting of validator ID:
<f:validator validatorId="#{empty param[checkbox.clientId] ? 'firstValidator' : 'secondValidator'}" />
Or conditional setting the validator's disabled attribute:
<f:validator validatorId="firstValidator" disabled="#{not empty param[checkbox.clientId]}" />
<f:validator validatorId="secondValidator" disabled="#{empty param[checkbox.clientId]}" />
Note that I altered the validator IDs as per Java instance variable naming conventions. You also don't do as follows in normal Java code, right?
Validator FirstValidator = new FirstValidator();

Issue when converting GET request parameters on viewParam

I'm trying to convert GET request parameters passed from another view like this:
<f:metadata>
<f:viewParam name="id"
value="#{targetViewBean.fooFromSourceView}"
converter="fooConverter"
converterMessage="Foo converter message"
required="true" requiredMessage="Foo required message"/>
<f:viewAction action="#{targetViewBean.doSomethingWithFoo()}"/>
</f:metadata>
But only the Converter.getAsString(..., Object value) method is called and value is always null, even thou the GET parameter is really sent.
I found BalusC blog post about this and, AFAIK, I followed it to the letter. Still no good. Here's the full code:
Source view
<h:head>
<title>Source view</title>
</h:head>
<h:body>
<ul>
<ui:repeat value="#{sourceViewBean.foos}" var="foo">
<li>
<h:link value="Foo \##{foo.id}" outcome="target-view">
<f:param name="id" value="#{foo.id}" />
</h:link>
</li>
</ui:repeat>
</ul>
</h:body>
Backing bean
#Named #ViewScoped
public class SourceViewBean implements Serializable {
public Collection<Foo> getFoos() {
return Db.INSTANCE.getFoos();
}
private static final long serialVersionUID = 1L;
}
Target view
<f:metadata>
<f:viewParam name="id"
value="#{targetViewBean.fooFromSourceView}"
converter="fooConverter"
converterMessage="Foo converter message"
required="true" requiredMessage="Foo required message"/>
<f:viewAction action="#{targetViewBean.doSomethingWithFoo()}"/>
</f:metadata>
<h:head>
<title>Target view</title>
</h:head>
<h:body>
<h:outputText value="ID: #{targetViewBean.fooFromSourceView.id}" />
</h:body>
Target view backing bean
#Named
#ViewScoped
public class TargetViewBean implements Serializable {
private Foo fooFromSourceView;
public void doSomethingWithFoo() {
System.out.println("Foo is here? " + fooFromSourceView != null);
}
public Foo getFooFromSourceView() {
return fooFromSourceView;
}
public void setFooFromSourceView(Foo fooFromSourceView) {
this.fooFromSourceView = fooFromSourceView;
}
private static final long serialVersionUID = 1L;
}
The converter
#FacesConverter(value = "fooConverter")
public class FooConverter implements Converter {
#Override
public Object getAsObject(
FacesContext context, UIComponent component, String value) {
if (value == null || !value.matches("\\d+")) {
return null;
}
for (Foo foo : Db.INSTANCE.getFoos()) {
if (foo.getId().equals(Integer.parseInt(value))) {
return foo;
}
}
throw new ConverterException(new FacesMessage("No Foo found!"));
}
#Override
public String getAsString(
FacesContext context, UIComponent component, Object value) {
if (!(value instanceof Foo) || ((Foo) value).getId() == null) {
return null;
}
return ((Foo) value).getId().toString();
}
}
I was able to find the problem after taking a look at the actual code you sent. The issue is not with the converter. It's with the xml namespaces at the top of your project. For instance, in source-view.xml you have
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
But they should be
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
And target-view.xhtml should be
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
GlassFish seems to change the namespaces for some reason. I didn't try to find out why it behaves like that though so keep that in mind. Anyway, once I changed it, the correct phases were being outputted in GlassFish's output window. So go and make the necessary change where needed.
Note: In case you are wondering why you are getting the following error
The metadata component needs to be nested within a f:metadata tag. Suggestion: enclose the necessary components within <f:metadata>
This seems to be a reported issue with JSF 2.2
Also, I'm not sure why your h:link is nested inside an h:form. It's not needed.
UPDATE
Seems like some of the taglibs are not fully functional or am I reading this wrong ?
https://java.net/jira/browse/JAVASERVERFACES-2868

Resources