Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 JSF,复合组件:使用默认属性值作为参数的方法调用_Jsf 2_Composite Component - Fatal编程技术网

Jsf 2 JSF,复合组件:使用默认属性值作为参数的方法调用

Jsf 2 JSF,复合组件:使用默认属性值作为参数的方法调用,jsf-2,composite-component,Jsf 2,Composite Component,我是JSF新手,还在学习。我试图寻找下面描述的我的具体问题的解决方案,但什么也找不到。如果是因为我在寻找错误的东西,请给我指出正确的方向,但希望这是一个尚未得到回答的问题,一个答案可以让每个人受益 下面的例子说明了我遇到的问题。该示例被简化为关注问题,并隐藏发生问题的实际项目的复杂性 考虑以下页面/类: /resources/test/custom.xhtml /test/CharsetProvider.java /test/CharsetHello.java /testWith.xhtml /

我是JSF新手,还在学习。我试图寻找下面描述的我的具体问题的解决方案,但什么也找不到。如果是因为我在寻找错误的东西,请给我指出正确的方向,但希望这是一个尚未得到回答的问题,一个答案可以让每个人受益

下面的例子说明了我遇到的问题。该示例被简化为关注问题,并隐藏发生问题的实际项目的复杂性

考虑以下页面/类:

  • /resources/test/custom.xhtml
  • /test/CharsetProvider.java
  • /test/CharsetHello.java
  • /testWith.xhtml
  • /testWithout.xhtml /resources/test/custom.xhtml

    这是一个具有一个默认值的属性的复合组件。组件只需获取属性值并将其作为参数传递给下面描述的CDIBean,以获取用于输出的模型对象

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:cc="http://java.sun.com/jsf/composite"
          xmlns:h="http://java.sun.com/jsf/html">
    
        <cc:interface>
            <cc:attribute name="charset"
                          default="#{charsetProvider.defaultCharset}"
                          type="java.nio.charset.Charset" />
        </cc:interface>
    
        <cc:implementation>
            <h:outputText value="#{charsetProvider.createCharsetHello(cc.attrs.charset).hello}"/>
        </cc:implementation>
    </html>
    
    test/CharsetHello.java

    这是“模型”对象。它只是将“Hello world!”转换为字节数组,然后使用给定的字符集返回

    package test;
    
    import java.nio.charset.Charset;
    
    public class CharsetHello {
    
        private static final String HW = "Hello World!";
        private final byte[] data;
        private final Charset cs;
    
        public CharsetHello(Charset cs) {
            this.cs = cs;
            this.data = CharsetHello.HW.getBytes(this.cs);
        }
    
        public String getHello() {
            return new String(this.data, this.cs);
        }
    }
    
    testWith.xhtml

    这是一个测试页面,它通过为组件的属性指定一个值来使用上面定义的复合组件。页面正确呈现,即在屏幕上打印“Hello World!”

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:test="http://java.sun.com/jsf/composite/test">
        <h:head>
            <title>Test</title>
        </h:head>
        <h:body>
            <test:custom charset="#{charsetProvider.getCharsetForName('UTF-16')}" />
        </h:body>
    </html>
    
    似乎在最后一种情况下,默认值在传递给方法之前被转换为java.lang.String

    首先,这是预期的行为吗?为什么

    如果这是预期的行为,您能建议一个不同的实现吗


    提前谢谢你

    此问题与此问题的依据完全相同:。Mojarra中的复合属性值类型被错误地计算为
    java.lang.Object
    ,而不是实际的模型类型


    据报道,这是一场灾难。它在MyFaces 2.1.9中工作。

    我能够复制它。不确定这是否是指定的行为,我必须先查看规范。至少直觉上我没有预料到。不过,对于EL具有内置强制的类型,如
    Number
    Boolean
    Enum
    (只要您不使用会被解释为
    long
    Integer
    ),它可以正常工作。再次感谢@BalusC跟踪并报告此问题。希望他们能尽快解决。同时,我只需要使用上面
    testWith.xhtml
    中描述的组件。
    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:test="http://java.sun.com/jsf/composite/test">
        <h:head>
            <title>Test</title>
        </h:head>
        <h:body>
            <test:custom charset="#{charsetProvider.getCharsetForName('UTF-16')}" />
        </h:body>
    </html>
    
    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:test="http://java.sun.com/jsf/composite/test">
        <h:head>
            <title>Test</title>
        </h:head>
        <h:body>
            <test:custom />
        </h:body>
    </html>
    
    /resources/test/custom.xhtml @14,94 value="#{charsetProvider.createCharsetHello(cc.attrs.charset).hello}": Cannot convert UTF-8 of type class java.lang.String to class java.nio.charset.Charset