xhtml表单数据如何传递给托管bean?

时间:2011-12-11 13:35:10

标签: java jsf jpa persistence

When/where do entities get created on a Java EE web application?

相关

那么表单数据如何传递给托管bean?在实体获得实例化之后?就是这样,我似乎无法将数据从表单传递给托管bean。当我试图坚持我的实体时,错误告诉我一切都是空的。

1 个答案:

答案 0 :(得分:0)

以防万一有人会遇到我所处的混淆,这就是我对托管bean所做的。我的猜测是,它是在第一次访问输入字段时创建的。

@ManagedBean

@RequestScoped public class untitledController实现Serializable {

@EJB
UntitledFacade facade;
Untitled untitled;

/** Creates a new instance of untitledController */
public untitledController() {
}

@PostConstruct
public void init() {
    // create the entity object to be used 
    untitled = new Untitled();
}

public Untitled getSelected() {
    if (untitled == null) {
        System.out.println("created a new object");
        return new Untitled();
    } else {
        System.out.println("will return old object");
        return untitled;
    }
}
public String create() throws Exception {
    try {
        System.out.println("will persist object");
        System.out.println("and your object is" + untitled);
        facade.create(untitled);
        return "success";
    } catch (Exception e){
        System.out.println("fail at persisting the entity");
        e.printStackTrace();
        return "fail";
    }
}

}