JSF 1.2 facelets自定义组件

3

我正在尝试在JSF 1.2(使用facelets)中开发自定义控件。

我按照不同教程的步骤进行操作(定义.tld、taglib.xml,在faces-config.xml中注册组件并实现UIComponent(组件自身呈现)和UIComponentELTag类),我的组件被呈现出来,我将值绑定到它上面,但是我为该标签定义的属性被忽略了。我在Tag类中记录了各种方法,并注意到没有任何一个方法被调用。

我错过了什么?有没有什么原因导致Tag处理程序类从未被调用?

提前感谢您的回答。

我的taglib.xml文件如下:

<?xml version="1.0"?> 
<!DOCTYPE facelet-taglib PUBLIC 
  "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
  "java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib> 
  <namespace>dynamissoft.com/entities/ui</namespace>
  <tag>
    <tag-name>legalEntityView</tag-name>
    <component>
      <component-type>rs.bozic.wastemanager.LegalEntityView</component-type>
    </component>
  </tag>
</facelet-taglib> 

请发布您的 xxx.taglib.xml 文件。 - lexicore
我的taglib.xml文件内容如下: <?xml version="1.0"?> <!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"> http://www.dynamissoft.com/entities/ui legalEntityView rs.bozic.wastemanager.LegalEntityView - Vladimir
2个回答

3

您尝试过使用Facelets(仅XML)创建自定义组件吗?这是最简单的方法,通常不再需要不同的Java类。

非常粗略的概述:

  • 创建Facelet XML文件(如myComponent.xhtml)
  • 在标签库中注册Facelet(该标签库应在web.xml中定义)
  • 可选地,在Java中创建一些支持bean

您可以使用普通的标记参数将值/bean传递给组件:

使用组件

在组件内部

Param1只是打印:#{myParam2} Param2用作表格的值

...

有很棒的教程可以在Google上找到,比如来自IBM的教程。

如果可能的话,请考虑使用JSF 2.0。Facelets已经集成,您可以更灵活地创建自定义组件。我以前写过一篇博客文章:http://blog.whitehorses.nl/2010/02/08/jsf-2-0/(或者您自己在Google上搜索)


谢谢您的回复。是的,我已经尝试过了,但它要求我做一些额外的工作(在WAS CE上注册资源等),这是我试图避免的。 不管怎样,我还是做到了,所以我开始使用.xhtml文件来创建组件,现在它可以工作了。 不过,我仍然不知道之前发生了什么 :-(。 - Vladimir

2

为了进一步解释Gerbrand的答案,这里提供一个适用于Facelets的简单组件的过程。它呈现一个span标签,用组件的text属性指定的文本进行包装。

  1. First create the component class (in our case it's just a flavour of UIOutput):

      package sample.mytag;
    
      import java.io.IOException;
      import javax.faces.component.UIOutput;
      import javax.faces.context.FacesContext;
      import javax.faces.context.ResponseWriter;
    
      public class SpanComponent extends UIOutput{            
        private String text;
        @Override
        public Object saveState(FacesContext context) {
          Object values[] = new Object[2];
          values[0] = super.saveState(context);
          values[1] = target;
          return ((Object) (values));
        }
    
        @Override
        public void restoreState(FacesContext context, Object state) {
          Object values[] = (Object[])state;
          super.restoreState(context, values[0]);
          target = (String)values[1];
        }
    
        public String getText() {
          return text;
        }
    
        public void setText(String text) {
          this.text = text;
        }
    
        @Override
        public void encodeBegin(FacesContext context) throws IOException {
          ResponseWriter writer=context.getResponseWriter();
          writer.startElement("span", component);
          writer.writeAttribute("id", id, null);
          writer.writeText(text, null);
          writer.endElement("span");
          writer.flush();                
        }
    
        @Override
        public String getFamily(){
          return "myTag.component";
        }
    
        @Override
        public void encodeEnd(FacesContext context) throws IOException {
          return;
        }
    
        @Override
        public void decode(FacesContext context) {
          return;
        } 
      }
    
  2. Next, we need a taglib XML file, let's call it mytag.taglib.xml and put it inside WEB-INF dir.

      <!DOCTYPE facelet-taglib PUBLIC
        "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
        "WEB-INF/facelet-taglib_1_0.dtd"> 
      <facelet-taglib> 
      <namespace>http://sample.tag/mytags</namespace> 
      <tag>
        <tag-name>myspan</tag-name>
          <component>
            <component-type>myTag.component</component-type>    
          </component>
      </tag>
      </facelet-taglib> 
    

    Note that:

    • .taglib.xml suffix is mandatory
    • <component-type> should have the same value that is returned by component's getFamily() method
    • you can replace WEB-INF/facelet-taglib_1_0.dtd with http://java.sun.com/dtd/facelet-taglib_1_0.dtd
  3. It's time to modify web.xml and faces-config.xml.

    Former should be modified with

    <context-param> 
      <param-name>facelets.LIBRARIES</param-name> 
      <param-value>/WEB-INF/mytag.taglib.xml</param-value> 
    </context-param>     
    

    faces-config.xml should get

    <component> 
     <component-type>myTag.component</component-type> 
     <component-class>sample.mytag.LabelComponent</component-class> 
    </component>  
    
  4. We're good to go!

        <ui:composition
          xmlns="http://www.w3.org/1999/xhtml"
          xmlns:ui="http://java.sun.com/jsf/facelets"  
          xmlns:sample="http://sample.tag/mytag">  
    
          <sample:myspan text="I'm inside a span!"/>
    
        </ui:composition>
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接