0

我已经设法让我的组件与普通字符串一起工作,现在我正在尝试添加解析ValueExpression.

为此,我正在编辑我的 EmoticonOutputTextTag 文件,以添加setProperties()对表达式的评估。

但我注意到这个方法永远不会被调用。

好像这个类没有使用,我不知道为什么。

事实上,在我的代码中setProperties()我已经写了这行 :eot.setInputText("randomText");并且我希望我的组件会显示这一点。事实上,我的组件会显示在 JSF 页面中传递的值,所以我猜测这个方法没有被调用。

我应该怎么办?

这是代码:

EmoticonOutputText.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.unilife.emoticonOutputText;

import javax.el.ValueExpression;
import javax.faces.component.UIOutput;

/**
 *
 * @author stefano
 */
public class EmoticonOutputText extends UIOutput {

    private static final String COMP_FAMILY = "javax.faces.Output";

    /**
     * Get the value of COMPONENT_FAMILY
     *
     * @return the value of COMPONENT_FAMILY
     */
    @Override
    public String getFamily() {
        return COMP_FAMILY;
    }
    private static final String RENDERER_TYPE = "EmoticonOutputTextRenderer";

    /**
     * Get the value of RENDERER_TYPE
     *
     * @return the value of RENDERER_TYPE
     */
    @Override
    public String getRendererType() {
        return RENDERER_TYPE;
    }
    private String style;

    /**
     * Get the value of style
     *
     * @return the value of style
     */
    public String getStyle() {
        return style;
    }

    /**
     * Set the value of style
     *
     * @param style new value of style
     */
    public void setStyle(String style) {
        this.style = style;
    }
    private String styleClass;

    /**
     * Get the value of styleClass
     *
     * @return the value of styleClass
     */
    public String getStyleClass() {
        return styleClass;
    }

    /**
     * Set the value of styleClass
     *
     * @param styleClass new value of styleClass
     */
    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }
    private String inputText;

    /**
     * Get the value of inputText
     *
     * @return the value of inputText
     */
    public String getInputText() {
        return inputText;
    }

    /**
     * Set the value of inputText
     *
     * @param inputText new value of inputText
     */
    public void setInputText(String inputText) {
        this.inputText = inputText;
    }
}

EmoticonOutputTextTag.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.unilife.emoticonOutputText;

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

/**
 *
 * @author stefano
 */
public class EmoticonOutputTextTag extends UIComponentELTag {

    private static final String COMP_TYPE = "EmoticonOutputTextTag";
    private static final String RENDERER_TYPE = "EmoticonOutputTextRenderer";
    private String style;
    private String styleClass;
    private ValueExpression inputText;

    public void setStyle(String style) {
        this.style = style;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public void setInputText(ValueExpression inputText) {
        this.inputText = inputText;
    }

    @Override
    public String getComponentType() {
        return COMP_TYPE;
    }

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

    @Override
    protected void setProperties(UIComponent component) {
        super.setProperties(component);
        EmoticonOutputText eot = (EmoticonOutputText)component;        
        if(style != null){
            eot.setStyle(style);
        }
        if(styleClass != null){
            eot.setStyleClass(styleClass);
        }
        if(inputText != null){
//            eot.setInputText(inputText.getExpressionString());
            eot.setInputText("randomText");
        }        
    }    
}

EmoticonOutputTextRenderer.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.unilife.emoticonOutputText;

import java.io.IOException;
import java.util.HashMap;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;
import javax.servlet.ServletContext;

/**
 *
 * @author stefano
 */
public class EmoticonOutputTextRenderer extends Renderer {

    //Contiene la corrispondenza tra la stringa da sostituire e il nome dell'emoticon
    private static final HashMap<String, String> emoticons = new HashMap<>();
    //Contiene il percorso dei files delle emoticon
    private final String basePath = ((ServletContext) (FacesContext.getCurrentInstance().getExternalContext().getContext())).getContextPath() + "/resources/images/emoticons/";

    public EmoticonOutputTextRenderer() {
        parseEmoticons();
    }

    private void parseEmoticons(){
        //Not needed 
    }

    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        EmoticonOutputText eot = (EmoticonOutputText) component;
        ResponseWriter writer = context.getResponseWriter();
        //Aggiungiamo l'eventuale stile CSS o direttamente la classe
        writer.startElement("span", null);
        if(eot.getStyle()!=null && !eot.getStyle().isEmpty()){
            writer.writeAttribute("style", eot.getStyle(), null);
        }
        if(eot.getStyleClass()!=null && !eot.getStyleClass().isEmpty()){
            writer.writeAttribute("class", eot.getStyleClass(), null);
        }
        //Andiamo ad effettuare il parse vero e proprio, sostituendo le emoticons come le immagini
        for(String str : eot.getInputText().split(" ")){
            if(emoticons.containsKey(str)){ //Se riconosco l'emoticon allora scrivo l'immagine
                writer.startElement("img", null);
                writer.writeAttribute("src", emoticons.get(str) + ".gif", null);
                writer.endElement("img");
                writer.writeText(" ", null);
            } else { //Altrimenti aggiungo semplicemente la parola
                writer.writeText(str + " ", null);
            }
        }
    }
}

