How to pass an Object and the ActionEvent by h:commandbutton actionListener? - jsf-2.2

I can pass the ActionEvent from the JSF-page down to a method void clicked(ActionEvent event) by <h:commandButton ... actionListener="#{something.clicked}" ...>
I can also pass an Object down to a method void clicked(Object obj) by <h:commandButton ... actionListener="#{something.clicked(Object obj)}" ...>
But how do I pass both in a single call to a method like void clicked(ActionEvent event, Object obj) ?

I guess you are looking for
<f:setPropertyActionListener />
You put it inside your commandButton or commandLink:
<h:commandButton actionListener="#{something.clicked}">
<f:setPropertyActionListener target="#{something.obj}" value="#{obj}">
</h:commandButton>
And it fills the something.obj property with obj before calling the listener.
For JSF 1.x you can use MyFaces Tomahawk's
<t:updateActionListener />
which does the same.

Related

How to execute single input validation by #FacesValidator using ajax event and render message?

usage:JSF 2.3.2 Mojarra
while adding ajax to my jsf i met such a case:
cannnot refresh: h:message id="phoneNumber-msg" during fulfill the form
<h:messages id="validation-messages" styleClass="validation-messages"/>
<h:form>
<h:outputLabel for="phoneNumber">Phone number</h:outputLabel>
<h:inputText id="phoneNumber" value="#{bean.phoneNumber}">
<f:validator validatorId="validators.PhoneNumber"/>
<f:ajax event="blur" execute="#this" render="phoneNumber-msg"/>
</h:inputText>
<h:message id="phoneNumber-msg" for="phoneNumber"/>
[...]
<h:commandButton value="Submit" action="#{userDetails.submit}"/>
</h:form>
and validators.PhoneNumber is a #FacesValidator:
#FacesValidator("validators.PhoneNumber")
public class PhoneNumberValidator implements javax.faces.validator.Validator {
#Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { ... }
}
ADDITIONALLY
The validation work properly(message from validators.PhoneNumber occured in both: id="validation-messages" and id="phoneNumber-msg") when refreshed by the "Submit" button that ends the form:
<h:commandButton value="Submit" action="#{userDetails.submit}"/>
</h:form>
If you think about different validation way -> at EmailAddress input I used my own class anotation (EmailAddressValidator implements ConstraintValidator) which validates by #ValidEmailAddress on the bean used in the form - it renders the email validation message properly, but here I would like to validate PhoneNumber in a different way.
Is it possible to render id="phoneNumber-msg" (that means proceeding validators.PhoneNumber during fulfill the form?

Validate a number value without custom validator in JSF

I have a JSF form. I want a message to be displayed, when a user entered 0 in qty field and clicked on the Add To Card button.
Here is the JSF form:
<h:form>
<h:inputText id="qtyField" value="#{booksBean.qty}">
<!--What kind of validation should i use here?-->
<f:ajax event="blur" render="qtyMsg"/>
</h:inputText>
<h:message id="qtyMsg" for="qtyField"/>
<h:commandButton value="Add To Card"
action="#{booksBean.orderBook()}"
rendered="#{booksBean.qty>0}">
<f:ajax execute="#form" rendered="#form"/>
</h:commandButton>
</h:form>
Do I need a custom validator class just to simply compare a number value with a zero?
Like this:
#FacesValidator("myValidator")
public class MyValidator implements Validator {
#Override
public void validate(FacesContext context, UIComponent component, Object value) {
if (intValue== 0 || intValue <0) {
throw new ValidatorException(new FacesMessage(...));
}
//...
}
Is there any shorter way without creating a custom validator class?
You can use f:validateLongRange for this.
<h:inputText value="#{backingBean.input1}">
<f:validateLongRange minimum="1" />
</h:inputText>
Checks whether the local value of a component is within a certain
range. The value must be any numeric type or String that can be
converted to a long.

How to know which component triggerd an p:ajax request

I have multiple input field with a p:ajax with a listener. They all connect to the same listener. How can I know what component triggerd the listener?
<h:inputText id="postalCode" size="20" value="# businessPartner.primaryAddress.postalCode}"
<p:ajax event="change" listener="#{businessPartner.primaryAddress.retrievePostalCodeCity}" >
</p:ajax>
</h:inputText>
<h:inputText id="city" size="60" value="# businessPartner.primaryAddress.city}"
<p:ajax event="change" listener="#{businessPartner.primaryAddress.retrievePostalCodeCity}" >
</p:ajax>
</h:inputText>
public void retrievePostalCodeCity() throws MWSException {
int country = address.getCountryId();
String postalCode = address.getPostalCode();
String city = address.getCity();
}
I have this problem because I used to use a4j ajax, but I'm moving the project to fully primefaces and no longer richfaces. The listener to a4j has an AjaxBehaviorEvent event and there I could do event.getComponent().getId()
How can I do the same with prime ajax?
The AjaxBehaviorEvent is not specific to RichFaces. It's specific to JSF2 itself. So you can just keep using it in PrimeFaces.
public void retrievePostalCodeCity(AjaxBehaviorEvent event) {
UIComponent component = event.getComponent();
// ...
}
As an alternative, or for the case that it's really not possible elsewhere, you could always use the new JSF2 UIComponent#getCurrentComponent() method.
public void retrievePostalCodeCity() {
UIComponent component = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
// ...
}
By the way, the very same construct should work just fine with JSF2's own <f:ajax>. I do not see any reason to use <p:ajax> here. It would however be the only way if you were actually using a PrimeFaces component such as <p:inputText>.
Unrelated to the concrete problem, the event="change" is the default already. You can just omit it.
It is almost same in primefaces:
<p:ajax event="change" listener="#{businessPartner.primaryAddress.retrievePostalCodeCity}" />
import javax.faces.event.AjaxBehaviorEvent;
.....
public void retrievePostalCodeCity(AjaxBehaviorEvent event) {
...
}
If you want to access via button component action/actionListener tag you can use ActionEvent and for any case make sure you set ajax="true":
<p:commandLink actionListener="#{businessPartner.primaryAddress.retrievePostalCodeCity}" ajax="true" />
import javax.faces.event.ActionEvent;
....
public void retrievePostalCodeCity(ActionEvent event) {
...
}

How to invoke JSF validator on keyup event?

I have implemented a JSF validator based on this example.
How can I trigger it on keyup event of <h:inputText>?
I am using JSF 2.0 and Richfaces 4.1.0 Final.
You can attach ajax listeners to DOM events on any JSF HTML input component by <f:ajax> tag.
<h:inputText id="foo" value="#{bean.foo}">
<f:ajax event="keyup" execute="#this" render="fooMessage" />
<f:validator validatorId="fooValidator" />
</h:inputText>
<h:message id="fooMessage" for="foo" />
The fooValidator can be just a simple Validator implementation which you register in the faces context by #FacesValidator annotation.
#FacesValidator("fooValidator")
public class FooValidator implements Validator {
#Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
// ...
if (invalid) {
throw new ValidatorException(new FacesMessage("Fail!"));
}
}
}
See also:
Communication in JSF 2 - Ajax validation
In RichFaces it's not much different. There's only the <a4j:ajax> tag which is basically just <f:ajax> on steroids. But it does not really provide additional benefits in this particular case. See also Is there any difference between f:ajax and a4j:ajax?

JSF ajax commandbutton from multiple forms within same page reinitiliazes the viewscoped bean

changeThe page has 2 forms. In the first form, the ajax-button initializes an attribute of the viewscoped managed bean. In the second form, the ajax-button uses that attribute to do some stuff. Problem is that the method called by the second form button never gets invoked. Instead, the viewscoped bean is re-initialized and when I hit the same button again, of course the attribute is null resulting in NPE.When I put the 2 buttons within the same form, then everything works as expected.Is this normal: should ajax enabled buttons always reside within the same form?
<h:form id="frmDetail">
<h:commandButton id="btn_changePlant" title="#{msg.ttChangePlant}" immediate="true" image="/resources/img/edit.png">
<f:ajax event="click" render=":fsDetailPlant :headerMsg" listener="{assortiment.detailPlantId}"/>
</h:commandButton>
</h:form>
...some other code ...
<h:form id="frmOffers">
<h:commandButton id="btn_offers" title="#{msg.ttOfferPlant}" immediate="true" value="#{msg.btn_offers}">
<f:ajax event="click" render=":fsDetailPlant :headerMsg" listener="{assortiment.changeOffers}"/>
</h:commandButton>
</h:form>
and the managed bean looks like
#ManagedBean
#ViewScoped
public class Assortiment extends BeanObject implements Serializable {
....
public void detailPlantId(AjaxBehaviorEvent actionEvent) {
clear();
plantdetail = facade.findPlantdetail(plantdetailsIdModel.getRowData());
}
public void changeOffers(AjaxBehaviorEvent actionEvent) {
System.out.println("........ Assortiment.changeOffers(AjaxBehaviorEvent actionEvent)");
if (containerItems.isEmpty()) {
SessieUtils.fillPropertyItems(containerItems, PlantProperty.codeValuesList(ECodeType.logistic, "L02"), facade.getLocale());
}
//
if (offersModel == null) {
offersModel = new ListDataModel<OffersDto>(plantdetail.getOffersList());
}
}
It might just be that the listener attributes need the # prefix,
listener="{assortiment.changeOffers}"
should be
listener="#{assortiment.changeOffers}"

Resources