JSF自定义组件

知道怎么自定义JSF组件,有利于我们理解一些第三方的界面框架,如:prime faces。

为了简单起见,我们把项目分成两个工程, 一个是WEB工程,引用我们自写义的组件,一个是普通的的java工程,打成jar包之后,在web工程里进行引用。

两个工程都是基于maven的。

先看java工程。

JAVA工程

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zolcorp.jsfcom</groupId>
  <artifactId>jsfcom</artifactId>
  <version>0.0.1</version>
  <name>jsfcom</name>
  <description>jsfcom</description>
  <dependencies>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.8.0</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>
		<dependency>
			<groupId>commons-digester</groupId>
			<artifactId>commons-digester</artifactId>
			<version>1.8</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>com.sun.faces</groupId>
			<artifactId>jsf-api</artifactId>
			<version>2.1.2</version>
		</dependency>
		<dependency>
			<groupId>com.sun.faces</groupId>
			<artifactId>jsf-impl</artifactId>
			<version>2.1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>org.mongodb</groupId>
			<artifactId>mongo-java-driver</artifactId>
			<version>2.11.0</version>
		</dependency>
	</dependencies>
</project>

src/main/resources/META-INF/helloworld.taglib.xml
<?xml version='1.0' encoding='UTF-8'?>
<facelet-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-facelettaglibrary_2_0.xsd"
	version="2.0">
	<namespace>http://myjsf.com/component</namespace>
	<tag>
		<tag-name>htmlHelloWorld</tag-name>
		<component>
			<component-type>htmlHelloWorld</component-type>
		</component>
	</tag>
</facelet-taglib>

src/main/resources/META-INF/faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xi="http://www.w3.org/2001/XInclude" 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-facesconfig_2_0.xsd">
	<component>
    	<component-type>htmlHelloWorld</component-type>
    	<component-class>com.zolcorp.myjsf.component.HtmlHelloWorld</component-class>
  	</component>
</faces-config>

JAVA组件类
public class HtmlHelloWorld extends UIComponentBase {

	@Override
	public String getFamily() {
		return null;
	}

	@Override
	public void encodeAll(FacesContext context) throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		writer.startElement("div", this);
		writer.writeAttribute("style", "color : red", null);
		writer.writeText("HelloWorld! today is: " + new java.util.Date(), null);
		writer.endElement("div");
	}
}

在项目上执行maven install,即可把项目打成jar包,上传到自己的maven仓库。


WEB项目

pom.xml中添加如下依赖:
<dependency>
			<groupId>com.zolcorp.jsfcom</groupId>
			<artifactId>jsfcom</artifactId>
			<version>0.0.1</version>
		</dependency>

新增一个页面,测试下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 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:c="http://java.sun.com/jsp/jstl/core"
	xmlns:zl="http://myjsf.com/component">
<h:head>
	<title>myjsf</title>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</h:head>
<h:body>
	<zl:htmlHelloWorld/>
</h:body>
</html>

页面显示如下:


你可能感兴趣的:(JSF,自定义组件)