0

我正在使用 jsf + primefaces 3.5。而且我的按钮没有在我的托管 bean 中调用一种方法。

我有这个 xhtml:

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

我的托管bean是:

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

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


    }
}

最奇怪的是,如果我删除 ,该方法被调用!

如果我在 p:commandButton "imediate = true" 中放置一个属性,则调用该方法,但是,信息(userSelected.name)为空!

非常感谢 :)

4

2 回答 2

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:

于 2013-09-09T00:35:58.270 回答
0

您尚未为 managedbean 命名UsuarioMB。因此它将被命名为usuarioMB.

@ManagedBean – 将此 bean 标记为具有在 name 属性中指定的名称的托管 bean。如果未指定 @ManagedBean 中的 name 属性,则托管 bean 名称将默认为完全限定类名的类名部分。

在此博客中阅读有关它的更多信息:http: //mkblog.exadel.com/2009/08/learning-jsf2-managed-beans/

其次,如果您上面的代码是完整的,那么您缺少公共 getter 和 setter for userSelected.

第三,您缺少ActionEvent声明无参数动作侦听器的,请参阅 Actions and actionListener 之间的差异

为了让您的代码正常工作,您需要将您的 xhtml 更改为

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

你的托管bean如下

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;
  }

}
于 2013-09-07T21:58:03.120 回答