如何将复合组件 ID 传递给方法?

How to pass the composite component ID to a method?

我想将当前复合组件的 ID 传递给一个 Java 方法。
我用这个试过了

value="#{testDashView.addCard(composite)}"
value="#{testDashView.addCard(@composite)}"
value="#{testDashView.addCard(this)}"

但每次字符串为空或出现错误时。

Java方法:

public void addCard(String key) {
        System.out.println(key);
        cards.put(key, "");
    }

我收到这个错误:

14:04:40,002 ERROR [io.undertow.request] (default task-15) UT005023: Exception handling request to /crm-mobile/testDashboard.xhtml: javax.servlet.ServletException: /resources/cs/component.xhtml @12,83 value="#{testDashView.addCard(@composite)}" Error Parsing: #{testDashView.addCard(@composite)}
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
    at io.undertow.servlet.handlers.ServletInitialHandler.access0(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:138)
    at io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:135)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create[=12=](SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=12=](UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=12=](UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=12=](UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=12=](UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=12=](UndertowDeploymentInfoService.java:1508)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
    at io.undertow.servlet.handlers.ServletInitialHandler.access[=12=]0(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleRequest(ServletInitialHandler.java:104)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:326)
    at io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:812)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.faces.view.facelets.TagAttributeException: /resources/cs/component.xhtml @12,83 value="#{testDashView.addCard(@composite)}" Error Parsing: #{testDashView.addCard(@composite)}
    at com.sun.faces.facelets.tag.TagAttributeImpl.getValueExpression(TagAttributeImpl.java:428)
    at com.sun.faces.facelets.tag.TagAttributeImpl.getValueExpression(TagAttributeImpl.java:378)
    at com.sun.faces.facelets.tag.jsf.ValueHolderRule$DynamicValueExpressionMetadata.applyMetadata(ValueHolderRule.java:129)
    at com.sun.faces.facelets.tag.MetadataImpl.applyMetadata(MetadataImpl.java:81)
    at javax.faces.view.facelets.MetaTagHandler.setAttributes(MetaTagHandler.java:129)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.setAttributes(DelegatingMetaTagHandler.java:102)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.doNewComponentActions(ComponentTagHandlerDelegateImpl.java:491)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:178)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at com.sun.faces.facelets.tag.composite.ImplementationHandler.apply(ImplementationHandler.java:81)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:312)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:371)
    at com.sun.faces.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:326)
    at com.sun.faces.facelets.tag.jsf.CompositeComponentTagHandler.applyCompositeComponent(CompositeComponentTagHandler.java:387)
    at com.sun.faces.facelets.tag.jsf.CompositeComponentTagHandler.applyNextHandler(CompositeComponentTagHandler.java:188)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:202)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:202)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at com.sun.faces.facelets.tag.ui.DefineHandler.applyDefinition(DefineHandler.java:106)
    at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:206)
    at com.sun.faces.facelets.impl.DefaultFaceletContext$TemplateManager.apply(DefaultFaceletContext.java:395)
    at com.sun.faces.facelets.impl.DefaultFaceletContext.includeDefinition(DefaultFaceletContext.java:366)
    at com.sun.faces.facelets.tag.ui.InsertHandler.apply(InsertHandler.java:111)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:202)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:312)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:371)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:350)
    at com.sun.faces.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:199)
    at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:174)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
    at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:161)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:1006)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:99)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    ... 39 more
Caused by: javax.el.ELException: Error Parsing: #{testDashView.addCard(@composite)}
    at com.sun.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:213)
    at com.sun.el.lang.ExpressionBuilder.build(ExpressionBuilder.java:237)
    at com.sun.el.lang.ExpressionBuilder.createValueExpression(ExpressionBuilder.java:295)
    at com.sun.el.ExpressionFactoryImpl.createValueExpression(ExpressionFactoryImpl.java:112)
    at org.jboss.weld.util.el.ForwardingExpressionFactory.createValueExpression(ForwardingExpressionFactory.java:53)
    at org.jboss.weld.el.WeldExpressionFactory.createValueExpression(WeldExpressionFactory.java:48)
    at org.jboss.weld.util.el.ForwardingExpressionFactory.createValueExpression(ForwardingExpressionFactory.java:53)
    at org.jboss.weld.el.WeldExpressionFactory.createValueExpression(WeldExpressionFactory.java:48)
    at com.sun.faces.facelets.tag.TagAttributeImpl.getValueExpression(TagAttributeImpl.java:412)
    ... 89 more
Caused by: com.sun.el.parser.ParseException: Encountered "@" at line 1, column 24.
Was expecting one of:
    "{" ...
    <INTEGER_LITERAL> ...
    <FLOATING_POINT_LITERAL> ...
    <STRING_LITERAL> ...
    "true" ...
    "false" ...
    "null" ...
    "(" ...
    ")" ...
    "[" ...
    "!" ...
    "not" ...
    "empty" ...
    "-" ...
    <IDENTIFIER> ...

    at com.sun.el.parser.ELParser.generateParseException(ELParser.java:2963)
    at com.sun.el.parser.ELParser.jj_consume_token(ELParser.java:2845)
    at com.sun.el.parser.ELParser.MethodArguments(ELParser.java:1446)
    at com.sun.el.parser.ELParser.DotSuffix(ELParser.java:1333)
    at com.sun.el.parser.ELParser.ValueSuffix(ELParser.java:1306)
    at com.sun.el.parser.ELParser.Value(ELParser.java:1249)
    at com.sun.el.parser.ELParser.Unary(ELParser.java:1219)
    at com.sun.el.parser.ELParser.Multiplication(ELParser.java:981)
    at com.sun.el.parser.ELParser.Math(ELParser.java:901)
    at com.sun.el.parser.ELParser.Concatenation(ELParser.java:857)
    at com.sun.el.parser.ELParser.Compare(ELParser.java:669)
    at com.sun.el.parser.ELParser.Equality(ELParser.java:563)
    at com.sun.el.parser.ELParser.And(ELParser.java:507)
    at com.sun.el.parser.ELParser.Or(ELParser.java:451)
    at com.sun.el.parser.ELParser.Choice(ELParser.java:409)
    at com.sun.el.parser.ELParser.Assignment(ELParser.java:243)
    at com.sun.el.parser.ELParser.SemiColon(ELParser.java:181)
    at com.sun.el.parser.ELParser.Expression(ELParser.java:174)
    at com.sun.el.parser.ELParser.DeferredExpression(ELParser.java:112)
    at com.sun.el.parser.ELParser.CompositeExpression(ELParser.java:40)
    at com.sun.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:182)
    ... 97 more

当前的复合组件实现是在 #{cc} 可用的 EL 中。它引用了 UIComponent, which has getId() and getClientId() 方法的实例。

换句话说,应该这样做:

value="#{testDashView.addCard(cc.id)}"

与具体问题无关,这种做法很臭。很可能 #{testDashView} 应该是一个支持组件,或者你不应该首先使用复合组件。

另请参阅:

  • How to implement a dynamic list with a JSF 2.0 Composite Component?
  • When to use <ui:include>, tag files, composite components and/or custom components?