how action result in div or panel with h:commandButton - jsf-2

i tried to show action result in panel with this :
<p:panel id="pnl" style="margin-bottom:10px;">
<p:commandButton id="addUser" value="Ajouter" action="#{userMB.addUser}" ajax="false" target=":pnl">
but i cant get result in same page, after click i've result in new page.
as you can see i used target for this, there is a way to do it ?
Thanks in advance

Change "target" to the word "update" and remove ajax="false"
Ensure that your action method addUser either doesn't return anything (the method signature's return declaration is "void") or returns null
You may need to make your backing bean view scoped (if you do change to ViewScoped, ensure that your backing bean is Serializable)

Related

Any way to bypass form validation on p:commandButton without using o:ignoreValidationFailed or immediate=true

I create page forms with a parameter that tells them where to return when done. The backing bean is #ReqeuestScoped and includes this:
<f:metadata>
<f:viewParam name="return" value="#{createGroup.paramReturn}" />
</f:metadata>
The form has fields with validators, so the cancel button has to skip validation. So, the cancel button looks like this:
<p:commandButton value="Cancel"
action="#{createGroup.doCancel}"
immediate="true"
/>
The purpose of the doCancel() method is to go into Flash scope and return the value set with setParamReturn method.
The problem is with the immediate="true" attribute is that the Flash storage that has the paramReturn variable in it is no longer available. If someone could tell me how to fix that this would be my preferred solution.
Of course without the immediate="true" attribute the action is never invoked because the form cannot pass validation. Another solution would be to cause validation to be ignored some other way.
I have looked at the OmniFaces <o:ignoreValidationFailed> tag and it seems to do what I want, but I don't want to import a whole library for just this tag. However, that is what I will do if there is no other simple way to fix this.

generate outputLink action from data base

I have many menus of outputLink and I generated them from data base mysql
like:
<ui:repeat value="#{loginAction.subMenusBeans.subMenusBeansList}"
var="subMenusBeansList">
<ui:fragment rendered="#{menusBeansList.menuId == subMenusBeansList.menuId}" >
<h:commandLink id="circlLink" value="#{subMenusBeansList.subMenuName}"
action="{subMenusBeansList.subMenuLink}" />
</ui:fragment>
</ui:repeat>
I get value of action from database and set it to 'subMenusBeansList.subMenuLink'
output:
head quarter
main offices
offices
and so on..
when I click on any of above link
exception:
action="#{subMenusBeansList.subMenuLink}": Method not found:
I want to set action method name from database because I have many hyperlink and want to set all links action from data base and generate them to xhtml using ui:repeat but when i click on any of hyperlink's action generated from #{subMenusBeansList.subMenuLink}' throw exception
javax.servlet.ServletException: javax.el.MethodNotFoundException: /template/
templateslider.xhtml #36,135 action="#{subMenusBeansList.subMenuLink}":
Method not found: com.ss.managedbeans.SubMenusBeans#552edddd.subMenuLink()
The problem you are running in to here is that <h:outputLink action="XXXX"> actually expects a MethodExpression, which is why it is not attempting to resolve the getter for your property, "subMenuLink."
What you probably want is, as BalusC suggested, to use an ` instead. This attribute expects a ValueExpression, and should be able to resolve your property and its getter.

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 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");

<h:commandButton> does not invoke action

During some tests made using JSF 2 (mojarra on Glassfish 3.1.1) I've faced with strange behavior I can't explain.
This is my managed bean:
#ManagedBean
#RequestScoped
public class TestBean {
private int id;
public void hideButton() {
id = 0;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Here is my xhtml page
<h:form>
<h:inputHidden value="#{testBean.id}"/>
<h:outputText value="#{testBean.id}"/>
<h:commandButton value="set 1"
actionListener="#{testBean.setId(1)}">
</h:commandButton>
<h:commandButton value="hide button"
action="#{testBean.hideButton}" rendered="#{testBean.id > 0}">
</h:commandButton>
</h:form>
I expected, the button "hide button" is not visible on initial load of the page and this is really the fact. After a click on the button "set 1", the button "hide button" appeares and that is also to expect.
Really not understandable thing to me is the fact the subsequent click on the button "hide button" does not invoke the method testBean.hideButton and set the id to 0.
I've read the very useful answer from BalusC (thanks a lot really) here
commandButton/commandLink/ajax action/listener method not invoked or input value not updated
and recognize, the problem is related to the attribute "rendered", if I remove it, the action is invoked.
But as far as I aware, the class member must be initialized during the UPDATE MODEL VALUES phase and the condition mentioned in the attribute rendered should be evaluated to true during INVOKE APPLICATION phase and the action should be invoked.
The example works if I change the scope of the bean to View/Session.
But it also works fine if I remove the render attribute from the "hide button"
Would somebody explain such behavior?
In other words, at what phase the expression of the rendered attribute is evaluated to make decision not to invoke the action?
The rendered attribute is also evaluated during apply request values phase, at the moment when JSF needs to identify which action needs to be invoked. If it evaluates false, then the action can't be identified and will thus also not be invoked.
The problem is caused by the too narrow managed bean scope. As your managed bean is request scoped, it get trashed by end of response and recreated (with all properties set to default) on any subsequent request. The model value on which the rendered attribute depends will only be updated during update model values phase, which is too late. You should be placing the managed bean in view scope instead.
Apart from changing the bean scope to view scope, another way is to check the request parameter map value instead in the rendered attribute.
<h:form id="form">
<h:inputHidden id="id" ... />
<h:commandButton ... rendered="#{param['form:id'] gt 0}" />
</h:form>
(by the way your usage of > instead of gt indicates that you're using the deprecated JSP view technology instead of Facelets, I would strongly recommend to migrate to Facelets)
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated
How to choose the right bean scope?
I've found myself the reason of the problem.
The method UIComponentBase.processDecodes (at the Apply Request Values phase) calls isRendered, which returns false, because it is before the Update Model Values. Which skips the decode of the component.
There are some workarounds possible, all of them imho are not cool, but nevertheless it works
It is possible manually to set necessary value in managed bean in (post)constructor from request parameters.
Or to use

Resources