How to iterate over variable in param using JSF EL expression - jsf-2

I have a PrimeFaces <p:dataGrid> component that contains a variable number of panels. Each panel contains a number of derived components. I have a delete button contained inside each of these panels to allow for deletion. I also have an add button outside of the dataGrid. Instead of using immediate="true" on the buttons, I figured out how to set the required attribute of each component in each panel.
For instance:
required="#{empty param['vehicleGrid:0:btnDelete'] and empty param['btnAdd']}".
For every delete button in the dataGrid and the add button, ignore component validation.
This works if there is a panel inside of the dataGrid, but it only references the first one. I need to dynamically check every panel. Maybe instead of looking at it from the markup page, maybe I need to look at it in Java terms since param is a Map<String, String>.

Bind the delete button component to the view and use UIComponent#getClientId() instead.
<h:inputText ... required="#{empty param[deleteButton.clientId]}" />
...
<h:commandButton binding="#{deleteButton}" ... />
This way the proper client ID will be looked up in the parameter map and there's then no need to iterate over the parameter map.

Related

Composite Component and setPropertyActionListener

I have a composite component that have a dialog...
Inside the dialog I have the following piece of code:
<p:commandButton id="selectButton" icon="ui-icon-check" oncomplete="lookupDialog.hide();" update=":#{cc.clientId}:#{cc.attrs.fieldId}_panelGrid">
<f:setPropertyActionListener target="#{cc.attrs.targetValue}" value="#{entity}" />
</p:commandButton>
So, when the button is clicked, the dialog vanishes, but the property isn´t set.
There is no errors, no warnings, nothing! So I simply don´t know what is happening...
If you need anymore details, please just say so! :)
***EDIT
This is a related question, but not really what I want to do...
Pass Argument to a composite-component action attribute
I just need the propertyActionListener to work.
Here some extra information:
<cc:attribute name="targetValue" required="true"/>
the value:
targetValue="#{acaoController.entity.responsavel}"
Where inside the bean (acaoController)
I have an entity...
And inside the entity I have another object, that is "responsavel".
Try checking setters and getters of responsavel getting called when you close the dialog box.
Otherwise you can use Flash to pass on the value between components. In the action method of command button,
Flash flash = FaceUtil.getFacesContext().getExternalContext().getFlash();
flash.put("entity",entity);
And you can extract the value like this:
Flash flash = FaceUtil.getFacesContext().getExternalContext().getFlash();
responsavel = (Responsavel) flash.get("entity");

Composite compoent command button actionListener issue

Trying to develop a composite component using jsf2.0 (Mojarra) which should render command buttons dynamically based on the list from the bean. The button action and immediate attribute are working fine.Trying to add the action Listener attribute whereas the action Listener should not work for the buttons whose immediate attribute value is true.Could anyone please suggest me how to achieve?
You can put two mutually exclusive command button into your component, one of them has actionListener and only renders when the immediate attribute is false, where as the other should only be rendered when the immediate attribute is true and it doesn't have actionListener.
<h:commandButton value="buttonWithoutActionListener"
rendered="#{cc.attrs.immediate}"/>
<h:commandButton value="buttonWithActionListener"
rendered="#{!cc.attrs.immediate}"
actionListener="#{cc.yourListener}"/>

JSF: setting a property in a wizard form with multiple submit actions

