JSF2自定义组件(UIInput)'value'属性

时间:2013-01-17 10:19:49

标签: jsf-2 custom-component

您好我正在尝试创建一个扩展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");
}

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

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

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

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

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

非常感谢!

1 个答案:

答案 0 :(得分:1)

  

现在一切正常但我不明白String hellomsg =(String)中的value属性getAttributes()。get(&#34; value&#34;);谈到。我试图调试这个程序,getAttributes()hashmap也包含奇怪的条目,我找不到任何条目,其键是&#34;值&#34;。

阅读javadoc of UIComponent#getAttributes()。到目前为止,它确实是一个抽象的地图。 get("value")并没有真正从地图返回一个条目,但它基本上通过调用其value方法获得组件本身的getValue()属性(如果有的话)(并且它是UIInput的情况。但是,如果您已经在组件中,您不需要调用getAttributes().get("value"),则可以直接调用getValue()方法。

因此请注意,如果您打算在属性地图中添加自定义属性,那么您应该使用其他名称,或者更好地使用StateHelper

另见:

相关问题