Jsf2 custom component programming series Part 1

Source: Internet
Author: User



The reason for this article is that with primefaces, we are amazed at the various cool UIS.
Components is also troubled by difficulties but the community has little support. Looking back to the years when JSF was used, from icefaces to primefaces
We are busy with commercial support, and seldom provide friendly and timely support to free users.
<The complete reference-Java Server
Faces2.0> the book mentions that if someone else has built a wheel, there is no need to build a wheel on their own. Yes. Generally, this is the case. But in the world of JSF
In reality, it is hard to get the support of the open-source community. On the one hand, it is used for itself, and on the other hand, it is to better understand JSF and other open-source libraries, if the third-party JSF library has problems, you can
Instead of relying on others.
All the code in this article adopts the technology: maven3, Emacs and jdee (you can also use other text editors such as VI). JSF selects the standard implementation mojarra 2.0.3-b03, glassfishv3.0.1, and is developed under ubuntu10.10.
This article references mainly from <the complete reference-Java Server faces2.0> book, as well as http://www.mkyong.com/jsf2/custom-tags-in-jsf-2-0


To be consistent with the mainstream JSF terminology, some of them use English without translation.
Class corresponding to the background of the uicomponent JSF tag
The overall structure of the UI component JSF tag and all classes and configuration files corresponding to the background
Composite component jsf2 provides Composite
The component creation method avoids the tedious process of generating HTML tags using Java code.
The tag can be combined. However, to learn the internal mechanism of JSF, it is necessary to compile the traditional non composite component.

Part 1 simple noncomposite component

Run MVN archetype: Generate. Many JSF template projects are selected, but none of them are satisfactory. Therefore, you can only create a simple jar package project by yourself.
MVN archetype: generate-dgroupid = com. freebird-dartifactid = jsfex
-Darchetypeartifactid = Maven-Archetype-Quickstart-dinteractivemode = false
Well, now Maven has created a project named jsfex. I modified Pom. XML as follows:
<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/maven-v4_0_0.xsd">
<Modelversion> 4.0.0 </modelversion>
<Groupid> com. freebird </groupid>
<Artifactid> jsfex </artifactid>
<Packaging> jar </packaging>
<Version> 1.0 </version>
<Name> jsfex </Name>
<URL> http://maven.apache.org </URL>
<Groupid> javax </groupid>
<Artifactid> javaee-web-API </artifactid>
<Version> 6.0 </version>
<Scope> provided </scope>
<Groupid> CH. QoS. logback </groupid>
<Artifactid> logback-classic </artifactid>
<Version> 0.9.27 </version>

Note: In src/main/resources/create a META-INF directory, put the helloworld. taglib. xml file in it, will be automatically put into the target directory jar files in the META-INF directory. Content of the helloworld. taglib. XM file:
<? 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://com.freebird/jsfex </namespace>
<Tag-Name> helloworld </Tag-Name>
<Component-type> htmlhelloworld </component-type>

Then put the faces-config.xml file, the content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Faces-config version = "2.0" 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-facesconfig_2_0.xsd">
<Component-type> htmlhelloworld </component-type>
<Component-class> com. freebird. component. htmlhelloworld </component-class>

It is said that annotation can avoid configuring compoent information in the faces-config.xml file, but I succeeded only when the code is written in the Web Application
When I pack my tags independently, I always prompt that the class cannot be found. This solution may be solved through the following article. I have not tried to provide a URL address:

The code for implementing the uicomponent class is as follows:

Package com. freebird. component;

Import javax. Faces. component. uicomponentbase;
Import javax. Faces. Context. responsewriter;
Import java. Io. ioexception;
Import javax. Faces. Context. facescontext;
Import java. util. date;

* Describe class htmlhelloworld here.
* Created: Mon Dec 27 14:39:47 2010
* @ Author <a href = "mailto: chenshu @ csdesktop"> chenshu </a>
* @ Version 1.0
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 ");


Now, put this jar package into a jsf2 application to test the pipeline. We will not introduce how to write the jsf2 application here. It is beyond the scope of this article. Writing a jsf2 application using Maven requires many steps, because I didn't find my favorite template. I have to customize it from the beginning. I use primefaces, mybatis, in the future, spring3.
In other articles.
First, register your own jar package and run the following command:
MVN clean install
MVN install: Install-file-dfile = target/jsfex-1.0.jar-dgroupid = com. freebird-dartifactid = jsfex-dversion = 1.0-dpackaging = jar

In the web application to be used, you only need to add the following configuration in POM. xml:
<Groupid> com. freebird </groupid>
<Artifactid> jsfex </artifactid>
<Version> 1.0 </version>

Use the following on the facelets page:
Xmlns: cs = "http://com.freebird/jsfex"/>
<H: Form>
<CS: helloworld/>
</H: Form>

After the compiled web application is deployed, the page displays the following results:
Helloworld! Todayis: Mon Dec 27 20:06:10 CST 2010


Unfinished, to be continued




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.