I have this scenario: in the first tab of a primefaces wizard component, I have some fields and a button. The button saves some data, does some business logic and, with the results, sets some properties of the form bean (which is in ViewScope) that are not related to a specific field of the form. I have checked that in the invoke application phase, the values are set properly.
In the second tab I have another button that has to do some business logic using the values set by the first one but, doing some debug, I noticed that the values, even if not related to any field of the form, are overwritten I think during the update model phase invoked when I click the second button.
How can I avoid this? Is there a way to obtain the correct behaviour?
I looked around but I couldn't find any example of a wizard form with multiple submissions. Thanks for help!
<p:wizard widgetVar="wiz" render="true" id="wizard" showNavBar="false">
<p:tab id="step0" title="Step0" step="0">
<!-- Some other fields-->
<p:commandButton value="Save and do some business logic"
action="#{formBean.save}"
oncomplete="wiz.loadStep (wiz.cfg.steps [1], false)">
</p:commandButton>
</p:tab>
<p:tab id="step1" title="Step1" step="1">
<!-- Some other fields-->
<p:commandButton value="Second button: use the previous informations"
action="#{formBean.doSomething}"
oncomplete="wiz.loadStep (wiz.cfg.steps [2], false)">
</p:commandButton>
</p:tab>
</p:wizard>
Edit:
To show an example I can say that my bean contains a business logic object. During the first submission, this object is being saved so the database (Mysql and Hibernate), assigns to it a progressive id.
During the second submission, when I try to read this id, the value is zero so, obviously, I get an error.
Something like:
public class FormBean{
private BLObject object;
// Constructor Getters and setters
// Method executed during the first submission
public void save(ActionEvent actionEvent) {
//Save the object and set his id
PersistanceClass.getInstance().save(object);
}
// Method executed during the second submission
public void doSomethingWhitTheId(ActionEvent actionEvent) {
//Access the id... id=0 returned
int id = object.getId();
}
}
The problem was that, navigating from one page to another, Primefaces wizard, uses a String to identify the tab. Returning the value of this String, which is not null nor void, makes the view bean go out of scope. It is ok when you have a simple form but, if you wanna set some properties not related to a form element during the flow, they get overwritten during the tab changes.
I solved the problem adding to the form an hidden parameter linked to the property I wanted to preserve.
<h:inputHidden value="formBean.idToPreserve" id="inputHidden" />

Dynamically added option in SelectOneMenu control on a JSF form submits as null

I have a standard JSF h:form which contains an h:SelectOneMenu control. As long as I am selecting an item from the list which is populated when the page is rendered it works perfectly. I don't think it is important, but to put it in context, the value from the select is used to build a query which returns a list of matching records.
I've implemented the JQuery autocomplete box on the control and it still works just fine as long as I'm selecting one of the original values.
The problem comes when I enter a value not in the select control when the page is rendered. Using JQuery, I've set it up so that when a value not on the list entered, the value is added to the select as a new option.
I can verify that the option is added to the underlying select control, and selected through the javascript. However when the setter is invoked in the backing bean immediately after that, the value passed in to the setter is null, and the function to run the query is never reached. The following error is returned in the AJAX response, but I have yet to be able to find a place where the value is validated. It isn't a required field either.
Validation Error: Value is not valid
Here is my front end code:
<h:selectOneMenu id="make" styleClass="combobox" value="#{listBean.make}"
effect="fade" label="#{listBean.makeLabel}" >
<f:selectItems value="#{listBean.makeList}" />
</h:selectOneMenu>
And the setter in the bean:
public void setMake(String make) {
this.make = make;
}
I'm guessing I just need to find a way to include the new option in the makeList List on the backing bean, but I'm not sure how to do that. Any help or suggestions would be appreciated.
Java EE 6, GlassFish 3.1, Eclipse 3.7 - problem observed on both FireFox and Chrome
You need to provide the dynamically added item through <f:selectItems>, not through JavaScript. If the item is not present in <f:selectItems>, then you will get exactly this validation error. This is done so as part of safeguard against tampered/hacked requests in an attempt to get illegal/unprovided values into the server side.
Easier is to use a JSF component library. PrimeFaces for example has a <p:autoComplete> for the exact purpose.

Selenium and JSF 2.0

When I a generate SelectOneMenu with JSF2.0 the the id I specified in the xhtml is attached to a generated ID from JSF.
e.g. out of my_fancy_id it generates j_idt9:my_fancy_id
Now I want to test my page with Selenium 2 Web Driver. I try to re-find my select menu:
driver.findElement(By.id("my_fancy_id"));
Of course it does't find anything because the id is changed. What is the best way to find the select menu on the page?
Usually the id of the form is prepended to all element ids inside the form. If you don't set a form id, JSF does it for you (the 'j_idt9'). Solution: Assign an id to your form and try to use the full id in your findElementmethod, e.g.:
<h:form id="myForm">
...
</h:form>
Call it this way:
driver.findElement(By.id("myForm:my_fancy_id"));
or you can add <h:form prependId="false"> so that the id of the form does not get prepended
You set the component identifier on controls; the renderers emit the client identifier to the markup.
This allows JSF to emit valid HTML ids (they must be unique) even in the face of templates and complex controls. A control will be namespaced by any parent that is a NamingContainer (such as a form).
In some containers, the client identifier will be namespaced by the view, but this generally only happens in portlet environments.
Some component libraries (like Tomahawk) have a forceId attribute, but care must be exercised in using them. I wrote a more extensive post on client identifiers here.

Resources