Custom Facelet component in JSF

前端 未结 1 1818
谎友^
谎友^ 2020-12-01 15:22

Is it possible to create a custom JSF core Facelet component. Something like of , or

1条回答
  •  青春惊慌失措
    2020-12-01 16:11

    It are in essence taghandlers. I.e. classes extending from TagHandler.

    Here's a Hello World taghandler.

    com.example.HelloTagHandler

    public class HelloTagHandler extends TagHandler {
    
        public HelloTagHandler(TagConfig config) {
            super(config);
        }
    
        @Override
        public void apply(FaceletContext context, UIComponent parent) throws IOException {
            // Do your job here. This example dynamically adds another component to the parent.
            if (ComponentHandler.isNew(parent)) {
                UIOutput child = new HtmlOutputText();
                child.setValue("Hello World");
                parent.getChildren().add(child);
            }
    
            nextHandler.apply(context, parent); // Delegate job further to first next tag in tree hierarchy.
        }
    
    }
    

    /WEB-INF/my.taglib.xml

    
    
        http://example.com/my
        
            hello
            com.example.HelloTagHandler
        
    
    

    /WEB-INF/web.xml (note: this part is not mandatory when my.taglib.xml is in /META-INF folder of a JAR file inside /WEB-INF/lib like as with JSF component libraries):

    
        javax.faces.FACELETS_LIBRARIES
        /WEB-INF/my.taglib.xml
    
    

    Usage in /some.xhtml

    
    ...
    
    

    To see the source code of Mojarra implementation of and , click the links.

    See also:

    • When to use , tag files, composite components and/or custom components?

    0 讨论(0)
提交回复
热议问题