确实使用标记<label>
而不是<h:outputLabel>
允许我改善页面的加载时间并减少保留的内存(因为在ViewRoot中不创建HTML组件)?是否还有其他标签要替换?
为了实现这些想法,我对简单项目进行了测试:Hello world Project
我用两页页面改变了hello.html:
的的Hello.html
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml"
> xmlns:f="http://java.sun.com/jsf/core"
> xmlns:h="http://java.sun.com/jsf/html">
>
> <h:head>
> <title>JSF 2.0 Hello World</title>
> </h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:form id="idfrm">
<label id="idlbl1">I'm a label 1</label>
<br/>
<label id="idlbl2">I'm a label 2</label>
<br/>
<label id="idlbl3" >I'm a label 3</label>
<br/>
<h:inputText value="#{helloBean.name}"></h:inputText>
<h:commandButton value="Welcome Me" action="#{helloBean.tester}"/>
</h:form>
</h:body>
</html>
和页面Hello2.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:form id="idfrm">
<h:outputLabel id="idlbl1" value="I'm an HtmlOUtputLabel 1"/>
<br/>
<h:outputLabel id="idlbl2" value="I'm an HtmlOUtputLabel 2"/>
<br/>
<h:outputLabel id="idlbl3" value="I'm an HtmlOUtputLabel 3"/>
<br/>
<h:inputText value="#{helloBean.name}"></h:inputText>
<h:commandButton value="Welcome Me" action="#{helloBean.tester}"/>
</h:form>
</h:body>
和HelloBean.java
> package com.mkyong.common;
> import java.io.Serializable;
> import java.util.List;
> import javax.faces.bean.ManagedBean;
> import javax.faces.component.UIComponent;
> import javax.faces.component.html.HtmlOutputLabel;
> import javax.faces.context.FacesContext;
> @ManagedBean
> public class HelloBean implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public HelloBean(){
}
private void traceAttrs (UIComponent c){
System.out.println("id="+c.getClientId());
}
public String tester(){
UIComponent frm =FacesContext.getCurrentInstance().
getViewRoot().findComponent("idfrm");
List<UIComponent> componentList = frm.getChildren();
for (UIComponent c:componentList){
if (c instanceof HtmlOutputLabel ){
traceAttrs(c);
}
}
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第一个测试操作(在Hello.html中)在控制台上提供此输出
id=idfrm:idlbl1
id=idfrm:idlbl2
id=idfrm:idlbl3
id=idfrm:j_idt11
,第二个给出
id=idfrm:j_idt9
所以我可以假设对象不再在Viewtree中了。在一个Web应用程序中,有很多标签,所以这种方法可能很有意思吗?
非常感谢您的回复