16
votes

I am facing a problem with commandButton, it is working only when the type is submit. Can someone take a look and let me know if there is a solution for that? The code below is very simple and does have the the propose to illustrate what I need. The method test() is not executed. Method runSubmit is executed successfully.

I need that test method is executed without a submit as the original page does have validations that are executed during the submit, test() method must be executed without a submit as it is a preliminary operation before of the submit.

I am using PrimeFaces 4.0, JDK 7, Tomcat 6 and JSF 2.0 (Apache), however I think it is happening in Mojarra as well.

    SESSION:

package com.andre.bean;

public class AndreBean {

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

public String test() {
System.out.println("Not submit executed");
return "true";
}

}

XHTML

<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:p="http://primefaces.org/ui">

<h:head> 
</h:head>

<h:form id="test">
<p:commandButton id="ns" value="not submit" type="button" action="#{andreBean.test}" ajax="false"></p:commandButton>
<p:commandButton id="s" value="submit" action="#{andreBean.runSubmit}"></p:commandButton>

</h:form> 

</html>

Thank you very much Andre

3
try with actionListener="#{andreBean.test()}" process="@this :test" ..Thangnv
Thanks for your response, just tried and didn't work, same result as before. However, I am got a parsing error when use parenthesis in the test method, works only with "andreBean.test". I read something that is some limitation related with EL, I need to use Tomcat version 6 in this project so EL available is up to 2.1. Thanks in advance for additional information.Andre
If you do not want a full form submit you'll have to use AJAX...unwichtich
I think I have a restriction because I need to use Tomcat 6. When I use parenthesis in my method (andreBean.test()) I got a parsing error and when I remove the parenthesis I receive a message saying that property "test" does not exist in "andreBean"...deadlock :( I have used ajax in the commandButton as below using parenthesis and not using: <f:ajax execute="@form" listener="#{andreBean.test}" /> Some other suggestion?Andre
You don't need the f:ajax. You can set ajax="true" (which is the default) on the p:commandButton and then use actionListener="#{andreBean.test}"unwichtich

3 Answers

36
votes

What's going on?

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.

Therefore only your second button will execute beans' method:

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

What probably you wanted to obtain?

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

Hope that helps.

14
votes

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

-4
votes

Please check your binding with bean.

bean fields should be String or non primitive.