Rectangle 27 7

A command button does not use an attribute or parameter to construct the URL to which redirection happens.

For this you normally use the h:commandLink or optionally the h:outputLink, but you need to be aware that these will render the parameters as they were at the time the page was requested. If the user makes changes to the selection after that, those won't be reflected in the link.

An alternative that would give you the latest selection, is using an action method and construct the redirect URL there.

And then in your bean:

As per the comments, if bean2 is a generic bean that has no knowledge about the page, you can pass in the target page as a parameter:

<p:commandButton action="#{bean2.navigateWithSelection('page1')}" />

And then in your bean again:

public String navigateWithSelection(String target) {
    return target + "?faces-redirect=true&" + convertSelectedItemsToString();
}

As a temporary solution I went your 2nd approach. But I dont like it, because that way my controller (bean) is heavily dependent on my views. I expected that jsf would provide me a better solution.

In JSF a backing bean is by convention "supposed" to back a single view, so that backing bean can be view dependent. Isn't bean2.convertSelectedItemsToString already view specific, or are you doing something generic there? If you need a generic solution you could pass in the page as a parameter. See updated answer.

primefaces - JSF ViewParam from CommandButton - Stack Overflow

jsf primefaces
Rectangle 27 7

A command button does not use an attribute or parameter to construct the URL to which redirection happens.

For this you normally use the h:commandLink or optionally the h:outputLink, but you need to be aware that these will render the parameters as they were at the time the page was requested. If the user makes changes to the selection after that, those won't be reflected in the link.

An alternative that would give you the latest selection, is using an action method and construct the redirect URL there.

And then in your bean:

As per the comments, if bean2 is a generic bean that has no knowledge about the page, you can pass in the target page as a parameter:

<p:commandButton action="#{bean2.navigateWithSelection('page1')}" />

And then in your bean again:

public String navigateWithSelection(String target) {
    return target + "?faces-redirect=true&" + convertSelectedItemsToString();
}

As a temporary solution I went your 2nd approach. But I dont like it, because that way my controller (bean) is heavily dependent on my views. I expected that jsf would provide me a better solution.

In JSF a backing bean is by convention "supposed" to back a single view, so that backing bean can be view dependent. Isn't bean2.convertSelectedItemsToString already view specific, or are you doing something generic there? If you need a generic solution you could pass in the page as a parameter. See updated answer.

primefaces - JSF ViewParam from CommandButton - Stack Overflow

jsf primefaces
Rectangle 27 2

If you intend for navigation to occur you should use action not actionListener in your commandButton, so:

<p:commandButton value="Submit"  action="#{beanController.getStr(strSearch)}" ajax="false"/>

Thanks, I was about to lookup the difference between them. and I saw your answer.

jsf - Primefaces commandLink works but not commandButton - Stack Overf...

jsf primefaces
Rectangle 27 2

If you intend for navigation to occur you should use action not actionListener in your commandButton, so:

<p:commandButton value="Submit"  action="#{beanController.getStr(strSearch)}" ajax="false"/>

Thanks, I was about to lookup the difference between them. and I saw your answer.

jsf - Primefaces commandLink works but not commandButton - Stack Overf...

jsf primefaces
Rectangle 27 11

Use the oncomplete attribute instead of onsuccess attribute.

<p:commandButton ... update="@form :dlgEdit" oncomplete="_dlgEdit.show()" />

The onsuccess attribute is invoked directly when ajax response is successfully arrived but long before the ajax updates are performed based on the ajax response. The oncomplete attribute is invoked when the ajax updates are successfully completed. See also the tag documentation of <p:commandButton>.

Basically, this is the event invocation order:

onclick
  • ajax request is prepared with form data based on process
onbegin
  • ajax response is successfully retrieved (HTTP status code is 200)
onsuccess
  • ajax updates are performed in HTML DOM based on update
oncomplete

I would just add that my opinion is that dynamic="true" in this example doesn't have any sense, as dialog is already completely updated after AJAX request, so in AJAX response there would be whole dialog, and another AJAX request will be made to load dialog dynamically.

@partlov: Yes indeed, that attribute can be omitted. However, it's useful if you have relatively large content in the dialog and would like to not immediately load it on initial request, but only when dialog is opened for 1st time.

I don't know exactly how would Primefaces act here, but if dynamic dialog is completely updated, it is maybe in state "not loaded", as it is initially loaded. So AJAX loading of dialog will be fired again? Maybe I'm wrong but I'm not in situation to check this right now.

@partlov: the show() implicitly triggers dynamic loading, but when used in onsuccess it will later be overridden when the update takes place with content from "one step behind".

