0

i'm using jsf + primefaces 3.5. And my button isn't calling one method in my managed bean.

I have this xhtml:

 <h:form>
      <p:inputText id="name" value="#{userMB.userSelected.name}" />  
      <p:commandButton id="btnSave" value="Salvar" actionListener="#{userMB.save}"/>  
 </h:form>

And my managed bean is:

@ManagedBean
@SessionScoped
public class UsuarioMB implements  Serializable{
 User userSelected; 

 public void save(){
     System.out.println(userSelected.getName());
     //call my daos and persist in database


    }
}

The most curious is that if i remove the , the method is called!

If i put a atribute in p:commandButton "imediate = true ", the method is called, BUT, the information (userSelected.name) is null !

Thanks very much :)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

1

It failed because it threw a NullPointerException because you never initialized userSelected.

Add this to your bean:

@PostConstruct
public void init() {
    userSelected = new User();
}

If you have paid attention to the server logs, you should have seen it. As to the complete absence of feedback about the exception in the webbrowser, whereas in normal synchronous (non-ajax) you would have seen a HTTP 500 error page, it's because you're sending an ajax request without apparently an ExceptionHandler configured.

That it works when you set immediate="true" on the button is simply because it will then bypass the processing of all input components which do not have immediate="true" set.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

You have not given a name to the managedbean UsuarioMB. As suche it will be named usuarioMB.

@ManagedBean – marks this bean to be a managed bean with the name specified in name attribute. If the name attribute in @ManagedBean is not specified, then the managed bean name will default to class name portion of the fully qualified class name.

read more about it in this blog: http://mkblog.exadel.com/2009/08/learning-jsf2-managed-beans/

Secondly, if your code above is complete, you are lacking public getter and setter for userSelected.

Thirdly you are missing the ActionEvent as you have declared a parameterless actionlistener, see Differences between action and actionListener

In order to get you code working you will need to change your xhtml to

<h:form>
  <p:inputText id="name" value="#{usuarioMB.userSelected.name}" />  
  <p:commandButton id="btnSave" value="Salvar" actionListener="#{usuarioMB.save}"/>  
</h:form>

And your managed bean as follows

import javax.faces.event.ActionEvent;
// ...

@ManagedBean
@SessionScoped
public class UsuarioMB implements  Serializable{
  private User userSelected; 

  public void save(ActionEvent event){
     System.out.println(userSelected.getName());
  }

  public User getUserSelected() {
    return userSelected;
  }

  public void setUserSelected(User userSelected) {
    this.userSelected = userSelected;
  }

}
Community
  • 1
  • 1
cheffe
  • 9,345
  • 2
  • 46
  • 57
  • hey guy! thanks for your answer! sorry, i wrote wrong my code in this example.. a managed bean UsuarioMB is UserMB. And the getters and setters already exists in my real code. So, the example that you pass me, not working... continues in the same way.. do you have someone idea? THANKS – Thiago Maltempi Sep 08 '13 at 03:15
  • Please edit your question accordingly instead of leaving it in an invalid state. And in future questions, please do not use Stack Overflow question editor as code editor. Instead, edit (and test!) the problematic code in your real development environment and then copypaste the code *unmodified*. – BalusC Sep 09 '13 at 00:36