Primefaces selectOneMenu侦听器未使用除字符串以外的其他对象调用 [英] Primefaces selectOneMenu listener not called with Objects other than Strings
问题描述
我正在使用Jsf 2.0和Primefaces 3.2实现一个Webapp.
我注意到了这种意外行为:我有一个selectOneMenu
和一个commandButton
,如下所示
I'm implementing a webapp using Jsf 2.0 and Primefaces 3.2.
I've noticed this unexpected behavoiur: I have a selectOneMenu
and a commandButton
, as below
<p:selectOneMenu id="selsel" value="#{bean.myObj}">
<f:selectItems value="#{bean.myObjList}" />
</p:selectOneMenu>
<p:commandButton id="btnid" value="Ok" actionListener="#{bean.updateSelectValues()}" />
发生的情况是,如果myObj
不是String
,则不会调用updateSelectValues
方法.我根本看不到任何异常或错误,只是没有被调用.这是支持bean:
What happens is that if myObj
is not a String
, the updateSelectValues
method is not called. I can't see any exception or error at all, it's just not called. Here's the backing bean:
private List<MyObj> myObjList;
private MyObj myObj;
// getters and setters
public void updateSelectValues() {
System.out.println(this.myObj);
}
myObj的代码:
public class MyObj implements Serializable {
private static final long serialVersionUID = 1L;
private String param1;
private int param2;
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("MyObj [param1=");
builder.append(this.param1);
builder.append(", param2=");
builder.append(this.param2);
builder.append("]");
return builder.toString();
}
}
推荐答案
这是因为HTML和HTTP无法理解Java对象.当JSF生成HTML时,所有Java对象都将转换为String
.当要由JSF处理提交的表单数据时,应该将所有String
的HTTP请求参数都转换回Java对象.
That's because HTML and HTTP does not understand Java objects. All Java objects are converted to String
when HTML is to be produced by JSF. All HTTP request parameters which are String
are supposed to be converted back to Java object when the submitted form data is to be processed by JSF.
对于您的具体问题,如果您添加了与表单等效的<h:message>
,<h:messages>
或PrimeFaces(并在ajax提交时对其进行了更新),那么您应该已经注意到空转换器"的转换错误".另外,如果您已注意服务器日志,则还应该看到有关未处理消息的警告.
As to your concrete problem, if you have added a <h:message>
, <h:messages>
or the PrimeFaces equivalent to the form (and also updated it on ajax submits), then you should have noticed a conversion error for "null converter". Also, if you have paid attention to the server log, you should also have seen a warning about an unhandled message.
您需要创建自定义 Converter
可以在MyObj
及其唯一的String
表示形式之间进行转换.例如:
You need to create a custom Converter
which converts between MyObj
and its unique String
representation. For example:
@FacesConverter(forClass=MyObj.class)
public class MyObjConverter implements Converter {
@Override
public String getAsString(FacesContext context, UIComponent component, Object valueToRender) {
// Convert MyObj to its unique String representation.
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
// Convert String to MyObj.
}
}
通常,这些对象已经通过其ID存储在某些数据库或映射中.然后,您可以完全使用该ID作为唯一的String
表示形式.
Usually those objects are already stored in some database or mapping by their ID. You then use exactly that ID as unique String
representation.
这篇关于Primefaces selectOneMenu侦听器未使用除字符串以外的其他对象调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!