gpt4 book ai didi

java - jsf 渲染条件不适用于 selectOneMenu

转载 作者:行者123 更新时间:2023-12-01 14:37:51 25 4
gpt4 key购买 nike

嗨,我读过很多类似的问题,但答案对我不起作用。

我有这个

<p:selectOneMenu id="tipoTaxon" value="#{taxonDM.taxon.tipoTaxon}"
name="tipoTaxon">
<f:converter converterId="tipoTaxonConverter" />
<f:selectItem itemLabel="Seleccione uno" itemValue="0" />
<f:selectItems value="#{tipoTaxonDM.tiposTaxones}" var="txn"
itemValue="#{txn.idTipoTaxon}" itemLabel="#{txn.nombreTipo}" />
<p:ajax render="test" />
</p:selectOneMenu>
<p:inputText id="test" rendered="#{taxonDM.taxon.tipoTaxon != null}" />

如您所见,我想在选择选项时渲染测试。 tipoTaxon 基本上是我的数据库中的一个表,并且是类,所以我必须制作一个转换器。现在看来是有效的,我没有收到以前的错误。现在我没有收到任何错误,但“测试”没有被渲染。

我尝试了以下方法

#{taxonDM.taxon.tipoTaxon != null}

还有

#{taxonDM.taxon.tipoTaxon.idTipoTaxon != null}"

我尝试在另一个面板上设置测试

<h:panelGrid columns="2" id="formTaxon">
<h:outputLabel value="Nombre Científico Taxón" for="taxonInput" />
<p:inputText value="#{taxonDM.taxon.nombreCientificoTaxon}"
id="taxonInput" />
<h:outputLabel value="Nombre Común" for="nombreComunInput" />
<p:inputText value="#{taxonDM.taxon.nombreComunTaxon}"
id="nombreComunInput" />
<h:outputLabel value="Tipo" for="tipoTaxon" />
<p:selectOneMenu id="tipoTaxon" value="#{taxonDM.taxon.tipoTaxon}"
name="tipoTaxon">
<f:converter converterId="tipoTaxonConverter" />
<f:selectItem itemLabel="Seleccione uno" itemValue="0" />
<f:selectItems value="#{tipoTaxonDM.tiposTaxones}" var="txn"
itemValue="#{txn.idTipoTaxon}" itemLabel="#{txn.nombreTipo}" />
<p:ajax render="formTaxon2" />

</p:selectOneMenu>
</h:panelGrid>
<h:panelGrid columns="2" id="formTaxon2">
<p:inputText id="test" rendered="#{taxonDM.taxon.tipoTaxon != null}" />
</h:panelGrid>

使用 render="test"或 render="formTaxon2"

我向 p:ajax 添加了一个监听器方法,它起作用了,所以我知道它正在被调用。

    public void tipoTaxonesXX(AjaxBehaviorEvent e){
System.out.println("Working");
}

它确实在我的控制台上打印了“Working”。我的表单也没有保存,所以我猜它在从 tipotaxon 或数字转换时遇到了麻烦,但它变成了空,我稍后会修复这个问题。

如果有人需要,这里是转换器

import ec.edu.puce.biologia.model.TipoTaxon;

@FacesConverter("tipoTaxonConverter")
public class TipoTaxonConverter implements Converter {

private TipoTaxonDao tipoTaxonDao;

@Override
public Object getAsObject(final FacesContext arg0, final UIComponent arg1,
final String value) {
if (value == null || !value.matches("\\d+")) {
return null;
}
try {
TipoTaxon tipoTaxon = tipoTaxonDao.recuperar(Long.valueOf(value));
System.out.println("Getting the operation value = "
+ tipoTaxon.getNombreTipo());
return tipoTaxon;
} catch (NumberFormatException e) {
return null;
// throw new ConverterException(new
// FacesMessage("Unknown operation ID: " + value));
} /*
* catch (EntidadNoEncontradaException e) { throw new
* ConverterException(new FacesMessage("Unknown operation ID: " +
* value)); }
*/
}

@Override
public String getAsString(final FacesContext arg0, final UIComponent arg1,
final Object value) {
if (!(value instanceof TipoTaxon)
|| ((TipoTaxon) value).getIdTipoTaxon() == null) {
return null;
}

return String.valueOf(((TipoTaxon) value).getIdTipoTaxon());
}
}

我需要添加一些异常(exception)

更新答案我这里的代码有很多错误,我修改了很多,但主要问题是转换器上的EJB不起作用。我最终使用了 ManagedBean。更多信息请点击 http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters

最佳答案

正如我在 previous answer to your question 中所说,两者 <p:selectOneMany>值必须指向您的用户类别,TipoTaxon每个 <f:selectItem>/<f:selectItems> itemValue 还必须指向相同的用户类别 TipoTaxon .

如您所见, itemValue="0" 都没有,也不是itemValue="txn.nombreTipo"满足上述陈述。纠正它并查看它的工作情况。

<小时/>

我对 future 发布的建议是发布完整、相关且必要格式化的代码,在您的情况下包括转换器代码、模型类和托管 bean 部分。另外,不要将相同问题发布两次/三次等,而是尝试自己解决,否则它将因重复而被关闭。 p>

关于java - jsf 渲染条件不适用于 selectOneMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16312074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com