1

您好我正在尝试创建一个扩展的自定义组件UIInput。在该组件中,我生成一个 html 输入、一个 html 提交按钮和一行文本。代码如下:

@Override
public void decode(FacesContext context) {
    Map requestMap = context.getExternalContext().getRequestParameterMap();
    String clientId = getClientId(context);
    char sep = UINamingContainer.getSeparatorChar(context);
    String symbol = ((String) requestMap.get(clientId + sep + "inputfield"));
    setSubmittedValue(symbol);
}

@Override
public void encodeEnd(FacesContext context) throws IOException {
    String clientId = getClientId(context);
    char sep = UINamingContainer.getSeparatorChar(context);
    //-----------------------this generates an html input-------------------------
    encodeInputField(context, clientId + sep + "inputfield");
    //Now if I uncomment the next line it generates another html, whose value always stays the same as the first one
    //encodeInputField(context, clientId + sep + "inputfield2");
    encodeSubmitButton(context, clientId + sep + "submit");
    encodeOutputField(context);
}

private void encodeInputField(FacesContext context, String clientId) throws IOException {
    // Render a standard HTML input field
    ResponseWriter writer = context.getResponseWriter();
    writer.startElement("input", this);
    writer.writeAttribute("type", "text", null);
    writer.writeAttribute("name", clientId, "clientId");
    Object value = getValue();
    if (value != null) {
        writer.writeAttribute("value", value.toString(), "value");
    }
    writer.writeAttribute("size", "6", null);
    writer.endElement("input");
}

private void encodeSubmitButton(FacesContext context, String clientId) throws IOException {
    // render a submit button
    ResponseWriter writer = context.getResponseWriter();
    writer.startElement("input", this);
    writer.writeAttribute("type", "Submit", null);
    writer.writeAttribute("name", clientId, "clientId");
    writer.writeAttribute("value", "Click Me!", null);
    writer.endElement("input");
}

private void encodeOutputField(FacesContext context) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    //----------------weird value that comes out of nowhere-----------------------
    String hellomsg = (String) getAttributes().get("value");
    writer.startElement("p", this);
    writer.writeText("You entered: " + hellomsg, null);
    writer.endElement("p");
}

现在一切正常,但我不明白value属性的String hellomsg = (String) getAttributes().get("value");来源。我试图调试这个程序,getAttributes()hashmap 还包含奇怪的条目,我找不到任何键为"value".

最后,如果我生成两个 html 输入,那么第二个输入的值始终与第一个相同。

我还注意到我可以value在标签中包含 a,例如<mycc:cinput value="yes">,当页面加载时,生成的 html 输入的值设置为yes.

我的疑问是:每个 UIInput 都有默认value属性吗?如果是这样,该属性值是否总是链接到任何 html 输入的value属性?如果是这样,它是否总是链接到value生成的第一个 html 输入的属性?

提前感谢您阅读这么长的问题。如果可能的话,你们能告诉我在哪里可以找到此类问题的答案吗?浏览随机的谷歌搜索结果让我头疼@_@

非常感谢!

4

1 回答 1

1

现在一切正常,但我不明白 String hellomsg = (String) getAttributes().get("value"); 中的 value 属性在哪里 来了。我试图调试这个程序,getAttributes() hashmap 也包含奇怪的条目,我找不到任何键为“value”的条目。

阅读. _ UIComponent#getAttributes()直截了当地说,这确实是一张抽象地图。并get("value")没有真正从映射中返回条目,但它基本上value通过调用其方法(如果有)来获取组件本身的属性(getValue()如果有的话UIInput)。但是如果您已经坐在组件本身中,则不需要调用getAttributes().get("value"),您可以直接调用该getValue()方法。

因此请注意,如果您打算在属性映射中放置自定义属性,那么您应该使用不同的名称,或者更好地使用StateHelper.

也可以看看:

于 2013-01-17T15:26:25.517 回答