f:param not working when used with trinidad 2 - jsf-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'");
}

Related

JSF ActionListener Event

I am using an ActionListener to find out if a next button was pressed, I plan to have a previous button implemented later.
This is the xhtml
<h:commandLink styleClass="btn btn-info" id="tablePagerNext"
value="Next" actionListener="#{records.increnemt}">
<f:ajax render="dataTableRecords" />
</h:commandLink>
Now that is supposed to call my method here:
public void increnemt(ActionEvent e){
System.out.println("The action taken was " + e.getComponent().getAttributes().get("action"));
}
Where based on the action it will call an appropriate method (This just prints out The action taken was null). I know that in swing you can do
if(e.getSource == jButton){
//do this
}else{}
in JSF there is a .getSource() what I am not sure of is what that source is based on the xhtml above. I am not really sure what I am looking for when the event fires.
Could someone clear this up for me?
As per my understanding you wanted to know which button was clicked (next or prev or whatever)
As per ActionEvent documentation you dont have any method to identify button uniquely. I wish you will research on f:param to set some value to cross check the same in controller bean method (increnemt).

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.

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

How to get the value of selected radio button in action class, struts 2?

How to get the value of selected radio button in action class, struts 2?
<s:form action="vote.action" method="post">
<s:radio name="vote" list="#{'1':'Candidate1','2':'Candidate2','3':'Candidate3'}" value="2" />
<s:submit method="execute" key="label.vote" align="center" />
In order for struts2 to inject your form values in your action class you need to do one of following things
Create individual properties in your action class with same name as field value in your JSP.
Create a bean with properties required by you and make sure to name those properties same as the one in your JSP.
Create getter and setter for the properties of bean in your action class.
I suggest to go through some of the documents describing how data flows between your JSP and Action class as well in reverse way.
In short for your radio button all you need to do is define getter and setter in your action class with same name as name of the jsp radio button field and you are all set to receive the value in your action class (power and magic of interceptors ;))
processing-forms

Resources