http://java.sun.com/j2ee/javaserverfaces/index.jsp
http://wwws.sun.com/software/communitysource/jsf/download.html download source
JSF的中心思想也是页面组件/属性事件。一般来说,JSF的页面组件是一个三件套{ UI Component, Tag, Renderer}。
UI Component有可能对应Model,Event,Listener。Tag包含componentType和rendererType两个属性,用来选择对应的的UI Component和Renderer。
JSF的应用核心无疑是JSF TagLib。JSF TagLib包含了对应所有重要HTML元素的Tag,而且Input Tag可以直接包含Validator Tag或者Validator属性,来定义验证手段。
我们通过JSF携带的cardemo例子,来看JSF的处理流程。
(1) carDetail.jsp有如下内容:
<h:commandButton action="#{carstore.buyCurrentCar}" value="#{bundle.buy}" />
可以看到,这个button的submit action和carstore.buyCurrentCar方法绑定在一起。我们在Tapestry里面曾经看到过类似的情景。
(2) carstore在faces-config.cml中定义:
<managed-bean>
<managed-bean-name> carstore </managed-bean-name>
<managed-bean-class> carstore.CarStore </managed-bean-class>
<managed-bean-scope> session </managed-bean-scope>
</managed-bean>
(3) carstore.CarStore类中的buyCurrentCar方法如下:
public String buyCurrentCar() {
getCurrentModel().getCurrentPrice();
return "confirmChoices";
}
(4) confirmChoices转向在faces-config.cml中定义:
<navigation-rule>
<from-view-id>/carDetail.jsp</from-view-id>
<navigation-case>
<description>
Any action that returns "confirmChoices" on carDetail.jsp should
cause navigation to confirmChoices.jsp
</description>
<from-outcome>confirmChoices</from-outcome>
<to-view-id>/confirmChoices.jsp</to-view-id>
</navigation-case>
</navigation-rule>
(5)于是转到页面confirmChoices.jsp。
除了Interceptor之外,JSF几乎包含了现代Web Framework应该具备的所有特性:页面组件,属性事件,IoC (ManagedBean),Component -> Renderer,类似于Swing Component的Model-Event-Listener。
也许设计者认为,众多庞杂的模式能够保证JSF成为一个成功的框架。Portal开源项目eXo就是建立在JSF框架上。
可以看出这样一个趋势,现代Web Framework认为B/S结构的无状态特性和HTML界面是对编程来说是需要极力掩盖的一个缺陷,所以尽量模拟C/S结构的组件和事件机制,以吸引更多的程序员