@BalusC Yeah!! Thank you so much!!!! I tried to show an alert on the two events, but I didn't try to move _dlgEdit.show() to oncomplete. Me stupid. @partlov dynamic="true" is for the real case, much complex and "dynamic" than my example here. Btw, it's always useful to know these details. Thank you

jsf 2 - Primefaces p:dialog doesn't always show up if update="dlg" in ...

jsf-2 primefaces
Rectangle 27 17

You can try either of the following , I vote number one, it's a cleaner design IMO

  • Bring the <p:dialog/> outside of the general <h:form/> and put an <h:form/> inside it instead <p:dialog id="dlg" header="#{messages.chooseSkillLevel}" widgetVar="dlg" modal="true" dynamic="true"> <h:form> <h:dataTable value="#{editSkills.skillsAndLevels}" var="skillslevel"> <h:column> #{skillslevel.skill.umiejetnosc} </h:column> <h:column> <p:selectOneMenu value="#{skillslevel.level}" > <f:selectItems value="#{editSkills.levels}" var="level" itemLabel="#{level.stopien}" itemValue="#{level.id}" /> </p:selectOneMenu> </h:column> </h:dataTable> <p:commandButton value="#{messages.confirm}" action="#{editSkills.showSkillsAndLevels}" oncomplete="dlg.hide();" /> THIS BUTTON IS NOT FIRED <p:commandButton value="#{messages.cancel}" onclick="dlg.hide()"/> </h:form> </p:dialog>

Add appendToBody="false" to the <p:dialog/> to ensure the dialog is rendered within the html form in the DOM instead of being auto-relocated to end of HTML body. But this may cause inconsistent rendering in various browsers.

Thanks for your answer! I did what you have told me to do, but now I'm getting this error: /user/editSkills.xhtml @39,121 itemValue="#{skill.id}": The class 'java.lang.String' does not have the property 'id'. What is strange I think it's connected with picklist and it's weird, cause picklist works fine. What should I do?

@AjMeen, that's a different issue altogether,probably caused by the fact that #{skill.id} and #{skillslevel.level} are not the same datatype(one of them is a string, the other is something else). But now your commandButton is definitely firing.

#{skillslevel.level} has nothing to do with this error, cause even if I remove skilsslevel part the error still shows up. I just probably need a converter. Anyway thanks for help!

@AjMeen, You're welcome. You should only need a converter if itemValue is a POJO/complex type. Look at this answer. The question directly correlates to yours

jsf 2 - Primefaces dialog + commandButton - Stack Overflow

jsf-2 dialog primefaces commandbutton
Rectangle 27 5

The commandButton generates a POST request submitting the form data of the form enclosing this button to the server. If you want to submit only the form inputs on the dialog enclose the dialog with a <h:form> and to get away with the problem of nesting the forms use appendToBody attribute on the dialog like this:

<p:dialog appendToBody="true">
  <h:form>
  </h:form>
</p:dialog>

Thanks Ravi for the help. I took off the dialog include from the main form and i put another form inside dialog. I set appentToBody="true" too. Now, it worked.

jsf - How to process only the primefaces dialog in commandButton actio...

jsf process dialog primefaces partial
Rectangle 27 6

ajax="false"
p:commandButton
h:commandButton

THought I had tried that, but tried it again and it works!

jsf 2 - jsf target="_blank" not working with primefaces commandbutton ...

jsf-2 primefaces
Rectangle 27 4

<p:commandButton  value="Cancel" oncomplete="PF('yourDialogWidgedVarName').hide();" process="@this"  />

Make sure to use ' when u call the widgetVar name. You can also use immediate="true" or process="@this".

you shouldn't need immediate="true" or process="@this". However your answer had the quick bit of information I needed. Thanks!

jsf 2 - Primefaces dialog + commandButton - Stack Overflow

jsf-2 dialog primefaces commandbutton
Rectangle 27 9

You need to reload the users from the DB after deletion.

userService.removeUserById(id);
users = userService.list();

Thank you very much. I don't know why I forgot that. Reloading the list works great!

jsf - Primefaces - update datatable with commandButton doesn't work - ...

jsf primefaces datatable ajax-update
Rectangle 27 4

Just let JSF validate by Ajax. Add a <f:ajax> or <p:ajax> which processes the current input field and re-renders the associated message when the blur event fires (when you tab out the field, e.g.).

<h:inputText id="foo" value="#{bean.foo}" required="true">
    <f:ajax event="blur" render="fooMessage" />
</h:inputText>
<h:message id="fooMessage" for="foo" />

