简体   繁体   English

JSF 2.0:以编程方式添加UIComponent时,@ ResourceDependency不起作用

[英]JSF 2.0: @ResourceDependency does not work when adding UIComponent programmatically

@ResourceDependency(library = "component/myComponent", name = "myComponent1.css")
public class MyComponent1 extends UIComponentBase {

    public void encodeBegin(FacesContext context) throws IOException {
        MyComponent2 comp2 = new MyComponent2();
        getChildren().add(comp2);
    }

}

@ResourceDependency(library = "component/myComponent", name = "myComponent2.css")
public class MyComponent2 extends UIComponentBase {

    // ...

}

myComponent1.css gets included into page, myComponent2.css does not. myComponent1.css包含在页面中, myComponent2.css不包含在页面中。

Feature? 特征? Bug? 错误? Configuration issue? 配置问题?

It there programmatic way to add resources to maybe workaround this? 是否有编程方式添加资源以解决此问题?

Running Mojarra 2.0.2 运行Mojarra 2.0.2

I know this was asked 10 months ago, but I've been facing this same issue. 我知道这是10个月前提出的,但我一直面临着同样的问题。 Your resource problem here is caused by the fact that you are using "new" to instantiate your child component. 这里的资源问题是由于您使用“ new”实例化子组件而引起的。 Instead, you should use context.getApplication().createComponent("MyComponentType") , "MyComponentType" being whatever you specified as the value in @FacesComponent annotation. 相反,您应该使用context.getApplication().createComponent("MyComponentType") ,“ MyComponentType”是您在@FacesComponent批注中指定的值。 The application parses the annotations while it creates the component, not when it is rendering. 应用程序在创建组件时(而不是在渲染时)解析注释。 Using new deprives the application from it's opportunity to handle the annotations. 使用new会使应用程序失去处理注释的机会。 Unfortunately, this doesn't actually resolve the issue, it should, but it doesn't. 不幸的是,这实际上并不能解决问题,但应该不会解决。

If you add: 如果添加:

UIComponent headFacet = context.getViewRoot().getFacet("javax_faces_location_HEAD");
if (headFacet == null) {
    System.out.println("No Head Facet");
} else {
    System.out.println("Head Children: " + headFacet.getChildCount());
    for (UIComponent c : headFacet.getChildren()) {
        System.out.println(c.getRendererType());
        System.out.println(c.getAttributes().get("name"));
    }
}

to your encodeBegin method you will be able to see that the Resources have actually been added (example adding the PrimeFaces FileUpload as a child): 到您的encodeBegin方法中,您将能够看到实际上已经添加了资源(例如将PrimeFaces FileUpload作为子级添加):

INFO: Head Children: 4
INFO: javax.faces.resource.Stylesheet
INFO: fileupload/fileupload.css
INFO: javax.faces.resource.Script
INFO: jquery/jquery.js
INFO: javax.faces.resource.Script
INFO: core/core.js
INFO: javax.faces.resource.Script
INFO: fileupload/fileupload.js

unfortunately, they still never get rendered, it is as if the component has a different view root than the page it is ultimately rendered on. 不幸的是,它们仍然永远不会被渲染,就好像该组件的视图根目录与其最终渲染的页面不同。 I'm still looking into this further before report any bugs. 在报告任何错误之前,我仍在进一步研究。 I am currently running mojarra 2.0.6, I may try it on 2.2 to see if the issue is resolved. 我当前正在运行mojarra 2.0.6,我可以在2.2上尝试一下,看看问题是否已解决。

Update: Tested on Mojarra 2.1.3 and 2.2-SNAPSHOT. 更新:在Mojarra 2.1.3和2.2-SNAPSHOT上测试。 It does not work on either. 它也不起作用。 I have added an issue to the Mojarra Issue Tracker 我已将一个问题添加到Mojarra问题跟踪器

Update Again: The people over at Mojarra have informed me that the encodeBegin is not the place to try adding components. 再次更新: Mojarra的人们告诉我,encodeBegin并不是尝试添加组件的地方。 They have pointed me to this blog posting, which describes how to "safely" do it. 他们向我指出了此博客文章,其中描述了如何“安全地”做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM