提交 JSF2 表单不会在同一页面上重新加载集合

发布于 2024-12-02 08:16:14 字数 2607 浏览 0 评论 0原文

JSF 2.0 (mojarra) 应用程序。我有一个非常简单的表单来添加项目

<h:form>
    #{msg['add custom title']}:<br />
    <table>
        <tr>
            <td>#{msg['heaading']}:</td>
            <td><h:inputText value="#{titlesBean.title.heading}"></h:inputText></td>
        </tr>
        <tr>
                          ...
        </tr>
    </table>
    <h:commandButton action="#{titlesBean.addTitle}" value="#{msg['g.save']}" />
</h:form>

然后在同一页面上我有一个已添加的所有项目的列表:

<h:dataTable id="manualTitlesForm" value="#{titlesBean.manualTitles}" var="title" border="1" cellspacing="0">
    <h:column>
        <f:facet name="header">#{msg['heaading']}</f:facet>
        #{title.heading}
    </h:column>
            ...
    <h:column>
        <f:facet name="header">#{msg['actions']}</f:facet>
        <h:form>
            <h:commandButton action="#{titlesBean.editManualTitle(title)}" value="#{msg['g.edit']}" />
            <h:commandButton action="#{titlesBean.deleteManualTitle(title.id)}" value="#{msg['g.delete']}" />
        </h:form>
    </h:column>
</h:dataTable>

bean 代码中的代码非常简单:

@Controller
@Scope(Scopes.REQUEST)
public class TitlesBean {

    private List<JTitle> manualTitles;


    @PostConstruct
    private void init() {
        this.manualTitles = titlesManager.getManualTitles();
    }

    public String addTitle() {
        title.setCreated(new Date());
        title.setManual(true);
        try {
            titlesManager.addTitle(title);
            title = new JTitle();// this is added, delete from the variable. only if no exception though !!!
            UserMessagesBean.addMessage("saved");
        } catch (Exception e) {
            UserMessagesBean.setValidationException(e.getMessage());//different exception added
        }
        return null;
    }

    public List<JTitle> getManualTitles() {
        return manualTitles;
    }
    }

现在的问题是 getManualTitles() 被调用的次数与我拥有的标题数量一样多,这会导致例如对数据库的 12 次调用,而不是 1 次。为什么会发生这种情况超出了我的理解。我可以通过在 bean 中缓存手册标题来解决这个问题。这不是我的主要问题。

问题在于 addTitle()getManualTitles() 之后调用。事实上,getManualTitles() 被调用了例如 10 次,然后是 addTitle(),然后是 getManualTitles() 方法两次。这让我认为这是某种并行执行,导致我的页面仅显示 12 条旧记录而不是 13 条。我必须重新加载页面,然后显示 13 条。

更新:现在缓存列表。问题仍然没有解决。

为什么?我该如何解决这个问题?

JSF 2.0 (mojarra) application. I have a very trivial form for adding an item

<h:form>
    #{msg['add custom title']}:<br />
    <table>
        <tr>
            <td>#{msg['heaading']}:</td>
            <td><h:inputText value="#{titlesBean.title.heading}"></h:inputText></td>
        </tr>
        <tr>
                          ...
        </tr>
    </table>
    <h:commandButton action="#{titlesBean.addTitle}" value="#{msg['g.save']}" />
</h:form>

And then on the same page I have a list of all the items already added:

<h:dataTable id="manualTitlesForm" value="#{titlesBean.manualTitles}" var="title" border="1" cellspacing="0">
    <h:column>
        <f:facet name="header">#{msg['heaading']}</f:facet>
        #{title.heading}
    </h:column>
            ...
    <h:column>
        <f:facet name="header">#{msg['actions']}</f:facet>
        <h:form>
            <h:commandButton action="#{titlesBean.editManualTitle(title)}" value="#{msg['g.edit']}" />
            <h:commandButton action="#{titlesBean.deleteManualTitle(title.id)}" value="#{msg['g.delete']}" />
        </h:form>
    </h:column>
</h:dataTable>

The code in the bean code is super simple:

@Controller
@Scope(Scopes.REQUEST)
public class TitlesBean {

    private List<JTitle> manualTitles;


    @PostConstruct
    private void init() {
        this.manualTitles = titlesManager.getManualTitles();
    }

    public String addTitle() {
        title.setCreated(new Date());
        title.setManual(true);
        try {
            titlesManager.addTitle(title);
            title = new JTitle();// this is added, delete from the variable. only if no exception though !!!
            UserMessagesBean.addMessage("saved");
        } catch (Exception e) {
            UserMessagesBean.setValidationException(e.getMessage());//different exception added
        }
        return null;
    }

    public List<JTitle> getManualTitles() {
        return manualTitles;
    }
    }

Now the problem is that getManualTitles() is called as many times as the number of titles I have, which causes for example 12 calls to the DB, instead of one. Why is this happening is beyond my understanding. I can fix this with caching the manual titles in the bean. This is not my main problem.

The problem is that addTitle() is called AFTER getManualTitles(). In fact getManualTitles() is called for example 10 times, then addTitle(), then two more times the getManualTitles() method. This makes me think that this is some kind of parallel execution which causes my page to show only the 12 old records instead of 13. I have to reload the page, then 13 is shown.

UPDATED: now caches the list. Problem still not solved.

WHY? How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满地尘埃落定 2024-12-09 08:16:14

这是一个快速解决方案,但不是真正的解决方案。重定向 addTitle() 的结果:

将以下​​内容添加到 addTitle() 中:

    ...
    FacesContext.getCurrentInstance().getExternalContext()
        .redirect("manualTitles.jsf");
    return null;
}

This is a quick fix, but not a real solution. Redirect the result of addTitle():

Add the following to addTitle():

    ...
    FacesContext.getCurrentInstance().getExternalContext()
        .redirect("manualTitles.jsf");
    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文