jsf - JQuery Validation Engine and primefaces commandButton - Stack Ov...

jsf jsf-2 jquery-validate facelets primefaces
Rectangle 27 1

Your problem occurs because our table is not rendered it simply does not exist in your html code, that is not in this context to be found to update or another source.

When you shut a larger scope as the panel and force an update in this it forces the table to check the condition for rendering, if yes the code is written and can be seen without problems.

Thanks for the reply, but could you please help me to understand how to fix the issue? Thanks

primefaces - JSF rendered is not working after commandButton - Stack O...

jsf primefaces
Rectangle 27 1

The value in inputText should not be empty

<p:inputText id="i10" value="" />

Instead you have to related to a property in your managed bean for example:

<p:inputText id="i10" value="#{cadFrota.attribute}" />

You have to create your attribute in your ManagedBean.

I think its this, this is a register page, so a i have to receive the inputted data from user by this value?

yeap, the value in inputText should not be empty

JSF Primefaces commandButton - Stack Overflow

jsf primefaces
Rectangle 27 31

What you get is correct behaviour. In PrimeFaces button with type="button" works as it does in basic HTML - it doesn't cause any request. As PrimeFaces user's guide says:

Push buttons are used to execute custom javascript without causing an ajax/non-ajax request. To create a push button set type as "button".

<p:commandButton type="button" value="Alert" onclick="alert('Prime')" />

If you want to "talk to" bean, you need to use type="submit" (which is default in p:commandButton). However... contrary to submit buttons behaviour in HTML, in PrimeFaces such submission will not force redirection to new page but all communication will be handled by underlying AJAX requests.

<p:commandButton id="s" value="submit" action="#{andreBean.runSubmit}" />

If you don't want to send all your form to bean you can limit the scope of components that are processed with "process" attribute of p:commandButton:

<h:form id="test">
    <p:inputText value="#{andreBean.value}"/>
    <p:commandButton id="s" value="submit" action="#{andreBean.runSubmit}" process="@this" />
</h:form>

With the following bean you will see the difference:

public class AndreBean {

    private String value;

    public void runSubmit() {
        System.out.println("Submit executed");
    }

    public String getValue() {
        System.out.println("getValue");
        return value;
    }

    public void setValue(String value) {
        System.out.println("setValue: " + value);
        this.value = value;
    }

}

If you don't limit executed components in console you get:

getValue

setValue: foobar

Submit executed

...and with components limited only to process="@this" you get only:

Submit executed

I was getting the similar issue and the process tag works. It was initially set to @this but then I add all my component id's so it would look like this, process=@this, componentId1, componentId2 this changes just works for me, and I didn't added the immediate=true flag t00

jsf - Primefaces - commandButton does not work - Stack Overflow

jsf primefaces
Rectangle 27 2

<p:commandButton value="#{msg['forecast']}" onclick="validator.validate();" update="somegrid" actionListener="#{someBean.someAction}" styleClass="ui-priority-primary" />
var validator = null;
$(document).ready(function () { 
    validator = $("form[id='forecastinput']").validate({ 
    //rules, messages, etc. 
    }); 
});

jsf - JQuery Validation Engine and primefaces commandButton - Stack Ov...

jsf jsf-2 jquery-validate facelets primefaces
Rectangle 27 10

Sometimes, the solution is simply add immediate="true", it changes the point, in JSF lifecyle, in which the bean action is triggered.

  • Here are an article about how and when use it: immediate attribute article

it works because you were hitting some validations that you don't see as long as you did not but any p:messages in your code.now when you added immediate=true you are bypassing validations and submitting.

jsf - Primefaces - commandButton does not work - Stack Overflow

jsf primefaces
Rectangle 27 2

Thanks to BalusC for all your help. I am still not sure how I missed this! The following works:

CommandButton submit = new CommandButton();
submit.setValue("Submit");
submit.setUpdate("myPanel");
submit.setId("create"+panelClass);
FacesContext facesCtx = FacesContext.getCurrentInstance();
ELContext elContext = facesCtx.getELContext();
Application app = facesCtx.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
MethodExpression methodExpression =null;
methodExpression = elFactory.createMethodExpression(elContext,"#    {createDeal.saveDeal}",null, new Class[]{});
submit.setActionExpression(methodExpression);
createButtons.getChildren().add(submit);

The null pointer exception was unrelated. It looks like simply adding the ID and removing the ActionListener did the trick. Much obliged!

primefaces - JSF commandButton not working when created dynamically wi...

jsf primefaces
Rectangle 27 2

