所以让我们选择一个UIComponent HtmlSelectOneRadio(请查看源代码:http://grepcode.com/file/repo1.maven.org/maven2/com.sun.faces/jsf-api/2.1.7/javax/faces/component/ html/HtmlSelectOneRadio.java#HtmlSelectOneRadio)
因此,有些setter会调用方法handleAttribute(...),有些则不会
public void setDir(java.lang.String dir) {
getStateHelper().put(PropertyKeys.dir, dir);
handleAttribute("dir", dir);
}
public void setDisabled(boolean disabled) {
getStateHelper().put(PropertyKeys.disabled, disabled);
}
Run Code Online (Sandbox Code Playgroud)
这对我来说是非常不可思议的handleAttribute,JSF大师可以向我解释一下这个方法尝试完成的原因以及为什么somes属性调用这个方法而其他方法没有?非常感谢
它与Mojarra内部渲染优化有关,其中只有内部handleAttribute()方法设置的属性被渲染为所谓的"pass-thru"属性,而不是如果已经检查了组件的每个单独属性.是否设置,如果存在相对较多的属性,则可能会更昂贵."pass-thru"属性是一个组件属性,可以直接呈现而无需任何特定的前/后处理.
在Mojarra的RenderKitUtils课堂上,从renderPassThruAttributes()方法开始:
316 if (canBeOptimized(component, behaviors)) {
317 //noinspection unchecked
318 List<String> setAttributes = (List<String>)
319 component.getAttributes().get(ATTRIBUTES_THAT_ARE_SET_KEY);
320 if (setAttributes != null) {
321 renderPassThruAttributesOptimized(context,
322 writer,
323 component,
324 attributes,
325 setAttributes,
326 behaviors);
327 }
328 } else {
329
330 // this block should only be hit by custom components leveraging
331 // the RI's rendering code, or in cases where we have behaviors
332 // attached to multiple events. We make no assumptions and loop
333 // through
334 renderPassThruAttributesUnoptimized(context,
335 writer,
336 component,
337 attributes,
338 behaviors);
339 }
Run Code Online (Sandbox Code Playgroud)
的canBeOptimized()返回true,如果component是标准的JSF组件中的一个(实际上,如果组件的包名称开头javax.faces.component.),并且如果没有超过1行为中behaviors阵列.
将renderPassThruAttributesOptimized()只会使它们在指定的属性setAttributes参数.
将renderPassThruAttributesUnoptimized()通过在每一个属性将循环attributes图和检查每一个属性,如果相关的值不为空/空,然后渲染它.
至于为什么没有设置一些属性handleAttribute(),这是因为它们需要一些更具体的前/后处理,并且已经由特定于组件的渲染器显式渲染,因此它们不需要渲染为"pass-thru" "属性.
编写自定义组件时,您无需担心这一点.您始终可以引入与此类似的自己的优化,但不建议依赖Mojarra特定的内部优化,因为当您使用MyFaces时它们可能根本不起作用.
| 归档时间: |
|
| 查看次数: |
542 次 |
| 最近记录: |