Posted on 2006-01-05 21:34
Eddie Lee 阅读(10975)
评论(1) 编辑 收藏 所属分类:
JSF
tree2是Myfaces的tomahawk控件中的一个,在使用tomahawk控件之前一定先确定已经按照myfaces的要求配置好web.xml文件。
Myfaces网站tomahawk栏目的Extensions Filter 子栏目中有如下一段话:
If you just use standard JSF component, but don't use any MyFaces' extended component (beginning with t:),
then you don't need the Extensions Filter.
However, if you use some of the MyFaces' extended components like t:inputFileUpload, t:inputHTtml, t:inputCalendar, ...
then you most likely need to have this filter configured in your webapp.
大概意思是,如果你在项目中没有使用到Myfaces的扩展组件(t:开头的),则你不需要配置这个Filter.
可是,如果你用了Myfaces的扩展组件,你必须为你的web程序配置这个Filter.
配置如下:
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.component.html.util.ExtensionsFilter</filter-class>
<init-param>
<param-name>maxFileSize</param-name>
<param-value>20m</param-value>
<description>Set the size limit for uploaded files.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB
</description>
</init-param>
</filter>
<!-- extension mapping for adding <script/>, <link/>, and other resource tags to JSF-pages -->
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<!-- servlet-name must match the name of your javax.faces.webapp.FacesServlet entry -->
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<!-- extension mapping for serving page-independent resources (javascript, stylesheets, images, etc.) -->
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>/faces/myFacesExtensionResource/*</ url-pattern>
</filter-mapping> 要使用MyFaces的tomahawk控件,要在页面引用<%@ taglib uri="
http://myfaces.apache.org/tomahawk" prefix="t"%>。
在页面中添加tree2的标签:
<t:tree2 id="sortTree" var="node" varNodeToggler="t" showRootNode="false" value="#{tree.treeData}">
</t:tree2> 建立托管Bean,用于填充tree。
public class TreeBacker implements Serializable
{
public TreeNode getTreeData ( )
{
TreeNode treeData = new TreeNodeBase ( "foo-folder" , "Inbox" , false ) ;
TreeNodeBase personNode = new TreeNodeBase("person", "Eddie L", false);
//personNode.getChildren().add(new TreeNodeBase("document","Eddie doc one", true));
TreeNodeBase doc = new TreeNodeBase("document","Eddie doc one", true);
doc.setIdentifier("No.1");
personNode.getChildren().add(doc);
treeData.getChildren().add(personNode);
System.out.println ( "Create Tree ..ok" ) ;
return treeData ;
}
} 其中TreeNodeBase personNode = new TreeNodeBase("person", "Eddie L", false);中的"person"的作用是表示渲染得时候显示什么样式,
跟页面中<f:facet name="person"></f:facet>相对应。
在页面中
<f:facet name="person">
<h:panelGroup>
<f:facet name="expand">
<t:graphicImage value="../images/person.png" rendered="#{t.nodeExpanded}" border="0" />
</f:facet>
<f:facet name="collapse">
<t:graphicImage value="../images/person.png" rendered="#{!t.nodeExpanded}" border="0" />
</f:facet>
<h:outputText value="#{node.description}" styleClass="nodeFolder" />
</h:panelGroup>
</f:facet> <t:graphicImage value="../images/person.png" rendered="#{t.nodeExpanded}" border="0" />显示Tree每一个节点的图片。rendered属性的意思是是否渲染的意思,
jsf中渲染的意思是把jsf组件树当前的状态转换成html。在jsf的生命周期里,渲染响应在最后一个周期,rendered值为false在渲染相应这个周期就不运行。
在处理叶子节点上,需要特殊的注意。因为对叶子节点是需要操作的。可能需要点击叶子节点连接到一个页面,或者执行某些action等。
<f:facet name="document">
<h:panelGroup>
<h:commandLink immediate="true" styleClass="#{t.nodeSelected ? 'documentSelected':'document'}" actionListener="#{t.setNodeSelected}" action="#{tree.okListener}" id="clickbutton">
<t:graphicImage value="../images/document.png" border="0" />
<h:outputText value="#{node.description}" />
<f:param name="docNum" value="#{node.identifier}" />
</h:commandLink>
</h:panelGroup>
</f:facet> commandLink标记可以有n个参数,参数分为参数名和参数值。在程序里可以读取。
编写action事件:okListener
FacesContext context = FacesContext.getCurrentInstance ( ) ;//取得上下文
String a = context.getExternalContext ( ).getRequestParameterMap ( )
.get ( "docNum" ).toString ( ) ;
a的值就是docNum的值。
identifier的值在创建树的时候一起加进去的:
TreeNodeBase doc = new TreeNodeBase("document","Eddie doc one", true);
doc.setIdentifier("No.1");
这样。一个tree2的应用基本就算ok了。