我正面临着一个奇怪的问题,即当bean是视图时。当state_saving_method设置为client时,转换器不会将值设置为指定的类型。它设置为“server”时效果很好,如果bean被请求并且设置为“client”,它也可以正常工作
网页看起来像这样。
<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 blog,但将PARTIAL_STATE_SAVING设置为false并没有帮助。我也在调试模式下运行,看到getAsObject确实返回了对象,但之后它从不调用setter。因此,当第二个下拉菜单调用getter时,它会检查这个disabled =“#{empty managedBeanState.selectedFoo}” 它始终为空。
更新:创建了issue ON JIRA