关于如何组合CDI bean的疑问:SessionScoped和RequestScoped

时间:2012-02-11 09:26:19

标签: java session java-ee jsf-2 java-ee-6

我的目标是展示一个具有创建,检索和更新功能的jsf页面。 我决定为每个操作创建不同的CDI bean和不同的复合组件,然后将它们放在页面中。

到目前为止一切顺利,但我刚刚结束,我发现了一个非常令人困惑的错误,我不知道如何解决它:

执行CREATE操作的CDI bean工具是@RequestScoped bean,因此输入字段会在请求后自行清除它们。(参见下图)

enter image description here

我对它没有任何问题(只是警告我无法摆脱),它运作正常。

我创建的下一个小工具是一个数据表,也可以编辑数据。要做到这一点,我需要使用@SessionScopped CDI bean。(见下图)

enter image description here

问题出现了: 呈现页面时,@ SessionConcoped bean将数据缓存在会话中,但是当插入新数据时,使用@RequestScoped bean,数据将转到数据库,但数据表不会显示新输入的值,因为不是在会议中。

那我该怎么办? 在这里,我将向您展示两个豆类:

THE CREATE BEAN

@Named("subjectControllerCreate")
@RequestScoped
public class SubjectControllerCreate implements Serializable {

    private Subject currentSubject;
    @EJB
    private SubjectFacade ejbFacade;

    //INITIALIZATION
    public SubjectControllerCreate() {
        currentSubject = new Subject();
    }


    //CREATE


       public String create() {
            try {         
                    currentSubject.setCreationDate(new Date());
                    getSubjectFacade().create(currentSubject);//Adds the current subject to the database!
                    JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("SubjectCreated"));
                    return "";//Can perform a redirect here if we want
                //}
                //return null;
            } catch (Exception e) {
                JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
                return null;
            }
        }

THE UPDATE BEAN

@Named("subjectControllerUpdate")
@SessionScoped
public class SubjectControllerUpdate implements Serializable {

    //Using DataModel<Subject> instead of List<Subject> is necessary in order to be able to get the current row.
    private DataModel<Subject> subjects;    
    @EJB
    private SubjectFacade ejbFacade;

    //INITIALIZATION   
    @PostConstruct
    public void init() {
       subjects = new ListDataModel<Subject>(getSubjectFacade().findAll());
    }

    //RETRIEVE
    public DataModel<Subject> retrieve() {
        return subjects;
    }

    //UPDATE
    public void update() {
        getSubjectFacade().edit(subjects.getRowData());
    }

    //HELP METHODS
    //RETURN THE FACADE FOR DATA MANIPULATION(Best practice)
    private SubjectFacade getSubjectFacade() {
        return ejbFacade;
    }

    //GETTERS AND SETTERS
    public DataModel<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(DataModel<Subject> subjects) {
        this.subjects = subjects;
    }        
}

当检测到“创建”对话框关闭时,是否可以使数据表发送一些ajax请求,以获取其余新输入的数据? 如果是,我怎么能这样做?

这是我的数据表的标记:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <h:form>
        <p:dataTable id="allSubjects" var="subject" value="#{subjectControllerUpdate.subjects}" paginator="true" rows="7" >
            <p:ajax event="rowEdit" listener="#{subjectControllerUpdate.update()}"/>
            <p:column headerText="Name" sortBy="#{subject.name}" style="width:200px" >
                <p:cellEditor>  
                    <f:facet name="output">  
                        <h:outputText value="#{subject.name}"/>  
                    </f:facet>  
                    <f:facet name="input">  
                        <p:inputText value="#{subject.name}" style="width:100%"/>  
                    </f:facet>  
                </p:cellEditor>  
            </p:column>

            <p:column sortBy="#{subject.description}" headerText="Description">               
                <p:cellEditor>  
                    <f:facet name="output">  
                        <h:outputText value="#{subject.description}"/>  
                    </f:facet>  
                    <f:facet name="input">  
                        <p:inputText value="#{subject.description}" style="width:100%"/>  
                    </f:facet>  
                </p:cellEditor>    
            </p:column>

            <p:column sortBy="#{subject.credits}" headerText="Credits" style="width:50px">
                <p:cellEditor>  
                    <f:facet name="output">  
                        <h:outputText value="#{subject.credits}"/>  
                    </f:facet>  
                    <f:facet name="input">  
                        <p:inputText value="#{subject.credits}" style="width:100%"/>  
                    </f:facet>  
                </p:cellEditor>
            </p:column>

            <p:column headerText="Options" style="width:50px">                   
                <p:rowEditor />
            </p:column>         
        </p:dataTable>
    </h:form>    

</html>

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

你不能只将@SessionScoped bean注入@RequestScoped bean,点击创建时,调用@SessionScoped bean中的方法刷新吗?