繁体   English   中英

转换器无法正常工作

[英]Converter does not work correctly

伙计们,我的问题是加载页面时出现此错误。 我正在尝试为我的selectOneMenu字段使用转换器,它必须很简单,但是找不到解决方案。 数据库中有数据,没有错误日志。

在此处输入图片说明

转换器:

@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>

尝试如下更改您的代码...

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>

只需定义要显式使用的转换器即可。

我希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM