java - 使用 JSF 的自定义组件

标签 java jsf jsf-2

我想创建自己的标签,但它不起作用

组件:

package com;

import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import java.io.IOException;

public class Printer extends UIOutput {
    private String label;

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    @Override
    public Object saveState(FacesContext context) {
        Object values[] = new Object[2];
        values[0] = super.saveState(context);
        values[1] = label;
        return ((Object) (values));
    }

    @Override
    public void restoreState(FacesContext context, Object state) {
        Object values[] = (Object[]) state;
        super.restoreState(context, values[0]);
        label = (String) values[1];
    }

    public void encodeBegin(FacesContext context)
            throws IOException {

        ResponseWriter writer =
                context.getResponseWriter();

        writer.startElement("label", this);
        writer.write(label);
        writer.endElement("label");
        writer.flush();
    }

    public String getFamily() {
        return "printer";
    }

    public void encodeEnd(FacesContext context)
            throws IOException {
        return;
    }

    public void decode(FacesContext context) {
        return;
    }
}

标签

package com;

import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentELTag;

public class PrinterTag extends UIComponentELTag {
    private String label;

    public PrinterTag() {
        int i = 10;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    @Override
    public String getComponentType() {
        return "printer";
    }

    @Override
    public String getRendererType() {
        return null;
    }

    protected void setProperties(UIComponent component) {
        super.setProperties(component);
        ((Printer) component).setLabel(label);
    }
}

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <context-param>
        <description>
        </description>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>
            /WEB-INF/faces-config.xml
        </param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <jsp-config>
        <taglib>
            <taglib-uri>http://ff.com</taglib-uri>
            <taglib-location>/WEB-INF/ff.tld</taglib-location>
        </taglib>
    </jsp-config>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>
</web-app>

顶级域名

<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1">

    <display-name>RichFaces</display-name>
    <tlib-version>3.3.3</tlib-version>
    <short-name>ff</short-name>
    <uri>http://ff.com</uri>
    <tag>
        <name>mylabel</name>
        <tag-class>com.PrinterTag</tag-class>
        <body-content>tagdependent</body-content>
        <attribute>
            <description>The attribute takes a value-binding expression for a component property of
                a backing bean
            </description>
            <name>label</name>
            <rtexprvalue>false</rtexprvalue>
            <type>java.lang.String</type>
        </attribute>
    </tag>
</taglib>

人脸配置

<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
              version="2.0">
    <component>
        <component-type>printer</component-type>
        <component-class>com.Printer</component-class>
    </component>
</faces-config>

测试.xhtml

<!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:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:ff="http://ff.com">

<h:head>
    <title>Simple JSF Facelets page</title>
</h:head>
<h:body>
     before-<ff:mylabel label="This is my first tag"/>-after
</h:body>
</html>

(打印之前——之后)

我正在使用 2.0.1-FCS jsf-api/jsf.impl/jSTL-1.1.0 jsp-api/servlet-api/标准

我哪里错了?

最佳答案

您已经在 J​​SP TLD 中定义了标记,而您正在使用 JSP 的后继 Facelets。您需要在 Facelets TLD 中定义标记。

阅读有关创建自定义 JSF 组件的教程/书籍时,请确保您阅读的是针对 JSF 2.0 的,而不是针对 JSF 1.2 的。

另见:

关于java - 使用 JSF 的自定义组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7598036/

相关文章:

java - Apache 公共(public)配置 : Generate configuration file from defaults

jsf - Tomcat @Resource 注释 API 注释在 Tomcat 7 中停止工作

mysql - JSF 应用程序创建过多数据库连接

java - 由于 FacesValidator,ManageBean 方法不再有效?

html - JSF 2如何根据浏览器版本设置html样式

java - 从 Drools 6.3 中的 xml 读取规则

java - Full GC 什么时候触发?

java - 如何在 Spring RabbitMQ 项目中使用多个虚拟主机?

javascript - PrimeFaces 5.2 及更新版本中图表扩展器属性的替代项是什么

Spring JSF 集成纯 Java 配置(无 web.xml)