facelets 可以自定義組件,今天看了一下,果然好用。
以前使用facelets只是定義一個界面的模板,并不沒有深入它的自定義組件方面的內容,其實它的自定義組件也就是定義一個tag,然后在xhtml中引入這個tag,但這要比普通的JSP方式的tag方便多了。
要想引用自定義的tag要在web.xml中加入下面代碼(前提是一定配置好其他的facelets內容)
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>
/WEB-INF/tld/xiangyun.taglib.xml
</param-value>
</context-param>
xiangyun.taglib.xml文件的內容如下:
<?xml version="1.0"?> <facelet-taglib> 在這里引用了一個component.jspx文件,這個文件就是一個或一組想放在一塊當做一個組件使用的內容,和其他的jspx文件的寫法一樣。內容如下: 只需要注意<ui:composition>標簽和引入的命名空間就行,標簽里面的內容就看你要完成的功能了。在這里我使用了ICEfaces的日期組件,可以替換成JSF支技的任何東西。 接下來就是要使用了。 <html xmlns="<ui:composition template="/pages/layout/layout.jspx"> 這里需要引入命名空間,定義前綴為ald, <ald:echo>這個標簽名同xiangyun.taglib.xml中聲明的要一致,inputDate是在定義組件component.jspx中聲明的#{inputDate},它可以接收EL表達式。 這樣我們就可以把大的復雜的頁面,分成可以重用的組件了。 有問題可以和我聯系:wfn_libo@163.com 也可以參考https://facelets.dev.java.net/nonav/docs/dev/docbook.html#gettingstarted-bean
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"
<namespace>http://xiangyun.cn/components</namespace>
<tag>
<tag-name>echo</tag-name>
<source>component.jspx</source>
</tag>
</facelet-taglib>
<ui:composition xmlns:ui="<ice:selectInputDate id="inputDate" popupDateFormat="MM/dd/yyyy"
value="#{inputDate}" renderAsPopup="true"
styleClass="iceSelInpDateInput" onkeydown="ctlent(event);"
rendered="true">
</ice:selectInputDate>
<h:outputText value="#{inputDate}"
rendered="true" styleClass="printText">
<f:convertDateTime dateStyle="long" type="date" timeZone="GMT+8"
locale="cn" />
</h:outputText>
</ui:composition>
<ui:define name="content">
<h:form id="testForm">
<ald:echo inputDate="#{testBean.date}"/>
</h:form>
</ui:define>
</ui:composition>
</html>