unilife.taglib.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
    <namespace>http://unilife.it/tags</namespace>
    <tag>
        <tag-name>EmoticonOutputText</tag-name>
        <description>
            OutputText con la possibilità di mostrare Emoticons
        </description>
        <component>
            <component-type>EmoticonOutputText</component-type>
            <renderer-type>EmoticonOutputTextRenderer</renderer-type>
        </component>
        <attribute>
            <name>style</name>            
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <name>styleClass</name>            
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <name>inputText</name>
            <required>true</required>            
            <type>java.lang.String</type>
        </attribute>
    </tag>
</facelet-taglib>

面孔-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="1.2" 
                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_1_2.xsd">
    <component>
        <component-type>
            EmoticonOutputText
        </component-type>
        <component-class>
            com.unilife.emoticonOutputText.EmoticonOutputText
        </component-class>
    </component>

    <render-kit>
        <renderer>
            <description>
                OutputText che permette il rendering di emoticons al posto delle combinazioni di tasti
            </description>
            <component-family>
                javax.faces.Output
            </component-family>
            <renderer-type>
                EmoticonOutputTextRenderer
            </renderer-type>
            <renderer-class>
                com.unilife.emoticonOutputText.EmoticonOutputTextRenderer
            </renderer-class>
        </renderer>
    </render-kit>  
</faces-config>

编辑:我在 lu4242 的回答之后编辑了一些东西,但现在我有一个 ClassCastException!

EmoticonOutputTextTag.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.unilife.emoticonOutputText;

import java.io.IOException;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.view.facelets.ComponentConfig;
import javax.faces.view.facelets.FaceletContext;
import javax.faces.view.facelets.TagAttribute;
import javax.faces.view.facelets.TagHandler;

/**
 *
 * @author stefano
 */

public class EmoticonOutputTextTag extends TagHandler {

    private static final String COMP_TYPE = "EmoticonOutputTextTag";
    private static final String RENDERER_TYPE = "EmoticonOutputTextRenderer";
    private String style;
    private String styleClass;
    private ValueExpression inputText;

    public EmoticonOutputTextTag(ComponentConfig config) {
        super(config);
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public void setInputText(ValueExpression inputText) {
        this.inputText = inputText;
    }

    @Override
    public void apply(FaceletContext fc, UIComponent uic) throws IOException {
        EmoticonOutputText eot = (EmoticonOutputText) uic;        
        TagAttribute ta = this.getRequiredAttribute("inputText");
        ValueExpression ve = ta.getValueExpression(fc, String.class);
        if(ve.isLiteralText()){
            eot.setInputText(ve.getExpressionString());
        } else {
            eot.setInputText((String)ve.getValue(fc));
        }
    }
}

unilife.taglib.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
    <namespace>http://unilife.it/tags</namespace>
    <tag>
        <tag-name>EmoticonOutputText</tag-name>
        <description>
            OutputText con la possibilità di mostrare Emoticons
        </description>
        <component>
            <component-type>EmoticonOutputText</component-type>
            <renderer-type>EmoticonOutputTextRenderer</renderer-type>
            <handler-class>com.unilife.emoticonOutputText.EmoticonOutputTextTag</handler-class>            
        </component>
        <attribute>
            <name>style</name>            
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <name>styleClass</name>            
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <name>inputText</name>
            <required>true</required>            
            <type>java.lang.String</type>
        </attribute>
    </tag>
</facelet-taglib>

现在的问题是uic不能转换为EmoticonOutputText..但是为什么呢?

4

1 回答 1

0

标记类从错误的类扩展。UIComponentELTag 用于 JSP。在 Facelets 中,可以为从 javax.faces.view.facelets.ComponentHandler 扩展的组件提供标签处理程序,并在 .taglib.xml 文件的 <tag><component> 内添加 <handler-class>。

如果您不想处理创建自定义组件的所有配置内容,请查看MyFaces Builder Plugin Site。它使用源代码注释来处理所有元数据并以最小的努力生成所有相关文件。它是用于Apache MyFaces CoreApache MyFaces Tomahawk和 MyFaces Sandbox 的工具。有一个原型可以帮助使用 maven 设置环境,请参阅说明。或者您可以采用“简单的方法”并创建一个 JSF 2.0 复合组件。

于 2012-05-01T01:39:57.463 回答