1

我面临一个奇怪的问题,当豆子被视域时。当 state_saving_method 设置为 client 时,转换器未将值设置为指定类型。当它设置为“服务器”时它工作得很好,如果 bean 被请求范围并设置为“客户端”也可以正常工作

网页看起来像这样。

<h:selectOneMenu value="#{managedBeanState.selectedFoo}" >
    <f:selectItem itemLabel="" />
    <f:selectItems value="#{managedBean.foos)" var="foo" itemLabel="#{foo.name} " itemValue="#{foo}" />
    <f:converter converterId="fooConverter" />
    <p:ajax update="whom" onstart="menuStart();" oncomplete="menuComplete();" />
</h:selectOneMenu>

  <h:selectOneMenu id="whom" value="#{managedBeanState.person}" 
                   disabled="#{empty managedBeanState.selectedFoo}">
    <f:selectItem itemLabel="" />
    <f:selectItems value="#{managedBean.persons)" var="person" itemLabel="#{person.name} " itemValue="#{person}" />
    <f:converter converterId="personConverter" />
    <p:ajax update="email" onstart="menuStart();" oncomplete="menuComplete();" />
</h:selectOneMenu>

转换器类看起来像这样。

@FacesConverter(value = "fooConverter", forClass = com.examples.Foo.class)
public class EntityConverter implements Converter {

@Override
public Object getAsObject(FacesContext context, UIComponent c, String value) {
    if (value.isEmpty()) {
        return null;
    }

    try {
        SessionFactory sessionFactory = (SessionFactory) FacesContextUtils.getRequiredWebApplicationContext(FacesContext.getCurrentInstance()).getBean("sessionFactory");
        String[] idAndClassName = value.split(":|_");
        String className = idAndClassName[1];
        String id = idAndClassName[0];
        return sessionFactory.getCurrentSession().load(Class.forName(className), Long.parseLong(id));
    } catch(Exception e) {
        throw new IllegalStateException("Error loading from value:" + value, e);
    }
}

@Override
public String getAsString(FacesContext context, UIComponent c, Object value) {
    if (value == null || StringUtils.isEmpty(value.toString())) {
        return StringUtils.EMPTY;
    }

    return ((Foo) value).getId().toString() + ":" + value.getClass().getName();
}
}

我的 web.xml 看起来像这样

<context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>false</param-value>
</context-param>

我检查了 BalusC博客 ,但将 PARTIAL_STATE_SAVING 设置为 false 并没有帮助。我还在调试模式下运行,看到 getAsObject 确实返回了对象,但之后它再也没有调用 setter。因此,当为第二个下拉菜单调用 getter 时,它检查 this disabled="#{empty managedBeanState.selectedFoo}" 它始终为空。

更新:在 JIRA上创建了一个问题

4

0 回答 0