面对Seam3

时间:2012-12-03 20:45:56

标签: jboss seam java-ee-6 cdi seam3

我有一个Entity与1 Named Bean ConversationScope,我创建了一个带有保存按钮的jsf form,我的问题是,为什么我的表单是使用@injected发布了我的实体,实体bean的属性(ArticuloController属性)尚未注入。存在与cdi类似于seam2的@in注释的内容,以填充@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

1 个答案:

答案 0 :(得分:1)

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