我有这个复合组件:
输入掩码.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:composite="http://xmlns.jcp.org/jsf/composite"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<composite:interface>
<composite:attribute name="value" />
<composite:attribute name="mask" type="java.lang.String" required="true" />
<composite:attribute name="converterId" type="java.lang.String" default="br.edu.ufca.eventos.visao.inputmask.inputMask" />
</composite:interface>
<composite:implementation>
<h:outputScript library="script" name="inputmask.js" target="head" />
<h:inputText id="mascara">
<c:if test="#{cc.getValueExpression('value') != null}">
<f:attribute name="value" value="#{cc.attrs.value}" />
</c:if>
<f:converter converterId="#{cc.attrs.converterId}" />
<f:attribute name="mask" value="#{cc.attrs.mask}" />
</h:inputText>
<h:outputScript target="body">
defineMask("#{cc.clientId}", "#{cc.attrs.mask}");
</h:outputScript>
</composite:implementation>
</html>
在我的最后一个问题中:
尝试以编程方式添加复合组件时出错(“没有为名称定义标签”) https://stackoverflow.com/questions/39511065/error-trying-to-add-composite-component-programmatically-no-tag-was-defined-fo
我收到此错误:
javax.faces.view.facelets.TagException: //C:/wildfly-10/standalone/tmp/eventos.ear.visao.war/mojarra7308315477323852505.tmp @2,127 <j:inputMask.xhtml> Tag Library supports namespace: http://xmlns.jcp.org/jsf/composite/componente, but no tag was defined for name: inputMask.xhtml
当尝试使用以下代码以编程方式添加上述复合组件时:
Map<String, String> attributes = new HashMap<>();
attributes.put("mask", "999.999");
Components.includeCompositeComponent(Components.getCurrentForm(), "componente", "inputMask.xhtml", "a123", attributes);
但我设法以这种方式解决这个问题:
方法的实现组件#includeCompositeComponent来自 OmniFaces 2.4(我使用的版本)是这样的:
public static UIComponent includeCompositeComponent(UIComponent parent, String libraryName, String tagName, String id, Map<String, String> attributes) {
String taglibURI = "http://xmlns.jcp.org/jsf/composite/" + libraryName;
Map<String, Object> attrs = (attributes == null) ? null : new HashMap<String, Object>(attributes);
FacesContext context = FacesContext.getCurrentInstance();
UIComponent composite = context.getApplication().getViewHandler()
.getViewDeclarationLanguage(context, context.getViewRoot().getViewId())
.createComponent(context, taglibURI, tagName, attrs);
composite.setId(id);
parent.getChildren().add(composite);
return composite;
}
因此,我决定尝试一下此方法的早期版本 OmniFaces 中的代码(进行了一些更改,添加了我提供的属性参数):
public static UIComponent includeCompositeComponent(UIComponent parent, String libraryName, String resourceName, String id, Map<String, String> attributes) {
// Prepare.
FacesContext context = FacesContext.getCurrentInstance();
Application application = context.getApplication();
FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
// This basically creates <ui:component> based on <composite:interface>.
Resource resource = application.getResourceHandler().createResource(resourceName, libraryName);
UIComponent composite = application.createComponent(context, resource);
composite.setId(id); // Mandatory for the case composite is part of UIForm! Otherwise JSF can't find inputs.
// This basically creates <composite:implementation>.
UIComponent implementation = application.createComponent(UIPanel.COMPONENT_TYPE);
implementation.setRendererType("javax.faces.Group");
composite.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, implementation);
if (!attributes.isEmpty()) {
ExpressionFactory factory = application.getExpressionFactory();
ELContext ctx = context.getELContext();
for (Map.Entry<String, String> entry : attributes.entrySet()) {
ValueExpression expr = factory.createValueExpression(ctx, entry.getValue(), Object.class);
composite.setValueExpression(entry.getKey(), expr);
}
}
// Now include the composite component file in the given parent.
parent.getChildren().add(composite);
parent.pushComponentToEL(context, composite); // This makes #{cc} available.
try {
faceletContext.includeFacelet(implementation, resource.getURL());
} catch (IOException e) {
throw new FacesException(e);
} finally {
parent.popComponentFromEL(context);
}
return composite;
}
最后错误消失了。复合组件被动态添加到页面中。
但另一个问题又出现了。
添加组件的按钮中的操作或多或少如下所示:
if (Components.findComponent("form:a123") == null)
{
Map<String, String> attributes = new HashMap<>();
attributes.put("value", "#{bean.cpf}");
attributes.put("mask", "999.999.999-99");
includeCompositeComponent(Components.getCurrentForm(), "componente", "inputMask.xhtml", "a123", attributes);
}
如您所见,复合组件仅添加一次。
首次添加组件时,组件中的脚本代码:
<h:outputScript target="body">
defineMask("#{cc.clientId}", "#{cc.attrs.mask}");
</h:outputScript>
已添加到页面。当我在浏览器中可视化 html 源代码时我可以看到它。但在回发时,该脚本代码不再呈现。它不在通用的 html 页面中。这<h:outputScript>
with target="head"
每次都会按预期渲染,但不是这个。
从我的角度来看,即使在页面上的回发上,也许在上述方法中组合组件代码的组装中仍然缺少一些东西来修复脚本代码。我真的不知道。这只是一个猜测。
你知道发生了什么或者缺少什么吗?
---- 更新 1 ----
我想我真的找到了问题的根源。这似乎是 JSF 中的一个错误,与以编程方式包含的复合组件中的脚本相关。
这是我发现的:
我注意到 OmniFaces 中包含我的复合组件的正确代码是这样的:
Components.includeCompositeComponent(Components.getCurrentForm(), "componente", "inputMask", "a123", attributes);
正确的是"inputMask"
, not "inputMask.xhtml"
。但正如我之前告诉过你的,当我使用此代码时,我收到此错误:
Caused by: javax.faces.FacesException: Cannot remove the same component twice: form:a123:j_idt2
所以我怀疑这个id的组件形式:a123:j_idt2是其中之一h:输出脚本存在于复合材料组件中。所以我将复合组件代码更改为:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:composite="http://xmlns.jcp.org/jsf/composite"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<composite:interface componentType="inputMask">
<composite:attribute name="value" />
<composite:attribute name="mask" type="java.lang.String" required="true" />
<composite:attribute name="converterId" type="java.lang.String" default="br.edu.ufca.eventos.visao.inputmask.inputMask" />
</composite:interface>
<composite:implementation>
<h:inputText id="mascara">
<c:if test="#{cc.getValueExpression('value') != null}">
<f:attribute name="value" value="#{cc.attrs.value}" />
</c:if>
<f:converter converterId="#{cc.attrs.converterId}" />
<f:attribute name="mask" value="#{cc.attrs.mask}" />
</h:inputText>
<script type="text/javascript">
defineMask("#{cc.clientId}", "#{cc.attrs.mask}");
</script>
</composite:implementation>
</html>
删除所有对h:输出脚本标签。 (当然,我把输入掩码.js复合组件外部的脚本以使组件继续工作)。
现在,当我运行代码时,组件最终被添加到页面,没有错误。但是,正如我之前对 OmniFaces 早期版本的代码所说的那样,该脚本仍然不会在回发中呈现。 JSF 仅在添加组件时呈现它,并在回发时丢失它。我知道这不是预期的行为。
所以,我问你:你知道我该如何解决这个脚本问题吗?或者至少在这种情况下我可以使用任何解决方法?
先感谢您。
---- 更新 2 ----
我找到了一个解决方法。我在复合组件的支持组件中执行了此操作,并且它有效,脚本始终呈现:
@Override
public void encodeEnd(FacesContext context) throws IOException
{
super.encodeEnd(context);
ResponseWriter writer = context.getResponseWriter();
writer.startElement("script", this);
writer.writeText(String.format("defineMask('%s', '%s');",
getClientId(), getAttributes().get("mask")), null);
writer.endElement("script");
}
但这有点丑陋而且似乎没有必要。同样,如果该组件不是以编程方式包含的,我就不需要支持组件。这似乎是 JSF 中的一个错误。你们中的一些人可以测试并确认这一点吗?我的意思是,测试以编程方式添加的带有脚本的复合组件是否在回发时丢失其脚本。
P.S.:我在用着OmniFaces 2.4 and 莫贾拉 2.2.13.