欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSF自定义组件

程序员文章站 2022-03-25 07:51:37
知道怎么自定义jsf,有利于我们理解一些第三方的界面框架,如:prime faces。 为了简单起见,我们把项目分成两个工程, 一个是web工程,引用我们自写义的组件,一个是普通的的java工程,打...

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

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

两个工程都是基于maven的。

先看java工程。

java工程
pom.xml


[html]
<project xmlns="https://maven.apache.org/pom/4.0.0" xmlns:xsi="https://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="https://maven.apache.org/pom/4.0.0 https://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.</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> 

<project xmlns="https://maven.apache.org/pom/4.0.0" xmlns:xsi="https://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="https://maven.apache.org/pom/4.0.0 https://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
[html]
<?xml version='1.0' encoding='utf-8'?> 
<facelet-taglib xmlns="https://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="https://www.w3.org/2001/xmlschema-instance" 
    xsi:schemalocation="https://java.sun.com/xml/ns/javaee 
  https://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd" 
    version="2.0"> 
    <namespace>https://myjsf.com/component</namespace> 
    <tag> 
        <tag-name>htmlhelloworld</tag-name> 
        <component> 
            <component-type>htmlhelloworld</component-type> 
        </component> 
    </tag> 
</facelet-taglib> 

<?xml version='1.0' encoding='utf-8'?>
<facelet-taglib xmlns="https://java.sun.com/xml/ns/javaee"
 xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="https://java.sun.com/xml/ns/javaee
  https://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
 version="2.0">
 <namespace>https://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
[html]
<?xml version="1.0" encoding="utf-8"?> 
<faces-config version="2.0" xmlns="https://java.sun.com/xml/ns/javaee" 
    xmlns:xi="https://www.w3.org/2001/xinclude" xmlns:xsi="https://www.w3.org/2001/xmlschema-instance" 
    xsi:schemalocation="https://java.sun.com/xml/ns/javaee https://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> 

<?xml version="1.0" encoding="utf-8"?>
<faces-config version="2.0" xmlns="https://java.sun.com/xml/ns/javaee"
 xmlns:xi="https://www.w3.org/2001/xinclude" xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="https://java.sun.com/xml/ns/javaee https://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组件类
[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("p", this); 
        writer.writeattribute("style", "color : red", null); 
        writer.writetext("helloworld! today is: " + new java.util.date(), null); 
        writer.endelement("p"); 
    } 

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("p", this);
  writer.writeattribute("style", "color : red", null);
  writer.writetext("helloworld! today is: " + new java.util.date(), null);
  writer.endelement("p");
 }
}
在项目上执行maven install,即可把项目打成jar包,上传到自己的maven仓库。

 


web项目
pom.xml中添加如下依赖:
[html]
<dependency> 
            <groupid>com.zolcorp.jsfcom</groupid> 
            <artifactid>jsfcom</artifactid> 
            <version>0.0.1</version> 
        </dependency> 

<dependency>
   <groupid>com.zolcorp.jsfcom</groupid>
   <artifactid>jsfcom</artifactid>
   <version>0.0.1</version>
  </dependency>
新增一个页面,测试下:
[html]
<?xml version="1.0" encoding="utf-8"?> 
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"  
"https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<html xmlns="https://www.w3.org/1999/xhtml" 
    xmlns:ui="https://java.sun.com/jsf/facelets" 
    xmlns:h="https://java.sun.com/jsf/html" 
    xmlns:f="https://java.sun.com/jsf/core" 
    xmlns:c="https://java.sun.com/jsp/jstl/core" 
    xmlns:zl="https://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>