提交 JSF2 表单不会在同一页面上重新加载集合
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个快速解决方案,但不是真正的解决方案。重定向
addTitle()
的结果:将以下内容添加到
addTitle()
中:This is a quick fix, but not a real solution. Redirect the result of
addTitle()
:Add the following to
addTitle()
: