有一个应用程序范围的bean。
@Named
@ApplicationScoped
public class Bean {
@Inject
private Service service;
private Entity entity; // Getter.
// Entity is periodically fetched by EJB timers on the server side
// which when fetched notifies associated clients through WebSockets.
// Clients then update themselves by sending an AJAX request.
// All of these things collectively form a different chapter.
// Just that update() needs to be invoked, when a client sends a synchronous GET request
public Bean() {}
@PostConstruct
private void init() {
consume();
}
private void consume() {
entity = service.getEntity();
}
public void update() {
consume();
System.out.println("update called.");
}
}
主bean模板中包含的页面访问此bean,如下所示(West.xhtml
):
<html lang="#{localeBean.language}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:form>
<!-- This is merely a fake attempt to show something like this is expected. -->
<ui:define name="metaData">
<f:metadata>
<f:viewAction action="{bean.update}"/>
</f:metadata>
</ui:define>
<h:outputText value="#{bean.entity.field}"/>
</h:form>
</html>
使用主模板中的update()
违反其语义,尝试调用<f:viewAction>
方法是错误的。
为了实现此功能,需要在单个模板客户端上重复<f:viewAction>
,这使得难以维护,尤其是在需要更改某些内容时。
是否有可能避免在每个模板客户端上的<f:viewAction>
重复播放?
母版页模板如下所示。这不是必需的。只需假设在以下主模板(<ui:include src=".."/>
)中使用/WEB-INF/templates/Template.xhtml
包含上述页面。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view locale="..." ...>
<ui:insert name="metaData"></ui:insert>
<h:head>
<title><ui:insert name="title">Default Title</ui:insert></title>
</h:head>
<h:body>
<h:panelGroup layout="block">
<ui:insert name="contentBar">
<!-- The file given above is included here - West.xhtml. -->
<ui:include src="/WEB-INF/template/contents/West.xhtml"/>
</ui:insert>
</h:panelGroup>
<ui:insert name="content">Default contents</ui:insert>
<!--Other content bars.-->
</h:body>
</f:view>
</html>
<f:viewAction>
需要放在与以下内容相关的模板客户端上。
<ui:composition template="/WEB-INF/templates/Template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:define name="title">Page Title</ui:define>
<ui:define name="metaData">
<f:metadata>
<f:viewAction action="#{bean.update}"/>
</f:metadata>
</ui:define>
<ui:define name="content">
<!--Main contents-->
</ui:define>
</ui:composition>
等等,<f:viewAction>
需要在每个这样的模板客户端上重复,以便它执行连贯的任务。
有没有办法在一个地方恰当地声明<f:viewAction>
,这样就不需要在每个相关的模板客户端上重复它了?