On the way to componentization (6) -- Advantages of JSF: custom components and modularization

Source: Internet
Author: User

Many JSF documents have mentioned that to create a custom component, You need to define classes inherited from UIComponentBase and then write the decode/encode method. A large set of content is complicated. Fortunately, we have other methods. In fact, defining a component does not need to write such code in most cases. The content in this section is one of the two drop-down box components that are worth choosing true/false. To define such a component, you need to do the following:


1. Set it to a page to implement this component. The main content is as follows (in fact, the content of this page is not mandatory ):


<H: selectOneMenu id = "# {id}" value = "# {value}" rendered = "# {rendered}" styleClass = "# {styleClass}">
<! -- Whether to display the option corresponding to the null Value -->
<C: if test = "# {not empty noSelectionLabel}">
<F: selectItem itemLabel = "# {noSelectionLabel}"/>
</C: if>
<F: selectItem itemLabel = "# {empty trueLabel? Yes: trueLabel} "itemValue =" true "/>
<F: selectItem itemLabel = "# {empty falseLabel? No: falseLabel} "itemValue =" false "/>
</H: selectOneMenu>


Every # {xx} in the above Code is passed in as an attribute parameter when we finally use this component.


2. Register a Tag. You need to add a registration in *. taglib. xml under the class path:


<! -- This is the namespace of the tag library -->
<Namespace> http://www.a.com/jsf/facelets/tags </namespace>
<! -- Register the tag -->
<Tag>
<Tag-name> selectBooleanMenu </tag-name>
<Source> resources/jsf/components/selectBooleanMenu.xhtml </source>
</Tag>


In this way, we can use this tag on the page:


<Html xmlns = "http://www.w3.org/1999/xhtml"
Xmlns: h = "http://java.sun.com/jsf/html"
Xmlns: f = "http://java.sun.com/jsf/core"
Xmlns: jsf = "http://www.a.com/jsf/facelets/tags">
<Body>
<H: form>
If you eat or do not eat, the meal will be there: <br/>
<Jsf: selectBooleanMenu value = "# {someAction. booleanValue}" noSelectionLabel = "select" trueLabel = "eat" falseLabel = "don't eat"/>
</H: form>
</Body>
</Html>


In this way, the custom tag component is easy. In a group of pages (selectbooleanmenu.xhtml), you can use any backend bean. So even if some work needs to be completed with the background code, you do not need to implement any interface.


About modularity, in traditional mvc, you request/a. do To an ActionBean, process it, and then forward to the page. There is a problem: assume a. do and ActionA are used to process business a, B. do and ActionB are used to process business B. C. do integrates some functions of a and B, so you cannot use ActionA and ActionB on the page corresponding to c at the same time. So you have to go around.


In JSF, an expression is used on a page to reference any Bean in the background context. To create a c page, you can directly reference ActionA and ActionB on the c page to complete the function. In this way, you do not need to do something "mixed. The modularization of background code is easier to implement, and the separation between foreground display and background service processing is clearer.

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.