Composite compoent command button actionListener issue - jsf-2

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}"/>

Related

JSF 2 + Primefaces: Need to update bean property on menu click before rendering dialog

I have a p:menuitem that needs to (1) update a backing bean property when clicked, and then (2) show a p:dialog.
This is the set up I have:
<p:menuitem value="Show Dialog"
oncomplete="dialog_widget.show();"
update=":dialog"
actionListener="#{bean.setCurrentAction}">
</p:menuitem>
<p:dialog widgetVar="dialog_widget" id="dialog" dynamic="true">
<h:form>
<p:inputText value="#{bean.record.text} />
// the proper rendering of this dialog form depends on bean.currentAction
// being set during JSF Phase 4 Update Model Values
</p:dialog>
And the backing bean:
public R getRecord() {
if (currentAction == null) {
return null;
}
return currentAction == NEW ? newRecord : selectedRecord;
}
The problem is that actionListeners and actions are only executed during Phase 5 and I need the bean.currentAction to be set before that so the dialog can be properly updated and rendered.
** A little background on what I'm trying to achieve: the dialog form is used to Create new records as well as Update exsiting records (Add and Edit Dialog). So the "currentAction" on the bean indicates which action the user is doing. Depending on which action, the form needs to use different model objects ("newRecord" or "selectedRecord") to pull and save the form data to.
Although not a very elegant solution you can use PrimeFaces' RequestContext's update method to set update target and use the execute method to show your dialog in your actionListener after setting the needed property.
If you requirement is to call the backing bean method before the dialog opens then you can go for a ajax function (i don't know whether you are allowed to use ajax in your application). for p:menuItem there is a function called onclick, where you can call a a4j:ajax function through which you can call a backing bean method and update the model before dialog opens.
By default action is called during "Invoke Application" phase. You can add immediate="true" attribute in p:menuitem tag. This will call action in "Apply Request Values" phase.

How to iterate over variable in param using JSF EL expression

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.

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.

f:param not working when used with trinidad 2

I am using JSF 2 Trinidad 2(MyFaces) and trying to use f:param in a command button
<tr:commandButton text="Submit" action={backingBean.actionSubmit}>
<f:param name="isSubmit" value="Yes" />
</tr:commandButton>
Basically I am trying to pass the param value for submit button and only perform the validation if a Submit button is clicked adding the condition to the required attribute for input elements to check for the submit button.
Is there a way to identify a particular button click in JSF2/Trinidad2 so that I can check for that button click in the validation method.
I am using JSF 1.2 but I think it works in JSF 2.0 the same way.
For your commandButton use an actionListener instead of action.
<tr:commandButton text="Submit" actionListener={backingBean.actionSubmit}>
Within your bean you can get the id of your button:
public void actionSubmit(ActionEvent event)
{
event.getComponent().getId();
}
You could consider to use <tr:setActionListener/>. In your case you could use something like:
<tr:commandButton text="Submit" action="#{backingBean.actionSubmit}">
<tr:setActionListener from="submit" to="#{backingBean.pressedButton}" />
</tr:commandButton>
Basically you use it to copy an object from the from attribute to the to attribute. Note that you could use EL in the from attribute as well (#{someBean.someObject}).
In you backing bean you need to add a String member called pressedButton (with getter and setter). Now you can use the following code in your actionSubmit
if ("submit".equals(pressedButton))
{
System.out.println("You pressed 'sumbit'");
}

grails set bean value from radio button

I'm somewhat new to grails (not groovy though) and I'm working on a sample CRUD application. The issue I'm trying to solve is how to set a property on a bean based on a radio button before I update it in the database. Is the Form Helper http://www.grails.org/plugin/form-helper plugin the way to go? Will the bean have its value set regardless of if the button is actually clicked by the user or if it is left at its default value?
thanks,
Jeff
Have you tried setting the name of the radio buttons to the bean property name and include the radio buttons in your form or remoteForm submission?

Resources