JSF 1.2 Facelets 自定义组件

发布于 2024-09-10 04:56:08 字数 821 浏览 10 评论 0原文

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

我按照不同教程中的步骤(定义 .tld、taglib.xml、faces-config.xml 中注册的组件并实现 UIComponent(组件呈现自身)和 UIComponentELTag 类),并且我的组件已呈现,我有绑定到它的值,但是属性我为该标签定义的内容将被忽略。我在 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> 

I am trying to develop custom control in JSF 1.2 (using facelets).

I followed steps from different tutorials (defining .tld, taglib.xml, registered component in faces-config.xml and implementing UIComponent (component renders itself) and UIComponentELTag classes) and my component is rendered, I have value bound to it, but attributes I defined for that tag are ignored. I logged various methods in Tag class and noticed that none of the methods is ever called.

What am I missing? Is there a reason Tag handler class is never invoked?

Thanks in advance.

My taglib.xml file is:

<?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> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昵称有卵用 2024-09-17 04:56:08

您是否尝试过使用facelets(仅限xml)创建自定义组件。这是最简单的方法,使用facelets,通常不再需要不同的java类。

非常粗略的概述:

  • 创建facelet xml文件(如myComponent.xhtml)
  • 在taglib内的facelet中注册(这又应该在web.xml中定义)
  • 或者,在Java中创建一些支持bean

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

使用组件

组件

Param1 内部刚刚打印:#{myParam2}
Param2 用作表的值

...

Google 上有很棒的教程,例如来自IBM

如果可能,请考虑使用 JSF 2.0。 Facelet 是集成的,您可以更灵活地创建自定义组件。我不久前创建了一篇博客文章: http:// blog.whitehorses.nl/2010/02/08/jsf-2-0/ (或 Google 自己)

Have you tried creating a custom component using facelets (xml only). That's the most easy way, using facelets, usually, the different java classes aren't needed anymore.

Very rough overview:

  • Create facelet xml file (like myComponent.xhtml)
  • Register in the facelet inside a taglib (which in turn should be defined in the web.xml)
  • Optionally, create some support beans in Java

You can pass values/beans to your component using normal tag paramets:

Using the component

Inside the component

Param1 is just printed: #{myParam2}
Param2 used as value for table

...

There are excellent tutorials on Google, like the one from IBM.

If possible, consider using JSF 2.0. Facelets are integrated, and you have more flexibility to create your custom components. I created a blog posting a while ago on that: http://blog.whitehorses.nl/2010/02/08/jsf-2-0/ (or Google yourself)

彼岸花ソ最美的依靠 2024-09-17 04:56:08

只是为了扩展 Gerbrand 的答案,这里有一个简单的 Facelets 兼容组件的过程。它呈现一个 span 标签,该标签包装通过组件的 text 属性指定的文本。

  1. 首先创建组件类(在我们的例子中,它只是
    UIOutput):

     包sample.mytag;
    
      导入java.io.IOException;
      导入 javax.faces.component.UIOutput;
      导入 javax.faces.context.FacesContext;
      导入 javax.faces.context.ResponseWriter;
    
      公共类 SpanComponent 扩展 UIOutput{            
        私有字符串文本;
        @覆盖
        公共对象 saveState(FacesContext 上下文){
          对象值[] = 新对象[2];
          值[0] = super.saveState(context);
          值[1] = 目标;
          返回((对象)(值));
        }
    
        @覆盖
        公共无效恢复状态(FacesContext上下文,对象状态){
          对象值[] = (Object[])状态;
          super.restoreState(上下文, 值[0]);
          目标=(字符串)值[1];
        }
    
        公共字符串 getText() {
          返回文本;
        }
    
        公共无效setText(字符串文本){
          this.text = 文本;
        }
    
        @覆盖
        公共无效encodeBegin(FacesContext上下文)抛出IOException {
          ResponseWriter writer=context.getResponseWriter();
          writer.startElement("span", 组件);
          writer.writeAttribute("id", id, null);
          writer.writeText(文本,空);
          writer.endElement("span");
          writer.flush();                
        }
    
        @覆盖
        公共字符串 getFamily(){
          返回“myTag.组件”;
        }
    
        @覆盖
        公共无效encodeEnd(FacesContext上下文)抛出IOException{
          返回;
        }
    
        @覆盖
        公共无效解码(FacesContext上下文){
          返回;
        } 
      }
    
  2. 接下来,我们需要一个 taglib XML 文件,我们将其命名为 mytag.taglib.xml 并将其放入 WEB-INF 中> 目录。

      
       
      <命名空间>http://sample.tag/mytags 
      <标签>
        <标签名称>myspan
          <组件>
            <组件类型>myTag.组件    
          
      
       
    

    请注意:

    • .taglib.xml 后缀为必填
    • 应该具有相同的
      组件的 getFamily() 方法返回的值
    • 你可以
      将 WEB-INF/facelet-taglib_1_0.dtd 替换为
      http://java.sun.com/dtd/facelet-taglib_1_0.dtd
  3. 是时候修改 web.xml 和 faces-config.xml 了。

    前者应修改为

    <上下文参数> 
      <参数名称>facelets.LIBRARIES 
      <参数值>/WEB-INF/mytag.taglib.xml 
         
    

    faces-config.xml 应该得到

    <前><代码><组件>
    <组件类型>myTag.组件
    <组件类>sample.mytag.LabelComponent

  4. 应该得到

    We're good to go!

       
    
          
    
        
    

Just to expand Gerbrand's answer a bit, here's a procedure for a simple Facelets-compatible component. It renders a span tag that wraps a text specified via component's text attribute.

  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>
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文