JSF自定义组件中ResponseWriter.writeAttribute方法说明

最近学习JSF遇到很多奇怪的问题,就比如说ResponseWriter类里面的writeAttribute方法,Java EE 6 官方文档都没有具体的说明。API对这个方法的描述是:

public abstract void writeAttribute(java.lang.String name, java.lang.Object value, java.lang.String property)

 throws java.io.IOException


Write an attribute name and corresponding value, after converting that text to a String (if necessary), and after performing any escaping appropriate for the markup language being rendered. This method may only be called after a call to startElement(), and before the opened element has been closed.


Parameters:
name - Attribute name to be added
value - Attribute value to be added
property - Name of the property or attribute (if any) of the UIComponent associated with the containing element, to which this generated attribute corresponds 


Throws: 
IllegalStateException - if this method is called when there is no currently open element 
java.io.IOException - if an input/output error occurs 
java.lang.NullPointerException - if name is null


前两个参数没有问题,用来写HTML标签的属性和值,如<label for="username">... 参数“name”指定label的“for”属性,“value”指定for的值“username”,可是我试了半天也不知道property是用来干什么的,api描述的大致意思好像是,这个生成的属性与UIComponent的属性的名字相关,而这个UIComponent又与包含元素相关。不知道是我理解有问题还是本身就定义不明确,我不懂是什么意思,文档中也没有例子。于是我便查看JSF 2.1的源代码,发现这个属性并没有被使用,只是当作一个参数被传进来,应该是还没有实现吧。这只是我的猜测,下面贴出源代码。希望有了解的朋友在评论里帮我解答一下。

public void writeAttribute(String name, Object value,
                               String componentPropertyName)
          throws IOException {

        if (name == null) {
            throw new NullPointerException(MessageUtils.getExceptionMessageString(
                  MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "name"));
        }
        if (value == null) {
            return;
        }

        if (isCdata) {
            return;
        }
        
        if (containsPassThroughAttribute(name)) {
            return;
        }

        if (name.equalsIgnoreCase("src") && isScriptOrStyle()) {
            scriptOrStyleSrc = true;
        }

        Class valueClass = value.getClass();

        // Output Boolean values specially
        if (valueClass == Boolean.class) {
            if (Boolean.TRUE.equals(value)) {
                // NOTE:  HTML 4.01 states that boolean attributes
                //        may legally take a single value which is the
                //        name of the attribute itself or appear using
                //        minimization.
                //  http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.3.4.2
                attributesBuffer.write(' ');
                attributesBuffer.write(name);
                attributesBuffer.write("=\"");
                attributesBuffer.write(name);
                attributesBuffer.write('"');
            }
        } else {
            attributesBuffer.write(' ');
            attributesBuffer.write(name);
            attributesBuffer.write("=\"");
            // write the attribute value
            String val = value.toString();
            ensureTextBufferCapacity(val);
            HtmlUtils.writeAttribute(attributesBuffer,
                                     escapeUnicode,
                                     escapeIso,
                                     buffer,
                                     val,
                                     textBuffer,
                                     isScriptInAttributeValueEnabled);
            attributesBuffer.write('"');
        }

    }



猜你喜欢

转载自blog.csdn.net/fengqiuzhihua/article/details/8804292