JSF 2.0:以编程方式添加UIComponent时,@ ResourceDependency不起作用 [英] JSF 2.0: @ResourceDependency does not work when adding UIComponent programmatically

查看:110
本文介绍了JSF 2.0:以编程方式添加UIComponent时,@ ResourceDependency不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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被包含在页面中,myComponent2.css没有被包含在页面中.

功能?漏洞?配置问题?

是否可以通过编程方式添加资源以解决此问题?

正在运行Mojarra 2.0.2

解决方案

我知道这是10个月前问到的,但我一直面临着同样的问题.这里的资源问题是由于您使用"new"实例化子组件而引起的.相反,您应该使用context.getApplication().createComponent("MyComponentType"), "MyComponentType"是您在@FacesComponent批注中指定为值的任何值.应用程序在创建组件时(而不是在渲染时)解析注释.使用new会使应用程序失去处理注释的机会.不幸的是,这实际上并不能解决问题,但应该不会解决.

如果添加:

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"));
    }
}

在您的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

不幸的是,它们仍然永远不会被渲染,就好像该组件的视图根目录与其最终渲染的页面不同.在报告任何错误之前,我仍在进一步研究.我目前正在运行mojarra 2.0.6,我可以在2.2上尝试一下,看看问题是否已解决.

更新:已在Mojarra 2.1.3和2.2-SNAPSHOT上进行了测试.它也不起作用.我已在Mojarra问题跟踪器中添加了问题

再次更新::Mojarra的人们告诉我,encodeBegin并不是尝试添加组件的地方.他们向我指出了此博客帖子,描述了如何安全"地进行操作.

@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.

Feature? Bug? Configuration issue?

It there programmatic way to add resources to maybe workaround this?

Running Mojarra 2.0.2

解决方案

I know this was asked 10 months ago, but I've been facing this same issue. Your resource problem here is caused by the fact that you are using "new" to instantiate your child component. Instead, you should use context.getApplication().createComponent("MyComponentType"), "MyComponentType" being whatever you specified as the value in @FacesComponent annotation. 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. 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):

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.

Update: Tested on Mojarra 2.1.3 and 2.2-SNAPSHOT. It does not work on either. I have added an issue to the Mojarra Issue Tracker

Update Again: The people over at Mojarra have informed me that the encodeBegin is not the place to try adding components. They have pointed me to this blog posting, which describes how to "safely" do it.

这篇关于JSF 2.0:以编程方式添加UIComponent时,@ ResourceDependency不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