2016-07-19 4 views
0

私の問題は、ページをロードするときにこのエラーが発生することです。私は私のselectOneMenuフィールドのためのコンバータを使用しようとしている、それはシンプルでなければならないが、私は解決策を見つけることができません。データベースにはデータがあり、エラーログはありません。コンバータが正しく動作しない

enter image description here

コンバータ:

@FacesConverter(forClass = Tag.class)//Tag is the class where I get my Tag. 
public class ConverteGadoTag implements Converter { 

@Inject 
private TagsRep tagsRep; //this is my repository 

@Override //this is BalusC code. 
    public Object getAsObject(FacesContext context, UIComponent component, String value) { 
     if (value == null || value.isEmpty()) { 
      return null; 
     } 

     if (!value.matches("\\d+")) { 
      throw new ConverterException("The value is not a valid ID number: " + value); 
     } 



     Long id = new Long(value); 
     return tagsRep.porId(id); 
     // You may want to perform an additional check if it didn't return null 
     // and otherwise throw ConverterException with "unknown Product ID". 
    } 

    @Override  
    public String getAsString(FacesContext context, UIComponent component, Object value) {   
     if (value == null) { 
      return null; // Or an empty string, can also. 
     } 

     if (!(value instanceof Tag)) { 
      throw new ConverterException("The value is not a valid Product: " + value); 
     } 

     Long id = ((Tag)value).getTagId(); 
     return (id != null) ? String.valueOf(id) : null; 
    } 

} 

マイXHTMLページ:

<ui:composition template="/WEB-INF/template/LayoutPadrao.xhtml" 
xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h ="http://java.sun.com/jsf/html" 
xmlns:f ="http://java.sun.com/jsf/core" 
xmlns:ui ="http://java.sun.com/jsf/facelets" 
xmlns:p ="http://primefaces.org/ui" 
xmlns:o="http://omnifaces.org/ui"> 

<ui:define name="titulo">Novo Gado</ui:define> 

<ui:define name="corpo"> 
<f:metadata> 
    <o:viewParam name="gado" value="#{cadastroGadoBean.gado}" converter="# {converteGadoTag}"/> 
    <f:event listener="#{cadastroGadoBean.inicializar}" type="preRenderView"/> 
</f:metadata> 


<h:form> 
    <h1>Novo Gado</h1> 

    <p:messages autoUpdate="true" closable="true"/> 

    <p:toolbar style="margin-top: 20px"> 
     <p:toolbarGroup> 
      <p:button value="Novo" outcome="/gado/CadastroGado"/> 
      <p:commandButton value="Salvar" id="botaoSalvar" action="#{cadastroGadoBean.salvar}" update="@form"/> 
     </p:toolbarGroup> 

    </p:toolbar> 

    <p:panelGrid columns="2" id="painel" style="width: 100%; margin-top: 20px" 
    columnClasses="rotulo, campo"> 

     <p:outputLabel value="Tipo" for="gadoTipo"/> 
     <p:inputText id="gadoTipo" size="60" maxlength="80" 
     value="#{cadastroGadoBean.gado.gadoTipo}"/> 

     <p:outputLabel value="Peso" for="gadoPeso"/> 
     <p:inputText id="gadoPeso" size="60" maxlength="80" 
     value="#{cadastroGadoBean.gado.gadoPeso}"/> 

     <p:outputLabel value="Sexo" for="sexo"/> 
     <p:inputText id="sexo" size="60" maxlength="80" 
     value="#{cadastroGadoBean.gado.sexo}"/> 

     <p:outputLabel value="Nascimento" for="gadoNasc"/> 
     <p:calendar id="gadoNasc" size="10" pattern="dd/MM/yyyy" 
     value="#{cadastroGadoBean.gado.gadoNasc}" /> 

     <p:outputLabel value="Raça" for="gadoRaca"/> 
     <p:inputText id="gadoRaca" size="60" maxlength="80" 
     value="#{cadastroGadoBean.gado.gadoRaca}"/> 

     <p:outputLabel value="Finalidade do Gado" for="gadoFinalidade"/> 
     <p:inputText id="gadoFinalidade" size="60" maxlength="80" 
     value="#{cadastroGadoBean.gado.gadoFinalidade}"/> 


     <p:outputLabel value="Status" for="gadoStatus"/> 
     <p:inputText id="gadoStatus" size="60" maxlength="80" 
     value="#{cadastroGadoBean.gado.gadoStatus}"/> 

     <p:outputLabel value="Ultima Latitude" for="ultimaLat"/> 
     <p:inputText id="ultimaLat" size="60" maxlength="80" 
     value="#{cadastroGadoBean.gado.ultimaLat}"/> 

     <p:outputLabel value="Ultima Longitude" for="ultimaLong"/> 
     <p:inputText id="ultimaLong" size="60" maxlength="80" 
     value="#{cadastroGadoBean.gado.ultimaLong}"/> 

     <p:outputLabel value="Tag"/> 
      <p:selectOneMenu title="Selecione a tag" value="#{cadastroGadoBean.tag}"> 
       <f:selectItems value="#{cadastroGadoBean.listTags}" var="tag" 
       itemValue="#{tag}" itemLabel="#{tag.descricao}"/> 
      </p:selectOneMenu> 
     </p:panelGrid> 
    </h:form> 
    </ui:define> 
</ui:composition> 
+0

*その後、私はBalusCのコードを使用し、それは働いた*したがって、Balusのコードを使用します。 –

+0

申し訳ありません。 B4私は彼のコードを使用して別のエラーがありました。今私はこれを得る。 –

答えて

0

Javaのコンバーター...次のようにコードを変更してください:

@FacesConverter("tagConverter") 
public class ConverteGadoTag implements Converter { 

XHTML:

<p:selectOneMenu title="Selecione a tag" value="#{cadastroGadoBean.tag}" converter="tagConverter"> 
       <f:selectItems value="#{cadastroGadoBean.listTags}" var="tag" 
       itemValue="#{tag}" itemLabel="#{tag.descricao}"/> 
      </p:selectOneMenu> 

だけで明示的に使用するために、コンバータを定義します。

こちらがお役に立てば幸いです。

+0

私はすでに試してきましたが、それは正しいことはありません。 getAsStringメソッドのこの行にnullPointerExceptionがあります。 return((Tag)value).getTagId()。toString(); –

+0

'tagId'は設定されていませんか?デバッグしてみてください。クラス 'タグ'はどのように見えますか? – chaeschuechli

+0

私は最初からこのエラーメッセージを解決しましたが、今度はtagId以外のDBにすべてのフィールドを保存します。エラーログはありません。 \t '@Id \t @GeneratedValueを 私のクラスのタグがatributesを持っており、idはそのようなものです(私は、コメントでここにいない場合はTellmeのを、コードを投稿することができた場合IDK)(戦略= GenerationType.SEQUENCE、発電機=" (tag_sequence)) \t @Column(name = "tag_id") \t public long getTagId(){ \t \t return tagId; \t} ' クラスGado: 'プライベートタグタグ; ... @ManyToOne \t @JoinColumn(名前= "TAG_ID") \tパブリックタグのgetTag(){ \t \tリターンタグ。 \t} ' –