2013-10-15 39 views
2

根据该JSF说明书中,h:selectOneMenu呈现为一个HTML <select>元件,用“大小”属性设置为项的数目:h:selectOneMenu呈现的select元素的默认大小是多少?

使用项目的数目作为“大小”的值属性。

(从tag documentation for selectOneMenu

然而,当我在页面中使用h:selectOneMenu(使用钻嘴鱼科或MyFaces的),呈现的<select>的大小属性总是被设置为1。

谷歌搜索周围也发现许多页面,只是声称h:selectOneMenu是为了呈现一个“下拉菜单”(这是大多数浏览器呈现为<select size="1">[...]

所以这是JSF规范中的错误,或者是我忽略了一些细节?为什么尺寸不符合规范中的描述?

+0

我正面临这个问题。使用size属性时,Bootstrap没有应用正确的样式。 –

回答

2

看着the source code of the MenuRenderer class,从com.sun.faces.renderkit.html_basic包后,目前还不清楚它是否是在说明书或者不是一个bug。

为了呈现h:selectOneMenu组件以及h:selectManyMenu组件,将调用renderSelect方法。关于尺寸属性:

  • 如果未指定(selectOneMenu的情况),则设置默认值。
  • 如果指定,the "size" attribute will be rendered as one of the "pass thru" attributes

这里是源代码:

protected void [More ...] renderSelect(FacesContext context, 
          UIComponent component) throws IOException { 

    [...] 

    // Determine how many option(s) we need to render, and update 
    // the component's "size" attribute accordingly; The "size" 
    // attribute will be rendered as one of the "pass thru" attributes 

    [...] 

    // If "size" is *not* set explicitly, we have to default it correctly 
    Integer size = (Integer) component.getAttributes().get("size"); 
    if (size == null || size == Integer.MIN_VALUE) { 
     size = count; 
    } 
    writeDefaultSize(writer, size); 

    RenderKitUtils.renderPassThruAttributes(context, 
            writer, 
            component, 
            ATTRIBUTES, 
            getNonOnChangeBehaviors(component)); 

    [...] 

} 

混乱在于writeDefaultSize方法。它始终将默认大小为1,即使要呈现由ITEMCOUNT参数给出的选项数量:

protected void [More ...] writeDefaultSize(ResponseWriter writer, int itemCount) 
    throws IOException { 
    // if size is not specified default to 1. 
    writer.writeAttribute("size", "1", "size"); 
} 

所以大小是在JSF规范描述的既不正常,请也不是在描述代码的评论。这可能不是一个错误,但它有点令人困惑。

相关问题