-3

I have this JSF button which calls Java method when it's pressed:

<h:commandButton id="editdata" value="HiddenDelete" style="position:absolute; bottom:25px; right:650px;" actionListener="#{bean.saveData}" rendered="#{bean.editable}">
    <f:ajax render="@form" execute="@form"></f:ajax>
</h:commandButton>

public void saveData() throws SQLException
{
.....
}

When I make a AJAX call the button is not working properly. Can you help me to find why the Java method is not called after AJAX call?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 2
    Please exclude the causes from http://stackoverflow.com/a/2120183 or post an SSCCE. The current code is not in SSCCE flavor. When copypasting your code and manually adding the minimal required code which you omitted from the question in order to get it to work as per the specification/documentation (e.g. adding the missing `ActionEvent` argument which should however have thrown exception), it works fine for me (and likely also everyone else). To learn how to create a proper SSCCE, carefully read http://stackoverflow.com/tags/jsf/info Without an SSCCE, your question is basically unanswerable. – BalusC May 15 '13 at 16:16
  • 2
    Further you would do yourself and us a favour if you elaborate the problem in developer's perspective and not in enduser's perspective. You merely stated "it doesn't work" like as if you were the enduser. This is not right. You should tell what step exactly of the whole process has failed. Firing of the DOM click event? Preparing of the ajax request? Sending of the ajax request? Decoding the ajax request by JSF? Identifying the clicked button? Queueing the action event? Invoking the action event? Etc. – BalusC May 15 '13 at 16:23
  • Peter, have you tried passing the event to your method (as suggested in my answer and also mentioned by BalusC)? If so, can you please share your updated code and the resulting case? 'Not working properly' is insufficient. Also share the annotations of the bean and the implementation of both `saveData` and `editable`. – Menno May 16 '13 at 10:06
  • Please write down detailed code for your jsf page with it's scope, and does your xhtml code wrapped into a form? – Jitesh May 20 '13 at 10:59
  • http://stackoverflow.com/questions/9393411/action-not-triggered-when-button-is-enabled-disabled-with-ajax http://stackoverflow.com/questions/12222687/jsf-fajax-listener-vs-commandbutton-action http://stackoverflow.com/questions/9880674/commandlink-action-does-not-work-after-an-ajax-call-jsf-2-0 –  May 22 '13 at 15:19

1 Answers1

0

You're not passing the event:

public void saveData(AjaxBehaviorEvent event) { ... }

Also, what do you expect to happen with the Exception you're throwing? Shouldn't you catch it?

try {
    // logic
}
catch(SQLException ex) {
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    FacesMessage facesMessage = new FacesMessage( 
        "There was an error, ...."); 
    facesContext.addMessage(null, facesMessage);
}

This will be printed in HTML by <h:messages />.

Menno
  • 12,175
  • 14
  • 56
  • 88
  • Well in case you implemented the code as given in my example, you're problem is probably caused elsewhere. Are you using `` tags instead of ``? The normal HTML-tags won't include `javax.faces.jsf.js` which is a necessary library which enables you to use ajax. – Menno May 13 '13 at 11:11
  • I've taken a quick look and the only thing that jumps out to me is that you're referring to the bean starting with a capital letter, which is not best practice material. Though I'm assuming this isn't causing you're problem since you're getting your values from the bean just fine, right? The only thing I can add to this is to provide a [SSCCE](http://sscce.org/) since I cannot point to any problem. Try creating a bean/html as short as possible which is still containing your problem. – Menno May 13 '13 at 13:25
  • True, the OP posted a terribly bad code snippet, but it's more likely that the problem is caused because the bean is request scoped and/or he omitted the `ActionEvent` argument while ignoring the logs and ajax response for any clues about any possible exceptions. Using `` does absolutely not require the move of action method to ``. – BalusC May 15 '13 at 16:19
  • @BalusC You're right. Though the method should still be called in case the bean is request scoped. Could this be caused due to the fact that the `rendered`-condition is false upon submitting the form? – Menno May 16 '13 at 09:07
  • @PeterPenzov: As noted in my edit, is your bean serializable? – Menno May 21 '13 at 19:22
  • If it wasn't while required, the OP would have received a `NotSerializableException` which is already a big hint at its own. Unless the OP never reads server logs, this is likely not the case. The cause is most likely #5 or #7 of http://stackoverflow.com/a/2120183, provided that OP has already fixed the bad code in the question. – BalusC May 22 '13 at 11:39