0

我有 1 Entitywith 1 NamedBean with ConversationScope,我创建了一个jsf form带保存按钮,我的问题是,为什么当我的表单使用@injected 发布我的实体时,实体 bean 的属性(ArticuloController 属性)没有被注入。与 cdi 一起存在类似于@inseam2 的注释 to populate @inject Entity entity

这是我的源代码:

@Entity
@Named("articulo")

public class Articulo implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "articulo_seq")
    @SequenceGenerator(name = "articulo_seq", sequenceName = "articulo_seq")
    private Integer articulo;

    private String descripcion="";

//my action class

@Named("articuloController")
@ConversationScoped

public class ArticuloController implements Serializable {

    @Inject
    private Articulo articulo;

    @Inject
private Messages messages;


@Begin
public String init() {
    messages.info("init conversation");
    //this values is not show in the jsf form
    articulo.setDescripcion("complete descripcion");
    articulo.setPrecio(new BigDecimal(0));
    return null;
}

public String save() {
        //the values of the properties is not the values filled by user, 
        //but is the values filled by init method
        System.out.println("save!" + articulo.getDescripcion()
            + articulo.getPrecio() + " " + id);

    return null;
}

my jsf page is


<f:metadata>
            <f:viewParam name="id" value="#{articuloController.id}" />
            <s:viewAction action="#{articuloController.init}" />
        </f:metadata>
        <h:form>
        <h:panelGrid columns="3" id="panel">


            <h:outputLabel value="Descripcion" />

            <h:inputText value="#{articulo.descripcion}" id="descripcion"
                required="true" label="Descripción" />

            <rich:message for="descripcion" />

            <h:outputLabel value="Precio" />

            <h:inputText value="#{articulo.precio}" id="precio" required="true"
                label="Precio" />

            <rich:message for="precio" />

            <h:commandButton value="Guardar" action="#{articuloController.save}" />

        </h:panelGrid>
</h:form>

更新:我正在使用 seam3 3.1.0.Final

4

1 回答 1

1

以这种方式混合 JPA 和 CDI 是一个非常糟糕的主意。如果你这样做,事情将无法正确保存,而且你到处都会有例外。您需要做的是从实体中删除 @Name 并为其创建生产者。您还需要向生产者添加范围注释,可能请求或对话可以正常工作。

于 2012-12-04T14:10:40.093 回答