您好我正在尝试创建一个扩展的自定义组件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 输入的属性?
提前感谢您阅读这么长的问题。如果可能的话,你们能告诉我在哪里可以找到此类问题的答案吗?浏览随机的谷歌搜索结果让我头疼@_@
非常感谢!