your form tag should be like this <h:form> because you are using h:commandButton tag and your p:dataTable tag should be inside h:form tag. Write method name in EL without parenthesis.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<f:metadata>
    <f:event type="preRenderView" listener="#{hibernateController.getAllWebSites()}"/>
    <f:event type="preRenderView" listener="#{hibernateController.getAllScores()}"/>
</f:metadata>
<h:head>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Mood Watch</title>
    <h:outputStylesheet library="css" name="style.css"  />
</h:head>   

<h:body> 
    <div id="wrapper">
    <div id="header">
        <h2>Mood Watch</h2>
    </div>
    </div>

    <h:form>
        <p:outputLabel>Filter : </p:outputLabel>
         <p:selectOneMenu id="whySelect1" value="#{hibernateController.siteFromMood}">
                 <f:selectItems value="#{hibernateController.mySites}" var="tempSites" itemLabel="#{tempSites.site}" itemValue="#{tempSites.moodSite}" />
            </p:selectOneMenu>
<h:commandButton value="Filter" actionListener="#{hibernateController.filterSites}"/>

                <h:commandButton value="View All" actionListener="#{hibernateController.setSitMoodToNull}"/>



    <p:dataTable id="moodTable" var="moodValues" value="#{hibernateController.myFrontEndModels}">
        <p:column headerText="Date">
            <h:outputText value="#{moodValues.dateCreated}" />
        </p:column>

        <p:column headerText="Site">
            <h:outputText value="#{moodValues.sitename}" />
        </p:column>

        <p:column headerText="Value">
            <h:outputText value="#{moodValues.moodValue}" />
        </p:column>

    </p:dataTable>


 </h:form>
</h:body>

Trust I have tried that as well I just took it out because the table has nothing to do with the form the table just renders data , but I just tried it again and didnt work

Can you upload your full backing bean code that contains the methods??

you didn't have h:form tag, did you replace <form> with <h:form>??

Thank you soo much bruh, I spent 2 hours on this can you imagine

no problem, i actually get worried when you say "no trigger", but Glad that the problem is solved now. please do upvote the answer also.

primefaces - Jsf h commandbutton not working - Stack Overflow

jsf primefaces
Rectangle 27 1

If you look at the markup of the Primefaces command button you will see it uses the button tag with a type of "submit" by default.

<button type="submit" style="height: 22px; line-height: 22px; font-size: 10px ! important;" onclick="PrimeFaces.ajax.AjaxRequest('/webapp/admin/listBadges.xhtml',{formId:'AddBadgesForm',async:false,global:true,source:'AddBadgesForm:j_idt54',process:'@all',update:'listBadgesForm:badgeList AddBadgesForm'});return false;" name="AddBadgesForm:j_idt54" id="AddBadgesForm:j_idt54" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" role="button" aria-disabled="false"><span class="ui-button-icon-primary ui-icon icoIDAdd"></span><span class="ui-button-text">&nbsp; Add Badge</span></button>

This is just an example of what you would see. Noting that, I am not sure how one would use Jquery validation on a button like this. As far as the user experience is considered however the server side validation is all done through AJAX so it is transparent to the user. I feel your reasoning for avoiding this is unjust unless your reasoning really is that performance is a huge concern or that visually it doesn't meet your business requirements.

jsf - JQuery Validation Engine and primefaces commandButton - Stack Ov...

jsf jsf-2 jquery-validate facelets primefaces
Rectangle 27 1

JSF won't work out of the box with JQuery validate plugin.

You must tweak it to make it work. This is because JSF posts forms using AJAX and not the default form.post method.

Use this to chain jquery validation when posting with h:commandlink and h:commandbutton:

//executes code before running default onclick behavior
//@param domId dom element id for the object which onclick method is being intercepted
//@param code a function holding the code to run before executing the original onclick method. This function should return true if original method is to be performed
function triggerBeforeOnClick(domId, code){
    if(document.getElementById){
        var commandLink = document.getElementById(domId);
        var commandLinkOnClick = commandLink.onclick;
        commandLink.onclick = function(){
            if(code()){
                return commandLinkOnClick();
            }
            return false;
        }
    }
}

replace domId with

Make sure you use it within $(document).ready(function( ... )); and after having loaded jquery and validate libraries.

You must also specify validation rules for each input field by hand (disregard jquery docs on this matter) like this:

$(document.getElementById('formId:inputId')).rules('add', {
      required: true
      , messages: {
         required: 'error message'
      }
});

jsf - JQuery Validation Engine and primefaces commandButton - Stack Ov...

jsf jsf-2 jquery-validate facelets primefaces