完整的教材: html
采用JSF架构的应用,也有不少项目是使用jsp而不是facelets。若是要在JSP里使用JSF组件,还得编写jsp的标签库,不过与JSP标签又不尽相同,下面我再次重构HelloWorld项目,看看JSP页面如何使用<ida:hellWorld />标签。测试
开发环境:ui
Windows 7this
IntelliJ IDEA 12.1.2spa
jboss-6.1.0.Final
JSF 1.2
HelloWorld的 重构过程:
一、新建HelloWorldTag,JSF1.2继承UIComponentELTag,网上不少教材都是JSF1.1版本的,继承的是UIComponentTag,这个类在JSF1.2已过期。
public class HelloWorldTag extends UIComponentELTag { private static final String COMPONENT_TYPE = "com.regaltec.faces.HelloWorld"; private static final String RENDERER_TYPE = "com.regaltec.faces.render.HelloWorld"; private ValueExpression name; public ValueExpression getName() { return name; } public void setName(ValueExpression name) { this.name = name; } @Override protected void setProperties(UIComponent component) { super.setProperties(component); component.setValueExpression("name", name); } @Override public void release() { super.release(); name = null; } @Override public String getComponentType() { return COMPONENT_TYPE; } @Override public String getRendererType() { return RENDERER_TYPE; } }
HelloWorldTag的属性与UIHelloWorld相似,事实上HelloWorldTag在这里只是作一个适配。HelloWorldTag必须重载setProperties、getComponentType、getRendererType这3个方法,getComponentType、getRendererType这2个方法用来肯定一个视图渲染器,而setProperties方法就是将tag里的属性转换UIComponent组件中了。
注:使用a4j的reRender时,是不会调HelloWorldTag这个类的,这跟jsf生命周期有关。
二、在WEB-INF目录下新建uicomponent.tld,这是JSP标签库描述文件。
<?xml version="1.0" encoding="ISO-8859-1"?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <tlib-version>1.0</tlib-version> <short-name>ida</short-name> <uri>http://www.regaltec.com/ida40</uri> <tag> <name>helloWorld</name> <tag-class>com.regaltec.faces.taglib.HelloWorldTag</tag-class> <body-content>empty</body-content> <attribute> <name>name</name> <required>false</required> <deferred-value> <type>java.lang.String</type> </deferred-value> </attribute> </tag> </taglib>
三、就这样咱们就完成重构工做了,但重构前咱们的web应用都是跑facelets页面的,为了跑jsp页面,还得从新配置web.xml及faces-config.xml这2个文件。
删除web.xml的facelets配置
<context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value> </context-param> <context-param> <param-name>facelets.LIBRARIES</param-name> <param-value>/WEB-INF/uicomponent.taglib.xml</param-value> </context-param>
删除faces-config.xml的facelets配置
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
四、新建jsp页面进行测试,这里简单的将helloWorld.xhtml转换为helloWorld.jsp页面。
<!DOCTYPE html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %> <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %> <%@ taglib prefix="a4j" uri="http://richfaces.org/a4j" %> <%@ taglib prefix="ida" uri="http://www.regaltec.com/ida40" %> <html> <f:view> <head> <title>Hello World JSP</title> </head> <body> <a4j:form> <h:panelGrid columns="3"> <h:outputText value="姓名:" /> <h:inputText value="#{helloWorldBean.name}" /> <a4j:commandButton value="肯定" reRender="out" /> </h:panelGrid> </a4j:form> <h:panelGroup id="out"> <ida:helloWorld name="#{helloWorldBean.name}" /> </h:panelGroup> </body> </f:view> </html>
启动jboss打开应用,以下图,在文本输入框里敲入世界点击肯定,文本框下方显示 你好,世界!。
以上代码在jboss-6.1.0.Final调试经过,感谢百度云网盘提供源码下载。