java - seam EntityHome 操作中的有线组件为 null

标签 java jakarta-ee seam

我有一个自定义的 EntityHome 类。我在连线方法中连接依赖实体,但是当我调用操作(持久化)时,连线组件始终为空。

可能是什么原因,seam gen 生成的类似代码显然可以正常工作。

这是实体类。

round.teeSet 组件在调用 persist 时始终为 null,尽管它显示在 jsf 页面中。

Note: Seam version: 2.2.0.GA Jboss: jboss-5.1.0.GA

我已经覆盖了 persist 方法来记录连线元素的值。

@Name("roundHome")
@Scope(ScopeType.CONVERSATION)
public class RoundHome extends EntityHome<Round>{

    @In(required = false)
    private Golfer currentGolfer;

    @In(create = true)
    private TeeSetHome teeSetHome;

    @Override
    public String persist() {
        logger.info("Persist called");
        if (null != getInstance().getTeeSet() ) {
            logger.info("teeSet not null in persist");
        } else {
            logger.info("teeSet null in persist");
            // wire();
        }
        String retVal =  super.persist();    //To change body of overridden methods use File | Settings | File Templates.
        return retVal;
    }

    @Logger
    private Log logger;

    public void wire() {

        logger.info("wire called");
        TeeSet teeSet = teeSetHome.getDefinedInstance();


        if (null != teeSet) {
            getInstance().setTeeSet(teeSet);
            logger.info("Successfully wired the teeSet instance with color: " + teeSet.getColor());
        }
    }

    public boolean isWired() {
        logger.info("is wired called");
        if(null == getInstance().getTeeSet()) {
            logger.info("wired teeSet instance is null, the button will be disabled !");
            return false;
        }
        else {
            logger.info("wired teeSet instance is NOT null, the button will be enabled !");
            logger.info("teeSet color: "+getInstance().getTeeSet().getColor());
            return true;
        }
    }

    @RequestParameter
    public void setRoundId(Long id) {
        super.setId(id);
    }

    @Override
    protected Round createInstance() {
        Round round = super.createInstance();
        round.setGolfer(currentGolfer);
        round.setDate(new java.sql.Date(System.currentTimeMillis()));
        return round;
    }
}

这里是xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:s="http://jboss.com/products/seam/taglib"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:a="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    template="layout/template.xhtml">

<ui:define name="body">
    <h:form id="roundform">
        <rich:panel>
            <f:facet name="header>">
                #{roundHome.managed ? 'Edit' : 'Add' } Round
            </f:facet>
            <s:decorate id="dateField" template="layout/edit.xhtml">
                <ui:define name="label">Date:</ui:define>
                <rich:calendar id="date" datePattern="dd/MM/yyyy" value="#{round.date}"/>
            </s:decorate>


            <s:decorate id="notesField" template="layout/edit.xhtml">
                <ui:define name="label">Notes:</ui:define>
                <h:inputTextarea id="notes" cols="80" rows="3" value="#{round.notes}" />
            </s:decorate>


            <s:decorate id="totalScoreField" template="layout/edit.xhtml">
                <ui:define name="label">Total Score:</ui:define>
                <h:inputText id="totalScore" value="#{round.totalScore}" />
            </s:decorate>


            <s:decorate id="weatherField" template="layout/edit.xhtml">
                <ui:define name="label">Weather:</ui:define>
                <h:selectOneMenu id="weather" value="#{round.weather}">
                    <s:selectItems var="_weather" value="#{weatherCategories}" label="#{_weather.label}"
                                   noSelectionLabel=" Select " />
                    <s:convertEnum/>
                </h:selectOneMenu>
            </s:decorate>


            <div style="clear: both;">
                <span class="required">*</span> required fields
            </div>
        </rich:panel>

        <div class="actionButtons">
            <h:commandButton id="save" value="Save"
                             action="#{roundHome.persist}"
                             rendered="#{!roundHome.managed}" />
            <!-- disabled="#{!roundHome.wired}"  /> -->

            <h:commandButton id="update" value="Update" action="#{roundHome.update}"
                             rendered="#{roundHome.managed}" />

            <h:commandButton id="delete" value="Delete" action="#{roundHome.remove}"
                             rendered="#{roundHome.managed}" />

            <s:button id="discard" value="Discard changes" propagation="end"
                      view="/Round.xhtml" rendered="#{roundHome.managed}" />

            <s:button id="cancel" value="Cancel" propagation="end"
                      view="/#{empty roundFrom ? 'RoundList' : roundFrom}.xhtml"
                      rendered="#{!roundHome.managed}" />

        </div>

        <rich:tabPanel>
            <rich:tab label="Tee Set">
                <div class="association">
                    <h:outputText value="Tee set not selected" rendered="#{round.teeSet == null}" />
                    <rich:dataTable var="_teeSet" value="#{round.teeSet}" rendered="#{round.teeSet != null}">
                        <h:column>
                            <f:facet name="header">Course</f:facet>#{_teeSet.course.name}
                        </h:column>
                        <h:column>
                            <f:facet name="header">Color</f:facet>#{_teeSet.color}
                        </h:column>
                        <h:column>
                            <f:facet name="header">Position</f:facet>#{_teeSet.pos}
                        </h:column>
                    </rich:dataTable>
                </div>
            </rich:tab>
        </rich:tabPanel>
    </h:form>
</ui:define>
</ui:composition>

最佳答案

我想您有一个 ScopeType.CONVERSATION (RoundHome) 实例,但没有启用任何长时间运行的对话。因此,您可能会有一个临时对话,它会一直存在到渲染响应阶段。

当您提交表单时,因为您有一个临时对话,您只需填充您提交的内容。它解释了为什么您的 TeeSet 为空。 (您提交的表单不包含对 Tee 实例的任何引用)

如果您真的希望您的 TeeSet 保存到下一次提交,您必须在显示之前启用一个长时间运行的对话并在提交表单后禁用。

只是一个建议:默认情况下,每个 Home 实例都是 ScopeType.CONVERSATION - 从 Home 继承。所以你可以如下设置你的 RoundHome

@Name("roundHome")
public class RoundHome extends EntityHome<Round> {

    ...

}

关于java - seam EntityHome 操作中的有线组件为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2729746/

相关文章:

java - List<Dog> 是 List<Animal> 的子类吗?为什么 Java 泛型不是隐式多态的?

java - 服务器上的 Resteasy 客户端字节数组为空

java - 如何使用 JPA 2.0 急切加载惰性字段?

seam - Seam3 和 Weld CDI 是一回事吗?

用户闲置 10 分钟后,Java 应用程序使屏幕显示无响应

java - 通过Java Spring注解替换字符串

java.lang.NoSuchMethodException : org. hibernate.validator.ClassValidator 缝 weblogic 10.3

jakarta-ee - 带有 JBoss 7.1 的 SEAM2 应用程序 - ClassNotFoundException

java - 编写方法时传递参数数量与传递映射哪个更好?

spring - 如何使用 Spring 3 的 SessionLocaleResolver 在 JSP 中获取访问者的语言环境