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()
被称为 AFTER getManualTitles()
。实际上getManualTitles()
是调用了例如 10 次,然后addTitle()
,然后再调用了两次该getManualTitles()
方法。这让我觉得这是某种并行执行,导致我的页面只显示 12 条旧记录而不是 13 条。我必须重新加载页面,然后显示 13。
更新:现在缓存列表。问题仍然没有解决。
为什么?我怎样才能解决这个问题?