【问题标题】:Converter doesn't work with a @ViewScoped转换器不适用于 @ViewScoped
【发布时间】:2012-08-13 13:52:56
【问题描述】:

我遇到了转换器的问题。在我的 xhtml 文件中,我有一个带有对象列表的 selectOneMenu,我想在我的 managedBean 中设置一个对象。

如果我的 managedBean 有@SessionScoped,则 managedbean 中的对象被填充,但如果 managedeban 有@ViewScoped,则转换器永远不会被使用并且我的对象为 null。

如何解决这个问题?

Xhtml:

<p:selectOneMenu value="#{rechercheBean.role}" converter="#{typConverter}">
    <f:selectItems id="item" value="#{typBean.roles}" var="r" itemLabel="#{r.valeur}" itemValue="#{r}" />
</p:selectOneMenu>

类型转换器:

public class TypConverter implements Converter{
    @EJB
    private TypFacadeLocal  TypBean;

    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
        if (submittedValue.trim().equals("")) {
            return null;
        }
        else {
            try {
                Integer id = Integer.parseInt(submittedValue);
                Typ typ = new Typ();
                typ = TypBean.find(id);
                return typ;
            }
            catch (NumberFormatException exception) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Typ non valide"));
            }
        }
    }

    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        }
        else {
            return String.valueOf(((Typ) value).getId());
        }
    }
}

发送很多

【问题讨论】:

  • 到目前为止发布的代码中看不到问题的原因。请发布 SSCCE。
  • 这个问题没有代码错误...当managedBean为@ViewScoped时,我的managedBean中的对象保持null。如果我在 TypConverter 中放了一个断点,它永远不会在这个 Scope 中使用(与 @SessionScoped 相反)
  • 我明白这一点。我只是基本上告诉您到目前为止发布的代码看起来都很好,因此根据给定的代码无法找到/理解您的具体问题的原因。您需要提供更多上下文/代码,以便我们能够发现真正的原因。 SSCCE 风格的 sn-p 将是最理想的(对于 JSF,这基本上是 尽可能小的 XHTML 文件和支持 bean 类,我们可以直接复制'n'paste'n'run 而不做任何更改自己看问题)。

标签: jsf null converter session-scope view-scope


【解决方案1】:

问题在于组件 c:when。有了组件的属性渲染器,就没有问题了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2012-08-27
    • 2012-04-14
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多