Jsf2 custom component programming series Part 5

Source: Internet
Author: User
Tags glassfish

When I wrote this chapter, I did not think of many difficulties. Now, let's simply say:
1. Adding the namespace in the taglib. xml file looks pretty good, but it brings a lot of trouble-El expression failure. This is my post on java.net with another programmer.
Http://www.java.net/forum/topic/glassfish/glassfish-webtier/el-composite-component-taglib-jsf20
Currently, my solution is to bypass this problem and only use the standard namespace, that is, http://java.sun.com/jsf/composite/tag_folder_path. At the same time, I simply searched the source code of primefaces and found that it was not implemented using composite component, because no XHTML file was found.

2. <the complete reference-Java Server faces2.0>
A. Chapter 2, page 2,
Public class loginpanel extends uinamingcontainer
This line of code is totally wrong. The correct one should be
Public class loginpanel extends uiinput implements uinamingcontainer
B. The book does not provide examples to demonstrate how to package a composite component with a backing class into a jar package. Therefore, the code snippets provided in the book are untrusted.
The alias of CC in c.xhtml is the same as that of the default CC.

<HTML xmlns = "http://www.w3.org/1999/xhtml" xmlns: H = "http://java.sun.com/jsf/html"
Xmlns: CC = "http://java.sun.com/jsf/composite">
<CC: interface/>
<CC: Implementation>
<H: panelgrid columns = "3">
<H: outputlabel for = "# {cc. clientid}: userid" value = "userid:"/>
<H: inputtext required = "true"
Requiredmessage = "userid is required" id = "userid"/>
<H: Message for = "# {cc. clientid}: userid"/>
<H: outputlabel for = "# {cc. clientid}: Password" value = "Password:"/>
<H: inputsecret required = "true"
Requiredmessage = "password is required" id = "password"/>
<H: Message for = "# {cc. clientid}: Password"/>
<H: outputtext value = "on login, go :"
Rendered = "#{! Empty cc. facets. loginoutcomechoicelist} "/>
<H: commandbutton id = "loginbutton" value = "login"/>
<H: messages for = "# {cc. clientid}"/>
</H: panelgrid>
</CC: Implementation>
</Html>

In the code above, xmlns: CC = "http://java.sun.com/jsf/composite
It is easy to mislead people. We recommend that you use xmlns: Composite instead of xmlns: CC. Because in the # {cc.} syntax, CC is a predefined Java object
Table top-level object namingcontainer of composite component

A lot of questions are not understood until you read the third edition of <core JSF>.

Okay. Start now.
First, implement the htmlinput2.xhtml code:
<? 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: F = "http://java.sun.com/jsf/core"
Xmlns: H = "http://java.sun.com/jsf/html"
Xmlns: Ui = "http://java.sun.com/jsf/facelets"
Xmlns: Composite = "http://java.sun.com/jsf/composite">
<Composite: interface componenttype = "htmlinput2">
<Composite: editablevalueholder name = "inputfield" target = "in"/>
<Composite: valueholder name = "outputfield" target = "out"/>
</Composite: interface>
<Composite: Implementation>
<H: inputtext id = "in" required = "true"/>
<H: commandbutton id = "clickbutton" value = "Click me! "/>
<H: outputtext id = "out"/>
</Composite: Implementation>
</Html>

Composite: interface specifies componenttype, JSF
Runtime uses it to find htmleinput2 class in the faces-config.xml file, this class implements the namingcontainer interface, inherits
Uiinput class. JSF runtime creates this class as the top-level object of customized component.
<Component>
<Component-type> htmlinput2 </component-type>
<Component-class> com. freebird. component. htmlinput2 </component-class>
</Component>
If you do not specify componenttype, JSF
Runtime searches for htmlinput2.java Based on the XHTML file name htmlinput2.
The namingcontainer interface inherits the uiinput class and creates this class as the top-level object of customized component.
If not, JSF runtime creates the default implementation class of the namingcontainer interface as the top-level object.
When should we specify compnenttype? When you want some java code background to help tag process some behavior.
For more information, see <core JSF> Chapter 9 of the third edition.

Now let's take a look at the implementation of the htmleinput2 class:
Package com. freebird. component;

Import javax. Faces. component. namingcontainer;
Import javax. Faces. component. uioutput;
Import javax. Faces. component. uiinput;
Import javax. Faces. event. actionevent;
Import java. util. Logging. level;
Import java. util. Logging. Logger;
Import java. Io. filewriter;
Import java. Io. bufferedwriter;
Import java. Io. filereader;
Import java. Io. bufferedreader;
Import java. Io. file;
Import javax. Faces. component. facescomponent;
Import javax. Faces. event. actionlistener;
Import javax. Faces. Convert. converterexception;
Import javax. Faces. Context. facescontext;
Import java. Io. ioexception;
Import javax.faces.component.html. htmlinputtext;
Import javax.faces.component.html. htmloutputtext;
Import javax. Enterprise. Context. applicationscoped;
Import java. util. Map;

/**
* Describe class htmlinput2 here.
*
*
* Created: Sat Jan 1 16:08:53 2011
*
* @ Author <a href = "mailto: chenshu @ csdesktop"> chenshu </a>
* @ Version 1.0
/
Public class htmlinput2 extends uiinput implements namingcontainer {

Public htmlinput2 (){
Getlogger (). Info ("htmlinput2 constructor ");
}

@ Override
Public String getfamily (){
Return "javax. Faces. namingcontainer ";
}

Private logger getlogger (){
Return logger. getlogger (htmlinput2.class. getname ());
}

Public void print (actionevent event ){
Getlogger (). Info ("Enter print method ");
}

Public void encodebegin (facescontext context) throws ioexception {
Map requestmap = context. getexternalcontext (). getrequestparametermap ();
String clientid = getclientid (context );
Getlogger (). Info ("clientid:" + clientid );
String inputvalue = (string) requestmap. Get (clientid + ": In ");
Htmlinputtext input = (htmlinputtext) findcomponent ("in ");
If (null = input ){
Getlogger (). Info ("can't find input component instance ");
Super. encodebegin (context );
Return;
}
Htmloutputtext output = (htmloutputtext) findcomponent ("out ");
If (null = output ){
Getlogger (). Info ("can't find output component instance ");
Output. setvalue ("null ");
Super. encodebegin (context );
Return;
}
Getlogger (). Info ("input value is:" + inputvalue );
Output. setvalue (inputvalue );
Super. encodebegin (context );
}
}

There are so many codes. When you enter data on the page, click the button to display the data you just entered. The specific example is uploaded to my space immediately.

 

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.