gpt4 book ai didi

jsf - 访问传递给扩展 PrimeFaces 组件的属性

转载 作者:行者123 更新时间:2023-12-04 00:54:12 24 4
gpt4 key购买 nike

我正在尝试创建一个自定义组件来扩展 PrimeFaces。

我在测试命名空间下有一个名为 textInput 的简单组件,它只调用 PrimeFaces textInput 组件并打印出传递给名为 fieldClass 的属性的值以及传递的任何属性的名称

如果我将 fieldClass 作为字符串传递:

<test:textInput id="foo" fieldClass="field-foo" />

这是结果

fieldClass = field-foo

[com.sun.faces.facelets.MARK_ID, fieldClass]

如果我将 fieldClass 作为表达式传递

<ui:param name="bar" value="field-foo"/>
<test:textInput id="foo" fieldClass="#{bar}" />

fieldClass 消失

fieldClass = NONE

[com.sun.faces.facelets.MARK_ID]

我如何真正掌握传递给组件的属性?

自定义组件使用的类如下:

测试.components.ExtendInputTextRenderer

package test.components;

import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;
import org.primefaces.component.inputtext.*;

@FacesRenderer(
componentFamily=ExtendInputText.COMPONENT_FAMILY,
rendererType=ExtendInputTextRenderer.RENDERER_TYPE
)

public class ExtendInputTextRenderer extends InputTextRenderer {
public static final String RENDERER_TYPE = "com.example.ExtendInputTextRenderer";

@Override
public void encodeEnd(FacesContext context, UIComponent component)
throws java.io.IOException {
ResponseWriter writer = context.getResponseWriter();
Map attrs = component.getAttributes();
String fieldClass = attrs.containsKey("fieldClass") ? (String) attrs.get("fieldClass").toString() : "NONE";
writer.write("fieldClass = " + fieldClass + "<br/>");
writer.write(attrs.keySet().toString() + "<br/>");
super.encodeEnd(context, component);
}
}

测试.components.ExtendInputText

package test.components;

import javax.faces.component.FacesComponent;
import org.primefaces.component.inputtext.InputText;



@FacesComponent(ExtendInputText.COMPONENT_TYPE)
public class ExtendInputText extends InputText {

public static final String COMPONENT_FAMILY = "com.example";
public static final String COMPONENT_TYPE = "com.example.ExtendInputText";

@Override
public String getFamily() {
return COMPONENT_FAMILY;
}

@Override
public String getRendererType() {
return ExtendInputTextRenderer.RENDERER_TYPE;
}
}

最佳答案

String fieldClass = attrs.containsKey("fieldClass") ? (String) attrs.get("fieldClass").toString() : "NONE";

你的错误是你在使用 containsKey()检查属性是否已指定。

这是来自 UIComponent#getAttributes() javadoc 的摘录:

getAttributes

public abstract java.util.Map<java.lang.String,java.lang.Object> getAttributes()

Return a mutable Map representing the attributes (and properties, see below) associated wth this UIComponent, keyed by attribute name (which must be a String). The returned implementation must support all of the standard and optional Map methods, plus support the following additional requirements:

  • The Map implementation must implement the java.io.Serializable interface.
  • Any attempt to add a null key or value must throw a NullPointerException.
  • Any attempt to add a key that is not a String must throw a ClassCastException.
  • If the attribute name specified as a key matches a property of this UIComponent's implementation class, the following methods will have special behavior:
    • containsKey() - Return false.
    • get() - If the property is readable, call the getter method and return the returned value (wrapping primitive values in their corresponding wrapper classes); otherwise throw IllegalArgumentException.
    • put() - If the property is writeable, call the setter method to set the corresponding value (unwrapping primitive values in their corresponding wrapper classes). If the property is not writeable, or an attempt is made to set a property of primitive type to null, throw IllegalArgumentException.
    • remove() - Throw IllegalArgumentException.

请注意,它因此总是返回 false对于 containsKey对于组件的属性。那是因为动态属性不存储在属性映射中,而是存储在组件实例本身中。它们仅在调用 get() 时得到解决.

需要将错误的行改成如下:

String fieldClass = (String) attrs.get("fieldClass");
if (fieldClass == null) fieldClass = "NONE";

关于jsf - 访问传递给扩展 PrimeFaces 组件的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11204262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com