2012-04-29 16 views
2

2つの大きな質問があります。JSFカスタムコンポーネントのエラー+ ValueExpression

私はh:outputTextの機能を拡張するための単純なJSFコンポーネントを作成しようとしています。

私はこれを試してみたguideと何とか私は何か正しいことをしました。

私は正常に私のtaglibをインポートすることができ、すべてが細かいようだが、私はそれを使用する場合、私はこの例外を得た:

javax.servlet.ServletException: Expression Error: Named Object: EmoticonOutputTextTag not found.

は、私はこれを解決するために何ができますか?

すべてのコードを添付しています。

このコードは私が付けているコードに関連しているので、inputTextプロパティでValueExpressionを有効にするにはどうすればよいですか? Oracleのガイド助けにはならなかった:/

META-INF/EmoticonOutputText.tld

<?xml version="1.0" encoding="UTF-8"?> 
<taglib version="2.1" 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-jsptaglibrary_2_1.xsd"> 
    <tlib-version>1.0</tlib-version> 
    <short-name>eot</short-name> 
    <uri>com.unilife.emoticonOutputText</uri> 
    <tag> 
    <name>EmoticonOutputText</name> 
    <tag-class>com.unilife.emoticonOutputText.EmoticonOutputTextTag</tag-class> 
    <body-content>scriptless</body-content> 
    <attribute> 
     <name>style</name> 
     <rtexprvalue>true</rtexprvalue> 
     <type>java.lang.String</type> 
    </attribute> 
    <attribute> 
     <name>styleClass</name> 
     <rtexprvalue>true</rtexprvalue> 
     <type>java.lang.String</type> 
    </attribute> 
    <attribute> 
     <name>inputText</name> 
     <required>true</required> 
     <rtexprvalue>true</rtexprvalue> 
     <type>java.lang.String</type> 
    </attribute> 
    </tag> 
</taglib> 

/META-INF/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> 
      com.unilife.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> 
       com.unilife.emoticonoutputtext 
      </renderer-type> 
      <renderer-class> 
       com.unilife.emoticonOutputText.EmoticonOutputTextRenderer 
      </renderer-class> 
     </renderer> 
    </render-kit> 
</faces-config> 

/META-INF/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>EmoticonOutputTextTag</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> 

com.unilife.emoticonOutputText/EmoticonOutputText.java

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

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 = "com.unilife.emoticonoutputtext"; 

    /** 
    * 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; 
    } 
} 

com.unilife.emoticonOutputText/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(){ 
     emoticons.put(":)", basePath + "smile"); 
     emoticons.put(":-)", basePath + "smile"); 
     emoticons.put("=)", basePath + "smile"); 
     emoticons.put(":(", basePath + "frown"); 
     emoticons.put(":-(", basePath + "frown"); 
     emoticons.put("=(", basePath + "frown"); 
     emoticons.put(":p", basePath + "tongue"); 
     emoticons.put(":-p", basePath + "tongue"); 
     emoticons.put("=p", basePath + "tongue"); 
     emoticons.put(":D", basePath + "grin"); 
     emoticons.put(":-D", basePath + "grin"); 
     emoticons.put("=D", basePath + "grin"); 
     emoticons.put(":o", basePath + "gasp"); 
     emoticons.put(":-o", basePath + "gasp"); 
     emoticons.put(";)", basePath + "wink"); 
     emoticons.put(";-)", basePath + "wink"); 
     emoticons.put("8)", basePath + "glasses"); 
     emoticons.put("8-)", basePath + "glasses"); 
     emoticons.put("8|", basePath + "sunglasses"); 
     emoticons.put("8-|", basePath + "glasses"); 
     emoticons.put(">:(", basePath + "grumpy"); 
     emoticons.put(">:-(", basePath + "grumpy"); 
     emoticons.put(":\\", basePath + "unsure"); 
     emoticons.put(":-\\", basePath + "unsure"); 
     emoticons.put(":/", basePath + "unsure"); 
     emoticons.put(":-/", basePath + "unsure"); 
     emoticons.put(":'(", basePath + "cry"); 
     emoticons.put("3:)", basePath + "devil"); 
     emoticons.put("3-:)", basePath + "devil"); 
     emoticons.put("O:)", basePath + "angel"); 
     emoticons.put("O-:)", basePath + "angel"); 
     emoticons.put(":*", basePath + "kiss"); 
     emoticons.put(":-*", basePath + "kiss"); 
     emoticons.put("<3", basePath + "heart"); 
     emoticons.put("^_^", basePath + "kiki"); 
     emoticons.put("-_-", basePath + "squint"); 
     emoticons.put("o.O", basePath + "confused"); 
     emoticons.put("O.o", basePath + "confused"); 
     emoticons.put(">:O", basePath + "upset"); 
     emoticons.put(">:-O", basePath + "upset"); 
     emoticons.put(":v", basePath + "pacman"); 
     emoticons.put(":3", basePath + "colonthree"); 
    } 

    @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); 
      } 
     } 
    } 
} 

com.unilife.emoticonOutputText/EmoticonOutputTextTag.java

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

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

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

    private static final String COMP_TYPE = "com.unilife.emoticonoutputtext"; 
    private static final String RENDERER_TYPE = "com.unilife.emoticonoutputtext"; 
    private String style; 
    private String styleClass; 
    private String inputText; 

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

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

    public void setInputText(String 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); 
     } 
    } 
} 

あなたが見ることができるように、コードの非常に簡単(実際にはIドン私がそれをテストすることができないので、私は間違っている場所を知っていないし、それはカスタムコンポーネントで初めてです!)

私を助けることができるネ?

答えて

0

Expression Error: Named Object: EmoticonOutputTextTag not found.

これは、指定された「コンポーネントタイプ」が見つからないことを意味します。タグのgetComponentType()戻りcom.unilife.emoticonoutputtext、しかし、あなたは間違って代わり、タグのクラス名の上に登録されました:

<component-type>EmoticonOutputTextTag</component-type> 

はそれに応じて、それを修正。私はまた、命名規則に一貫しています。また、チュートリアル手順の後に手順を開始し、チュートリアルで言及されている方法とは異なる方法で作業する代わりに、実際の例を試してみましょう。

+0

あなたは正しいです、今は私が望むように動作します! 私のinputText属性にValueExpressionを有効にする方法の提案はありますか? 私はちょうどJSF 1.2のチュートリアルを見つけましたが、2.0は何もありません:\ – StepTNT

+0

JSF2を使用していますか?まあ、これは確かにJSF 1.2-ishです。 JSPとFaceletsの両方をサポートするために別々のtaglibファイルを作成しました。申し訳ありませんが、Oracle独自のJava EE 6チュートリアル以外のJSF2カスタムコンポーネントチュートリアルはわかりません。 http://docs.oracle.com/javaee/6/tutorial/doc/bnavg.html本当に面倒な読書ですが、そこにはJSF2のすべてが説明されています。一方、既存のJSFコンポーネント・ライブラリのソース・コードを調べることをお勧めします。たとえば、私と私の同僚によって維持管理されているOmniFaces:http://code.google.com/p/omnifaces/ – BalusC

+0

多分私はばかですが、Oracleのガイドは私を助けませんでした! ValueExpressionを受け入れるライブラリの中で最も単純なコンポーネントは何ですか?たぶんこれが何かを学ぶ唯一の方法です:) – StepTNT

関連する問題