JSF ViewScoped bean未完全构建

时间:2019-05-04 14:52:08

标签: jpa jsf managed-bean

我有一个使用DAO的简单JSF CRUD页面+ bean,如下所示:

        <table>
            <tr><td>Postname</td><td><h:inputText value="#{newsworks.newsletter.name}"></h:inputText></td></tr>
            <tr><td>Author</td><td><h:inputText value="#{newsworks.newsletter.subject}"></h:inputText></td></tr>
            <tr><td>Brief</td><td><h:inputText value="#{newsworks.newsletter.fromaddress}"></h:inputText></td></tr>
            <tr style="display:none"><td>Postmessage(TXT)</td><td><h:inputTextarea rows="30" style="width:540px" value="#{newsworks.newsletter.emailtxtcontent}"></h:inputTextarea></td></tr>
            <tr style="display:none"><td>Postmessage(HTML)</td><td><h:inputTextarea id="classic" rows="30" style="width:540px" value="#{newsworks.newsletter.emailhtmlcontent}"></h:inputTextarea></td></tr>
            <tr>
                <td></td>
                <td>
                    <h:commandLink value="Submit" action="#{newsworks.submit()}" ></h:commandLink>
                    <h:commandLink value="Update" action="#{newsworks.update()}" ></h:commandLink>
                </td>
            </tr>
        </table>                        

        <table>
            <thead>
                <td>ID</td>
                <td>Postname</td>
                <td>Subject</td>
                <td>From</td>
                <td>Emailtype</td>
                <td>Postmessage</td>
                <td>Delete</td>
            </thead>
        <ui:repeat value="#{newsworks.newsletters}" var="pageitem">
            <tr>
                <td><h:outputText value="#{pageitem.name}"></h:outputText></td>
                <td><h:outputText value="#{pageitem.subject}"></h:outputText></td>
                <td><h:outputText value="#{pageitem.fromaddress}"></h:outputText></td>
                <td><h:outputText value=""></h:outputText></td>
                <td><h:outputText value=""></h:outputText></td>
                <td>
                    <h:commandLink value="Delete" action="#{newsworks.delete(pageitem)}" ></h:commandLink>
                    <h:commandLink value="Edit" action="#{newsworks.select(pageitem)}" ></h:commandLink>
                </td>
            </tr>
        </ui:repeat>
        </table>

bean的相关部分如下:

@ManagedBean(name="newsworks")
@ViewScoped
public class NewsWorks implements Serializable {

private static final long serialVersionUID = 1L;
private static final String PERSISTENCE_UNIT_NAME = "rdb";
private static EntityManagerFactory factory;

public Newsletter newsLetter = new Newsletter();

@PostConstruct
public void getAllNewsletters() {
    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();

    Query qa = em.createNativeQuery("select * from Newsletter order by id asc ", model.Newsletter.class);
    List<Newsletter> NewsletterList = (List<Newsletter>) qa.getResultList();

    if(NewsletterList.isEmpty())
        NewsletterList = new ArrayList<Newsletter>();

    setNewsletters(NewsletterList); 
}


public String select(Newsletter bp) {
    setNewsletter(bp);
    return null;
}

public String update() {
    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();

    Newsletter bp = newsLetter;
    Newsletter bolocal = em.find(Newsletter.class, bp.getId());

    em.getTransaction().begin();
    bolocal.setId(bp.getId());
    bolocal.setName(bp.getName());
    bolocal.setSubject(bp.getSubject());
    bolocal.setFromaddress(bp.getFromaddress());
    bolocal.setSendgroup(bp.getSendgroup());

    em.getTransaction().commit();

    setNewsletter(new Newsletter());

    getAllNewsletters();

    return null;
}

    public Newsletter newsLetter = new Newsletter();

public Newsletter getNewsletter() {
    return newsLetter;
}

public void setNewsletter(Newsletter newslet) {
    this.newsLetter = newslet;
}

public List<Newsletter> Newsletters = new ArrayList<Newsletter>();

public List<Newsletter> getNewsletters() {
    return Newsletters;
}

public void setNewsletters(List<Newsletter> Newsletters) {
    this.Newsletters = Newsletters;
}

.....

当调用select()方法时,我得到一个完全构造的Newsletter对象,但是当调用update()方法时,“ newsLetter”成员对象仅部分构造(具体而言,仅.xhtml中引用的成员)当其他页面设置为null时,则可以使用该页面,尤其是'id')。

我的问题,这和javax.faces.PARTIAL_STATE_SAVING有关吗?我也玩过javax.faces.STATE_SAVING_METHOD(即客户端和服务器),我尝试了所有这些设置的排列,但无法使此ViewScoped位正常工作。

任何人都可以发现代码中任何明显的问题或需要进行其他任何更改以完整保留ViewScoped bean吗?

0 个答案:

没有答案