简体   繁体   English

如何正确地将方法从jsf调用到ManagedBean

[英]How to correctly call method from jsf to ManagedBean

well I'm learning jsf/hibernate and I'm having trouble understanding how to correctly call methods to my ManagedBean its a simple CRUD so I need to pass vía the jsf form a Ciclista object, but since the inputs are Ciclista attributes how do I pass the Object ? 我正在学习jsf/hibernate并且在理解如何正确地向我的ManagedBean调用方法(它是一个简单的CRUD)时遇到了麻烦,因此我需要将jsf传递给Ciclista对象,但是由于输入是Ciclista属性,我该如何通过对象? (how to create instance of it) here's the code: (如何创建实例)下面是代码:

@Override
public String create(Ciclista c) {
    Session s = sFac.openSession();
    s.beginTransaction();
    s.save(c);
    s.getTransaction().commit();
    s.close();
    return "Administrador?faces-redirect=true";
}

and jsf form: 和jsf形式:

   <h:form class="form">
        <div id="input-wrapper">
            <h:inputText class="inputs" value="#{ciclistaBeanDB.nombre}" />
            <h:inputText class="inputs" value="#{ciclistaBeanDB.nacionalidad}" />
            <h:inputText class="inputs" value="#{ciclistaBeanDB.equipo}" />
        </div>
        <h:commandButton class="btn" action="#{ciclistaBeanDB.create()}" value="Create" />
   </h:form>

I get error: method not found because i don't pass the Ciclista c object in crear(//here) since i don't have the object per se, just its attributes how do I proceed ?? 我收到错误消息:未找到方法,因为我本身没有对象,所以我没有通过crear(//here)Ciclista c对象,只是它的属性该如何进行?

You are getting the MethodNotFoundException , because you wrote crear instead of Create . 您将获得MethodNotFoundException ,因为您写的是crear而不是Create

But the right way to do it, would be to call a save() method without arguments, which is responsible to assemble your object you want to persist. 但是正确的方法是调用不带参数的save()方法,该方法负责汇编您要保留的对象。

Since you are already binding your input fields to bean properties, all you would need to do is, use a method like this: 由于您已经将输入字段绑定到bean属性,因此您所要做的就是使用如下方法:

public String save(){
   Ciclista c = new Ciclista();
   c.setNombre(this.nombre);
   c.setNacionalidad(this.nacionalidad);
   c.setEquipo(this.equipo);

   create(c);
   return "Administrador?faces-redirect=true";
}

your commandbutton now should target the save() method of your bean. 现在,您的命令按钮应该以bean的save()方法为目标。 The input values in the form will be inserted into your baking bean properties and the save() method would assemble the instance and finally calls your persist-method. 表单中的输入值将插入到您的烘焙bean属性中,并且save()方法将组装实例并最终调用您的persist-method。

<h:commandButton class="btn" action="#{ciclistaBeanDB.save}" value="Create" />

Usually you should not call the DatabaseBean directly, but kind of a Controller - or how ever one would like to call it. 通常,您不应该直接调用DatabaseBean ,而应该是一种Controller -或人们希望如何调用它。 This Bean then should utilize your database-service which should only be responsible for CRUD-Functionality. 然后,这个Bean应该利用您的数据库服务,该服务仅负责CRUD功能。

At least for big applications it would be a mess having the databasemethods and other (view-related) methods mixed in one class. 至少对于大型应用程序而言,将数据库方法和其他(与视图相关的)方法混合在一类中将是一团糟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM