2012-04-30 9 views
0

私は平易なStringでコンポーネントを処理することができましたが、今ではValueExpressionを解析する可能性を追加しようとしています。JSFカスタムコンポーネントのタグクラスをスキップしますか?

これを行うには、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.tag lib.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> 

のfaces-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> 

EDIT: 私は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> 

は、今の問題は、なぜ..but uicEmoticonOutputTextにキャストすることができないということです?

答えて

0

タグクラスは、間違ったクラスから拡張されています。 UIComponentELTagはJSP用です。 Faceletsでは、javax.faces.view.facelets.ComponentHandlerから拡張し、.taglib.xmlファイルに< handler-class> <タグ> <コンポーネント>を追加するタグハンドラを提供できます。

作成カスタムコンポーネントのすべての設定を扱いたくない場合は、MyFaces Builder Plugin Siteをご覧ください。ソースコード注釈を使用してすべてのメタデータを処理し、最小限の労力ですべての関連ファイルを生成します。 Apache MyFaces CoreApache MyFaces Tomahawk、MyFaces Sandboxで使用されるツールです。 maven See Instructionsを使用して環境をセットアップするのに役立つアーキタイプがあります。または、「簡単な方法」を採用して、JSF 2.0コンポジットコンポーネントを作成することもできます。

+0

私はUIComponentELTagを使用しました.OIComponentELTagはOracleのガイドに記載されているためです。 Faceletタグハンドラを書く方法の例はありますか?私はコンポーネントを組み合わせる以上のことをやらなければならないので、コンポジットコンポーネントを使用することはできません!私はmyfaces-archetype-jsfcomponents20をダウンロードしましたが、TagHandlerクラスはありません!名前は定義されていますが、ファイルはありません:\ – StepTNT

+0

編集した新しいコードを追加しました。新しいエラーが発生しました:D – StepTNT

+0

私は解決しました。ありがとう:) – StepTNT

関連する問題