Jsf 当state\u saving\u method设置为client时,转换器未设置值
我面临一个wierd问题,即当bean被视范围限制时。当state_saving_方法设置为client时,转换器未将值设置为指定类型。当它被设置为“服务器”时,它可以很好地工作;如果bean是requestscope并设置为“客户机”,它也可以很好地工作 网页看起来像这样Jsf 当state\u saving\u method设置为client时,转换器未设置值,jsf,jsf-2,Jsf,Jsf 2,我面临一个wierd问题,即当bean被视范围限制时。当state_saving_方法设置为client时,转换器未将值设置为指定类型。当它被设置为“服务器”时,它可以很好地工作;如果bean是requestscope并设置为“客户机”,它也可以很好地工作 网页看起来像这样 <h:selectOneMenu value="#{managedBeanState.selectedFoo}" > <f:selectItem itemLabel="" /> <
<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>
状态保存方法:“客户端”或“服务器”(=默认值)。参见JSF规范2.5.2
javax.faces.STATE_保存方法
客户
javax.faces.PARTIAL_STATE_保存
错误的
我已经检查了BalusC,但将PARTIAL_STATE_SAVENG设置为false没有帮助。我也在调试模式下运行,看到getAsObject确实返回了对象,但此后它从未调用setter。因此,当为第二个下拉菜单调用getter时,它会在该菜单中检查此禁用=“#{empty managedBeanState.selectedFoo}”
它总是空的
更新:在JIRA上创建了一个
equals()
方法,看起来您的Foo
类的equals()方法被破坏了。但这应该导致著名的“验证错误:值无效”错误。您是否检查了服务器日志和/或添加了
或
?您所指的equals()方法是什么?是的,我试过放置,它只是默默地不做任何事情。暂时来说,我将managedBean作为请求范围,managedBeanState作为视图范围,效果很好。