Facelets自定义标签的应用

  最近在用JSF做项目,其中也借用了facelets和richfaces额的相关辅助插件。呵呵,发现facelets的自定义标签真的很方便,所以在此的例子是做的一个基本的分页控件(用于xhtml页面):

1.首先与JSP额自定义标签类似,在web-inf下建立一个pager.taglib.xml的文件,内容大致如下:

  <?xml  version="1.0"  encoding="gb2312"?>
<!DOCTYPE facelet-taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
  "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
    <namespace>http://www.bypage.com/page</namespace>
    <tag>
      <tag-name>pager</tag-name>
      <source>pager.xhtml</source>
    </tag>
</facelet-taglib>

 

 

 

2. 要使用自定义的facelets标签,在web.xml中必须做如下配置:
 <context-param>
    <param-name>facelets.LIBRARIES</param-name>
    <param-value>/WEB-INF/pager.taglib.xml</param-value>
 </context-param>
 

 

 

3.配置pager.xhtml的代码:

<?xml  version="1.0"  encoding="gb2312"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:rich="http://richfaces.org/rich"
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:t="http://myfaces.apache.org/tomahawk"
      xmlns:c="http://java.sun.com/jstl/core"
>

<f:view> 
     记录数:<h:outputText value="#{backingBean.maxItems}" />
    &nbsp;&nbsp;&nbsp;总页数:<h:outputText value="#{backingBean.maxPages}" />
    &nbsp;&nbsp;&nbsp;当前页:<h:outputText value="#{backingBean.pageNumber}" />
  <c:if test="#{backingBean.maxPages gt 0 and backingBean.pageNumber ne 1}">
     &nbsp;&nbsp;
     <h:commandLink action="#{backingBean[action]}" value="首页" >
         <f:param  name="pageNumber" value="1"></f:param>
         <f:param  name="maxPageItems" value="#{backingBean.maxPageItems}"></f:param>
     </h:commandLink>
  </c:if>
  <c:if test="#{backingBean.maxPages le 0 or backingBean.pageNumber eq 1}">
        &nbsp;&nbsp;首页
  </c:if>
     <c:if test="#{backingBean.pageNumber gt 1}">
       &nbsp;<h:commandLink action="#{backingBean[action]}" value="上一页" >
              <f:param  name="pageNumber" value="#{backingBean.prePage}"></f:param>
              <f:param  name="maxPageItems" value="#{backingBean.maxPageItems}"></f:param>
         </h:commandLink>
     </c:if> 
     <c:if test="#{backingBean.pageNumber le 1}">
          &nbsp;上一页
     </c:if> 
  <c:if test="#{backingBean.pageNumber lt backingBean.maxPages}">
          &nbsp;<h:commandLink action="#{backingBean[action]}" value="下一页" >
               <f:param  name="pageNumber" value="#{backingBean.nextPage}"></f:param>
               <f:param  name="maxPageItems" value="#{backingBean.maxPageItems}"></f:param>
           </h:commandLink>
     </c:if> 
     &nbsp;
     <c:if test="#{backingBean.pageNumber ge backingBean.maxPages}">
          &nbsp;下一页
     </c:if>
    
     <c:if test="#{backingBean.maxPages gt 0 and backingBean.pageNumber ne backingBean.maxPages}">
      &nbsp;<h:commandLink action="#{backingBean[action]}" value="末页" >
     
               <f:param name="pageNumber" value="#{backingBean.maxPages}"/>
               <f:param name="maxPageItems" value="#{backingBean.maxPageItems}"/>
      
      </h:commandLink>
     </c:if> 
  <c:if test="#{backingBean.maxPages le 0 or backingBean.pageNumber eq backingBean.maxPages}">
        &nbsp;末页
     </c:if>
  &nbsp;&nbsp;页码:<h:inputText value="#{backingBean.PAGER_pageNumber}" name="PAGER_pageNumber" id="PAGER_pageNumber" size="2" />

     <h:commandLink action="#{backingBean[action]}" value="转到" >
          <f:param  name="pageNumber" value="#{backingBean.PAGER_pageNumber}"></f:param>
       <f:param  name="maxPageItems" value="#{backingBean.maxPageItems}"></f:param>
     </h:commandLink>
        &nbsp;每页:
     <h:inputText  name="maxPageItems" size="2"  id="mpg" value="#{backingBean.maxPageItems}"></h:inputText>
      <h:commandLink action="#{backingBean[action]}" value="显示" >
          <f:param  name="pageNumber" value="1"></f:param>
       <f:param  name="maxPageItems" value="#{backingBean.maxPageItems}"></f:param>
   </h:commandLink>
</f:view>
</ui:composition>


 

4.还有一个基本的bean工具类,有上面xhtml中用的所有属性,和基本的分页方法,一旦按照以上方式定义好以后,就可以在任何的xhtml页面中调用以上定义的facelets标签,可以实现和JSP自定义标签一样便捷的效果!!

 

 

你可能感兴趣的:(C++,JSF,F#,Richfaces,C#)